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; |
26
|
|
|
|
27
|
|
|
use ArrayIterator; |
28
|
|
|
use Brickoo\Component\Annotation\Exception\InvalidTargetException; |
29
|
|
|
use Brickoo\Component\Common\Assert; |
30
|
|
|
use IteratorAggregate; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* AnnotationReaderResult |
34
|
|
|
* |
35
|
|
|
* Implements an annotation reader result. |
36
|
|
|
* @author Celestino Diaz <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
class AnnotationReaderResult implements IteratorAggregate { |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
private $className; |
42
|
|
|
|
43
|
|
|
/** @var array */ |
44
|
|
|
private $annotations; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Class constructor. |
48
|
|
|
* @param string $className |
49
|
|
|
*/ |
50
|
1 |
|
public function __construct($className) { |
51
|
1 |
|
Assert::isString($className); |
52
|
1 |
|
$this->className = $className; |
53
|
1 |
|
$this->annotations = [ |
54
|
1 |
|
Annotation::TARGET_CLASS => [], |
55
|
1 |
|
Annotation::TARGET_METHOD => [], |
56
|
1 |
|
Annotation::TARGET_PROPERTY => [] |
57
|
1 |
|
]; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the target class name. |
62
|
|
|
* @return string the class name |
63
|
|
|
*/ |
64
|
1 |
|
public function getClassName() { |
65
|
1 |
|
return $this->className; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Adds an annotation to the properly container matching the collection type. |
70
|
|
|
* @param \Brickoo\Component\Annotation\Annotation $annotation |
71
|
|
|
* @throws \Brickoo\Component\Annotation\Exception\InvalidTargetException |
72
|
|
|
* @return \Brickoo\Component\Annotation\AnnotationReaderResult |
73
|
|
|
*/ |
74
|
2 |
|
public function addAnnotation(Annotation $annotation) { |
75
|
2 |
|
$target = $annotation->getTarget(); |
76
|
|
|
|
77
|
2 |
|
if (!$this->isTargetValid($target)) { |
78
|
1 |
|
throw new InvalidTargetException($target); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$this->annotations[$target][] = $annotation; |
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns an array iterator containing all annotations. |
87
|
|
|
* @return \ArrayIterator containing all annotations |
88
|
|
|
*/ |
89
|
1 |
|
public function getIterator() { |
90
|
1 |
|
return new ArrayIterator(array_merge( |
91
|
1 |
|
$this->annotations[Annotation::TARGET_CLASS], |
92
|
1 |
|
$this->annotations[Annotation::TARGET_METHOD], |
93
|
1 |
|
$this->annotations[Annotation::TARGET_PROPERTY] |
94
|
1 |
|
)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns an annotations iterator matching the target. |
99
|
|
|
* @param integer $target |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
* @throws \Brickoo\Component\Annotation\Exception\InvalidTargetException |
102
|
|
|
* @return \ArrayIterator |
103
|
|
|
*/ |
104
|
2 |
|
public function getAnnotationsByTarget($target) { |
105
|
2 |
|
Assert::isInteger($target); |
106
|
|
|
|
107
|
2 |
|
if (!$this->isTargetValid($target)) { |
108
|
1 |
|
throw new InvalidTargetException($target); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return new ArrayIterator($this->annotations[$target]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checks if the target is valid. |
116
|
|
|
* @param integer $target |
117
|
|
|
* @return boolean check result |
118
|
|
|
*/ |
119
|
5 |
|
private function isTargetValid($target) { |
120
|
5 |
|
return isset($this->annotations[$target]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|