1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Annotations\Annotation; |
4
|
|
|
|
5
|
|
|
use Doctrine\Annotations\Metadata\AnnotationTarget; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Annotation that can be used to signal to the parser |
9
|
|
|
* to check the annotation target during the parsing process. |
10
|
|
|
* |
11
|
|
|
* @author Fabio B. Silva <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @Annotation |
14
|
|
|
*/ |
15
|
|
|
final class Target |
16
|
|
|
{ |
17
|
|
|
public const TARGET_CLASS = AnnotationTarget::TARGET_CLASS; |
18
|
|
|
public const TARGET_METHOD = AnnotationTarget::TARGET_METHOD; |
19
|
|
|
public const TARGET_PROPERTY = AnnotationTarget::TARGET_PROPERTY; |
20
|
|
|
public const TARGET_ANNOTATION = AnnotationTarget::TARGET_ANNOTATION; |
21
|
|
|
public const TARGET_ALL = AnnotationTarget::TARGET_ALL; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $map = [ |
27
|
|
|
'ALL' => self::TARGET_ALL, |
28
|
|
|
'CLASS' => self::TARGET_CLASS, |
29
|
|
|
'METHOD' => self::TARGET_METHOD, |
30
|
|
|
'PROPERTY' => self::TARGET_PROPERTY, |
31
|
|
|
'ANNOTATION' => self::TARGET_ANNOTATION, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $value; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Targets as bitmask. |
41
|
|
|
* |
42
|
|
|
* @var integer |
43
|
|
|
*/ |
44
|
|
|
public $targets; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Literal target declaration. |
48
|
|
|
* |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
public $literal; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Annotation constructor. |
55
|
|
|
* |
56
|
|
|
* @param array $values |
57
|
|
|
* |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
*/ |
60
|
19 |
|
public function __construct(array $values) |
61
|
|
|
{ |
62
|
19 |
|
if (!isset($values['value'])){ |
63
|
1 |
|
$values['value'] = null; |
64
|
|
|
} |
65
|
19 |
|
if (is_string($values['value'])){ |
66
|
4 |
|
$values['value'] = [$values['value']]; |
67
|
|
|
} |
68
|
19 |
|
if (!is_array($values['value'])){ |
69
|
1 |
|
throw new \InvalidArgumentException( |
70
|
1 |
|
sprintf('@Target expects either a string value, or an array of strings, "%s" given.', |
71
|
1 |
|
is_object($values['value']) ? get_class($values['value']) : gettype($values['value']) |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
18 |
|
$bitmask = 0; |
77
|
18 |
|
foreach ($values['value'] as $literal) { |
78
|
18 |
|
if(!isset(self::$map[$literal])){ |
79
|
1 |
|
throw new \InvalidArgumentException( |
80
|
1 |
|
sprintf('Invalid Target "%s". Available targets: [%s]', |
81
|
1 |
|
$literal, implode(', ', array_keys(self::$map))) |
82
|
|
|
); |
83
|
|
|
} |
84
|
17 |
|
$bitmask |= self::$map[$literal]; |
85
|
|
|
} |
86
|
|
|
|
87
|
17 |
|
$this->targets = $bitmask; |
88
|
17 |
|
$this->value = $values['value']; |
89
|
17 |
|
$this->literal = implode(', ', $this->value); |
|
|
|
|
90
|
17 |
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.