AnnotationException.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. namespace Doctrine\Common\Annotations;
  20. /**
  21. * Description of AnnotationException
  22. *
  23. * @since 2.0
  24. * @author Benjamin Eberlei <kontakt@beberlei.de>
  25. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  26. * @author Jonathan Wage <jonwage@gmail.com>
  27. * @author Roman Borschel <roman@code-factory.org>
  28. */
  29. class AnnotationException extends \Exception
  30. {
  31. /**
  32. * Creates a new AnnotationException describing a Syntax error.
  33. *
  34. * @param string $message Exception message
  35. * @return AnnotationException
  36. */
  37. public static function syntaxError($message)
  38. {
  39. return new self('[Syntax Error] ' . $message);
  40. }
  41. /**
  42. * Creates a new AnnotationException describing a Semantical error.
  43. *
  44. * @param string $message Exception message
  45. * @return AnnotationException
  46. */
  47. public static function semanticalError($message)
  48. {
  49. return new self('[Semantical Error] ' . $message);
  50. }
  51. /**
  52. * Creates a new AnnotationException describing a constant semantical error.
  53. *
  54. * @since 2.3
  55. * @param string $identifier
  56. * @param string $context
  57. * @return AnnotationException
  58. */
  59. public static function semanticalErrorConstants($identifier, $context = null)
  60. {
  61. return self::semanticalError(sprintf(
  62. "Couldn't find constant %s%s", $identifier,
  63. $context ? ", $context." : "."
  64. ));
  65. }
  66. /**
  67. * Creates a new AnnotationException describing an error which occurred during
  68. * the creation of the annotation.
  69. *
  70. * @since 2.2
  71. * @param string $message
  72. * @return AnnotationException
  73. */
  74. public static function creationError($message)
  75. {
  76. return new self('[Creation Error] ' . $message);
  77. }
  78. /**
  79. * Creates a new AnnotationException describing an type error of an attribute.
  80. *
  81. * @since 2.2
  82. * @param string $attributeName
  83. * @param string $annotationName
  84. * @param string $context
  85. * @param string $expected
  86. * @param mixed $actual
  87. * @return AnnotationException
  88. */
  89. public static function typeError($attributeName, $annotationName, $context, $expected, $actual)
  90. {
  91. return new self(sprintf(
  92. '[Type Error] Attribute "%s" of @%s declared on %s expects %s, but got %s.',
  93. $attributeName,
  94. $annotationName,
  95. $context,
  96. $expected,
  97. is_object($actual) ? 'an instance of '.get_class($actual) : gettype($actual)
  98. ));
  99. }
  100. /**
  101. * Creates a new AnnotationException describing an required error of an attribute.
  102. *
  103. * @since 2.2
  104. * @param string $attributeName
  105. * @param string $annotationName
  106. * @param string $context
  107. * @param string $expected
  108. * @return AnnotationException
  109. */
  110. public static function requiredError($attributeName, $annotationName, $context, $expected)
  111. {
  112. return new self(sprintf(
  113. '[Type Error] Attribute "%s" of @%s declared on %s expects %s. This value should not be null.',
  114. $attributeName,
  115. $annotationName,
  116. $context,
  117. $expected
  118. ));
  119. }
  120. /**
  121. * Creates a new AnnotationException describing a invalid enummerator.
  122. *
  123. * @since 2.4
  124. * @param string $attributeName
  125. * @param string $annotationName
  126. * @param string $context
  127. * @param array $available
  128. * @param mixed $given
  129. * @return AnnotationException
  130. */
  131. public static function enumeratorError($attributeName, $annotationName, $context, $available, $given)
  132. {
  133. throw new self(sprintf(
  134. '[Enum Error] Attribute "%s" of @%s declared on %s accept only [%s], but got %s.',
  135. $attributeName,
  136. $annotationName,
  137. $context,
  138. implode(', ', $available),
  139. is_object($given) ? get_class($given) : $given
  140. ));
  141. }
  142. /**
  143. * @return AnnotationException
  144. */
  145. public static function optimizerPlusSaveComments()
  146. {
  147. throw new self("You have to enable opcache.save_comments=1 or zend_optimizerplus.save_comments=1.");
  148. }
  149. }