1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
14
|
|
|
* all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Annotation\Definition; |
26
|
|
|
|
27
|
|
|
use Brickoo\Component\Annotation\Annotation; |
28
|
|
|
use Brickoo\Component\Common\Assert; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AnnotationDefinition |
32
|
|
|
* |
33
|
|
|
* Implements an annotation definition. |
34
|
|
|
* @author Celestino Diaz <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class AnnotationDefinition { |
37
|
|
|
|
38
|
|
|
/** @var integer */ |
39
|
|
|
private $target; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
private $annotationName; |
43
|
|
|
|
44
|
|
|
/** @var boolean */ |
45
|
|
|
private $required; |
46
|
|
|
|
47
|
|
|
/** @var AnnotationParameterDefinition[] */ |
48
|
|
|
private $requiredParameters; |
49
|
|
|
|
50
|
|
|
/** @var array */ |
51
|
|
|
private $optionalParameters; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Class constructor. |
55
|
|
|
* @param string $annotationName |
56
|
|
|
* @param integer $target |
57
|
|
|
* @param boolean $required |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
*/ |
60
|
3 |
|
public function __construct($annotationName, $target = Annotation::TARGET_CLASS, $required = true) { |
61
|
3 |
|
Assert::isString($annotationName); |
62
|
3 |
|
Assert::isInteger($target); |
63
|
3 |
|
Assert::isBoolean($required); |
64
|
3 |
|
$this->target = $target; |
65
|
3 |
|
$this->annotationName = $annotationName; |
66
|
3 |
|
$this->required = $required; |
67
|
3 |
|
$this->requiredParameters = []; |
68
|
3 |
|
$this->optionalParameters = []; |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the target. |
73
|
|
|
* @return integer the annotation target |
74
|
|
|
*/ |
75
|
1 |
|
public function getTarget() { |
76
|
1 |
|
return $this->target; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Checks if the annotation matches a target. |
81
|
|
|
* @param integer $target |
82
|
|
|
* @return boolean check result |
83
|
|
|
*/ |
84
|
1 |
|
public function isTarget($target) { |
85
|
1 |
|
Assert::isInteger($target); |
86
|
1 |
|
return $this->getTarget() == $target; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the annotation name. |
91
|
|
|
* @return string the annotation name |
92
|
|
|
*/ |
93
|
1 |
|
public function getName() { |
94
|
1 |
|
return $this->annotationName; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Checks if the annotation is required. |
99
|
|
|
* @return boolean check result |
100
|
|
|
*/ |
101
|
1 |
|
public function isRequired() { |
102
|
1 |
|
return $this->required; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Adds a parameter to annotation. |
107
|
|
|
* @param \Brickoo\Component\Annotation\Definition\AnnotationParameterDefinition $parameter |
108
|
|
|
* @return \Brickoo\Component\Annotation\Definition\AnnotationDefinition |
109
|
|
|
*/ |
110
|
3 |
|
public function addParameter(AnnotationParameterDefinition $parameter) { |
111
|
3 |
|
if ($parameter->isRequired()) { |
112
|
2 |
|
$this->requiredParameters[] = $parameter; |
113
|
2 |
|
} |
114
|
|
|
else { |
115
|
1 |
|
$this->optionalParameters[] = $parameter; |
116
|
|
|
} |
117
|
3 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the required parameters. |
122
|
|
|
* @return AnnotationParameterDefinition[] the required parameters |
123
|
|
|
*/ |
124
|
1 |
|
public function getRequiredParameters() { |
125
|
1 |
|
return $this->requiredParameters; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Checks if the annotation has required parameters. |
130
|
|
|
* @return boolean check result |
131
|
|
|
*/ |
132
|
1 |
|
public function hasRequiredParameters() { |
133
|
1 |
|
return empty($this->requiredParameters) === false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns the optional parameters. |
138
|
|
|
* @return AnnotationParameterDefinition[] the optional parameters |
139
|
|
|
*/ |
140
|
1 |
|
public function getOptionalParameters() { |
141
|
1 |
|
return $this->optionalParameters; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|