Target.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Annotation;
  20. /**
  21. * Annotation that can be used to signal to the parser
  22. * to check the annotation target during the parsing process.
  23. *
  24. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  25. *
  26. * @Annotation
  27. */
  28. final class Target
  29. {
  30. const TARGET_CLASS = 1;
  31. const TARGET_METHOD = 2;
  32. const TARGET_PROPERTY = 4;
  33. const TARGET_ANNOTATION = 8;
  34. const TARGET_ALL = 15;
  35. /**
  36. * @var array
  37. */
  38. private static $map = array(
  39. 'ALL' => self::TARGET_ALL,
  40. 'CLASS' => self::TARGET_CLASS,
  41. 'METHOD' => self::TARGET_METHOD,
  42. 'PROPERTY' => self::TARGET_PROPERTY,
  43. 'ANNOTATION' => self::TARGET_ANNOTATION,
  44. );
  45. /**
  46. * @var array
  47. */
  48. public $value;
  49. /**
  50. * Targets as bitmask.
  51. *
  52. * @var integer
  53. */
  54. public $targets;
  55. /**
  56. * Literal target declaration.
  57. *
  58. * @var integer
  59. */
  60. public $literal;
  61. /**
  62. * Annotation construct
  63. *
  64. * @param array $values
  65. *
  66. * @throws \InvalidArgumentException
  67. */
  68. public function __construct(array $values)
  69. {
  70. if (!isset($values['value'])){
  71. $values['value'] = null;
  72. }
  73. if (is_string($values['value'])){
  74. $values['value'] = array($values['value']);
  75. }
  76. if (!is_array($values['value'])){
  77. throw new \InvalidArgumentException(
  78. sprintf('@Target expects either a string value, or an array of strings, "%s" given.',
  79. is_object($values['value']) ? get_class($values['value']) : gettype($values['value'])
  80. )
  81. );
  82. }
  83. $bitmask = 0;
  84. foreach ($values['value'] as $literal) {
  85. if(!isset(self::$map[$literal])){
  86. throw new \InvalidArgumentException(
  87. sprintf('Invalid Target "%s". Available targets: [%s]',
  88. $literal, implode(', ', array_keys(self::$map)))
  89. );
  90. }
  91. $bitmask += self::$map[$literal];
  92. }
  93. $this->targets = $bitmask;
  94. $this->value = $values['value'];
  95. $this->literal = implode(', ', $this->value);
  96. }
  97. }