1 | <?php |
||
36 | class AnnotationParser { |
||
37 | |||
38 | /** @const regular expressions templates */ |
||
39 | const REGEX_ANNOTATION = "~[^\"]%s(?<%s>[\\w]+)[^%s(]*\\(\\s*(?<%s>[^\\)\\(]*)\\s*\\)~"; |
||
40 | const REGEX_PARAMETER = "~((?<%s>\\w+)\\s*=)?\\s*(?<%s>(\"[^\"]*\")|[0-9\\.]+|true|false)~"; |
||
41 | const REGEX_VALUE = "~((?<%s>\\w+)\\s*=)?\\s*(?<%s>('[^']*')|[0-9\\.]+|true|false)~"; |
||
42 | const REGEX_ARRAY = "~^\\[\\[.+\\]\\]$~"; |
||
43 | |||
44 | /** @const regular expressions capture groups */ |
||
45 | const REGEX_CAPTURE_ANNOTATION = "annotation"; |
||
46 | const REGEX_CAPTURE_VALUES = "values"; |
||
47 | const REGEX_CAPTURE_PARAM = "param"; |
||
48 | const REGEX_CAPTURE_VALUE = "value"; |
||
49 | |||
50 | /** @var string */ |
||
51 | private $annotationPrefix; |
||
52 | |||
53 | /** @var array */ |
||
54 | private $annotationWhitelist; |
||
55 | |||
56 | /** |
||
57 | * Class constructor. |
||
58 | * @param string $annotationPrefix |
||
59 | */ |
||
60 | 1 | public function __construct($annotationPrefix = "@") { |
|
65 | |||
66 | /** |
||
67 | * Changes the annotation prefix. |
||
68 | * @param string $annotationPrefix |
||
69 | * @return \Brickoo\Component\Annotation\AnnotationParser |
||
70 | */ |
||
71 | 1 | public function setAnnotationPrefix($annotationPrefix) { |
|
76 | |||
77 | /** |
||
78 | * Changes the annotations whitelist. |
||
79 | * @param array $whitelist |
||
80 | * @return \Brickoo\Component\Annotation\AnnotationParser |
||
81 | */ |
||
82 | 1 | public function setAnnotationWhitelist(array $whitelist) { |
|
86 | |||
87 | /** |
||
88 | * Parse the document comment to extract annotations. |
||
89 | * @param string $target |
||
90 | * @param string $targetLocation |
||
91 | * @param string $docComment |
||
92 | * @throws \InvalidArgumentException |
||
93 | * @return \Brickoo\Component\Annotation\Annotation[] |
||
94 | */ |
||
95 | 1 | public function parse($target, $targetLocation, $docComment) { |
|
107 | |||
108 | /** |
||
109 | * Returns the matches containing annotations. |
||
110 | * @param string $annotationPrefix |
||
111 | * @param string $docComment |
||
112 | * @return array |
||
113 | */ |
||
114 | 1 | private function getAnnotationsMatches($annotationPrefix, $docComment) { |
|
128 | |||
129 | /** |
||
130 | * Returns a list containing the annotation name and values. |
||
131 | * @param array $annotationsMatches |
||
132 | * @return array |
||
133 | */ |
||
134 | 1 | private function getAnnotationList(array $annotationsMatches) { |
|
146 | |||
147 | /** |
||
148 | * Checks if the annotation is in the whitelist. |
||
149 | * @param string $annotation |
||
150 | * @return boolean |
||
151 | */ |
||
152 | 1 | private function isAnnotationInWhitelist($annotation) { |
|
155 | |||
156 | /** |
||
157 | * Returns the annotations values. |
||
158 | * @param string $annotationIndex |
||
159 | * @param array $annotationsMatches |
||
160 | * @return array |
||
161 | */ |
||
162 | 1 | private function getAnnotationValues($annotationIndex, array $annotationsMatches) { |
|
167 | |||
168 | /** |
||
169 | * Returns the parameters values pairs. |
||
170 | * @param string $valuesString |
||
171 | * @param string $valuesRegex |
||
172 | * @return array |
||
173 | */ |
||
174 | 1 | private function getParameterValues($valuesString, $valuesRegex) { |
|
184 | |||
185 | /** |
||
186 | * Attach the extracted parameters values. |
||
187 | * @param array &$parameterValues |
||
188 | * @param array $values |
||
189 | * @return void |
||
190 | */ |
||
191 | 1 | private function attachParameterValues(&$parameterValues, array $values) { |
|
199 | |||
200 | /** |
||
201 | * Converts the value to appropriate type. |
||
202 | * @param string $value |
||
203 | * @return mixed |
||
204 | */ |
||
205 | 1 | private function convertValue($value) { |
|
215 | |||
216 | /** |
||
217 | * Transforms the string to appropriate scalar. |
||
218 | * @param string $value |
||
219 | * @return string|float|integer|boolean transformed value |
||
220 | */ |
||
221 | 1 | private function transformScalar($value) { |
|
226 | |||
227 | /** |
||
228 | * Transform value if is numeric. |
||
229 | * @param string $value |
||
230 | * @return void |
||
231 | */ |
||
232 | 1 | private function transformIfIsNumeric(&$value) { |
|
237 | |||
238 | /** |
||
239 | * Transform value if is boolean. |
||
240 | * @param string $value |
||
241 | * @return void |
||
242 | */ |
||
243 | 1 | private function transformIfIsBoolean(&$value) { |
|
248 | |||
249 | /** |
||
250 | * Converts annotationList into an array of annotation objects. |
||
251 | * @param integer $target |
||
252 | * @param string $targetLocation |
||
253 | * @param array $annotationList |
||
254 | * @return \Brickoo\Component\Annotation\Annotation[] |
||
255 | */ |
||
256 | 1 | private function convertAnnotations($target, $targetLocation, array $annotationList) { |
|
263 | |||
264 | } |
||
265 |