Target.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 LGPL. 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. public function __construct(array $values)
  67. {
  68. if (!isset($values['value'])){
  69. $values['value'] = null;
  70. }
  71. if (is_string($values['value'])){
  72. $values['value'] = array($values['value']);
  73. }
  74. if (!is_array($values['value'])){
  75. throw new \InvalidArgumentException(
  76. sprintf('@Target expects either a string value, or an array of strings, "%s" given.',
  77. is_object($values['value']) ? get_class($values['value']) : gettype($values['value'])
  78. )
  79. );
  80. }
  81. $bitmask = 0;
  82. foreach ($values['value'] as $literal) {
  83. if(!isset(self::$map[$literal])){
  84. throw new \InvalidArgumentException(
  85. sprintf('Invalid Target "%s". Available targets: [%s]',
  86. $literal, implode(', ', array_keys(self::$map)))
  87. );
  88. }
  89. $bitmask += self::$map[$literal];
  90. }
  91. $this->targets = $bitmask;
  92. $this->value = $values['value'];
  93. $this->literal = implode(', ', $this->value);
  94. }
  95. }