1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\Common\Annotations;
21
22/**
23 * Description of AnnotationException
24 *
25 * @since  2.0
26 * @author Benjamin Eberlei <kontakt@beberlei.de>
27 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
28 * @author Jonathan Wage <jonwage@gmail.com>
29 * @author Roman Borschel <roman@code-factory.org>
30 */
31class AnnotationException extends \Exception
32{
33    /**
34     * Creates a new AnnotationException describing a Syntax error.
35     *
36     * @param string $message Exception message
37     *
38     * @return AnnotationException
39     */
40    public static function syntaxError($message)
41    {
42        return new self('[Syntax Error] ' . $message);
43    }
44
45    /**
46     * Creates a new AnnotationException describing a Semantical error.
47     *
48     * @param string $message Exception message
49     *
50     * @return AnnotationException
51     */
52    public static function semanticalError($message)
53    {
54        return new self('[Semantical Error] ' . $message);
55    }
56
57    /**
58     * Creates a new AnnotationException describing an error which occurred during
59     * the creation of the annotation.
60     *
61     * @since 2.2
62     *
63     * @param string $message
64     *
65     * @return AnnotationException
66     */
67    public static function creationError($message)
68    {
69        return new self('[Creation Error] ' . $message);
70    }
71
72    /**
73     * Creates a new AnnotationException describing a type error.
74     *
75     * @since 1.1
76     *
77     * @param string $message
78     *
79     * @return AnnotationException
80     */
81    public static function typeError($message)
82    {
83        return new self('[Type Error] ' . $message);
84    }
85
86    /**
87     * Creates a new AnnotationException describing a constant semantical error.
88     *
89     * @since 2.3
90     *
91     * @param string $identifier
92     * @param string $context
93     *
94     * @return AnnotationException
95     */
96    public static function semanticalErrorConstants($identifier, $context = null)
97    {
98        return self::semanticalError(sprintf(
99            "Couldn't find constant %s%s.",
100            $identifier,
101            $context ? ', ' . $context : ''
102        ));
103    }
104
105    /**
106     * Creates a new AnnotationException describing an type error of an attribute.
107     *
108     * @since 2.2
109     *
110     * @param string $attributeName
111     * @param string $annotationName
112     * @param string $context
113     * @param string $expected
114     * @param mixed  $actual
115     *
116     * @return AnnotationException
117     */
118    public static function attributeTypeError($attributeName, $annotationName, $context, $expected, $actual)
119    {
120        return self::typeError(sprintf(
121            'Attribute "%s" of @%s declared on %s expects %s, but got %s.',
122            $attributeName,
123            $annotationName,
124            $context,
125            $expected,
126            is_object($actual) ? 'an instance of ' . get_class($actual) : gettype($actual)
127        ));
128    }
129
130    /**
131     * Creates a new AnnotationException describing an required error of an attribute.
132     *
133     * @since 2.2
134     *
135     * @param string $attributeName
136     * @param string $annotationName
137     * @param string $context
138     * @param string $expected
139     *
140     * @return AnnotationException
141     */
142    public static function requiredError($attributeName, $annotationName, $context, $expected)
143    {
144        return self::typeError(sprintf(
145            'Attribute "%s" of @%s declared on %s expects %s. This value should not be null.',
146            $attributeName,
147            $annotationName,
148            $context,
149            $expected
150        ));
151    }
152
153    /**
154     * Creates a new AnnotationException describing a invalid enummerator.
155     *
156     * @since 2.4
157     *
158     * @param string $attributeName
159     * @param string $annotationName
160     * @param string $context
161     * @param array  $available
162     * @param mixed  $given
163     *
164     * @return AnnotationException
165     */
166    public static function enumeratorError($attributeName, $annotationName, $context, $available, $given)
167    {
168        return new self(sprintf(
169            '[Enum Error] Attribute "%s" of @%s declared on %s accept only [%s], but got %s.',
170            $attributeName,
171            $annotationName,
172            $context,
173            implode(', ', $available),
174            is_object($given) ? get_class($given) : $given
175        ));
176    }
177
178    /**
179     * @return AnnotationException
180     */
181    public static function optimizerPlusSaveComments()
182    {
183        return new self(
184            "You have to enable opcache.save_comments=1 or zend_optimizerplus.save_comments=1."
185        );
186    }
187
188    /**
189     * @return AnnotationException
190     */
191    public static function optimizerPlusLoadComments()
192    {
193        return new self(
194            "You have to enable opcache.load_comments=1 or zend_optimizerplus.load_comments=1."
195        );
196    }
197}
198