1 | <?php |
||
2 | |||
3 | namespace BultonFr\Annotation\Parsers; |
||
4 | |||
5 | use Countable; |
||
6 | use Exception; |
||
7 | use Iterator; |
||
8 | use Reflector; |
||
9 | use BultonFr\Annotation\ParserManager; |
||
10 | |||
11 | /** |
||
12 | * Abstract class for parser which contain a list of parser. |
||
13 | * |
||
14 | * @package BultonFr\Annotation |
||
15 | */ |
||
16 | abstract class AbstractManyParser implements Iterator, Countable |
||
17 | { |
||
18 | /** |
||
19 | * @const EXCEP_KEY_NOT_EXIST Exception code if user ask a key which |
||
20 | * not exist. |
||
21 | */ |
||
22 | const EXCEP_KEY_NOT_EXIST = 301001; |
||
23 | |||
24 | /** |
||
25 | * The parser manager system |
||
26 | * |
||
27 | * @var \BultonFr\Annotation\ParserManager |
||
28 | */ |
||
29 | protected $parserManager; |
||
30 | |||
31 | /** |
||
32 | * The Reflection object for the readed item |
||
33 | * |
||
34 | * @var \Reflector |
||
35 | */ |
||
36 | protected $reflection; |
||
37 | |||
38 | /** |
||
39 | * The list of Parser object |
||
40 | * |
||
41 | * @var AbstractParser[<string>] |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
42 | */ |
||
43 | protected $list = []; |
||
44 | |||
45 | /** |
||
46 | * The list of keys used in $list |
||
47 | * |
||
48 | * @var string[<int>] |
||
0 ignored issues
–
show
|
|||
49 | */ |
||
50 | protected $itemKeys = []; |
||
51 | |||
52 | /** |
||
53 | * The current index in the Iterator loop |
||
54 | * |
||
55 | * @var integer |
||
56 | */ |
||
57 | protected $index = 0; |
||
58 | |||
59 | /** |
||
60 | * Constructor |
||
61 | * |
||
62 | * @param ParserManager $parserManager |
||
63 | * @param Reflector $reflection |
||
64 | */ |
||
65 | public function __construct( |
||
66 | ParserManager $parserManager, |
||
67 | Reflector $reflection |
||
68 | ) { |
||
69 | 1 | $this->parserManager = $parserManager; |
|
70 | 1 | $this->reflection = $reflection; |
|
71 | 1 | } |
|
72 | |||
73 | /** |
||
74 | * Instanciate a new parser for each item find |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | abstract public function run(); |
||
79 | |||
80 | /** |
||
81 | * Get the parser manager system |
||
82 | * |
||
83 | * @return \BultonFr\Annotation\ParserManager |
||
84 | */ |
||
85 | public function getParserManager(): ParserManager |
||
86 | { |
||
87 | 1 | return $this->parserManager; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get the Reflection object for the readed item |
||
92 | * |
||
93 | * @return \Reflector |
||
94 | */ |
||
95 | public function getReflection(): Reflector |
||
96 | { |
||
97 | 1 | return $this->reflection; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get the list of Parser object |
||
102 | * |
||
103 | * @return AbstractParser[<string>] |
||
0 ignored issues
–
show
|
|||
104 | */ |
||
105 | public function getList(): array |
||
106 | { |
||
107 | 1 | return $this->list; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get the list of keys used in $list |
||
112 | * |
||
113 | * @return string[<int>] |
||
0 ignored issues
–
show
|
|||
114 | */ |
||
115 | public function getItemKeys(): array |
||
116 | { |
||
117 | 1 | return $this->itemKeys; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Get the current index in the Iterator loop |
||
122 | * |
||
123 | * @return integer |
||
124 | */ |
||
125 | public function getIndex(): int |
||
126 | { |
||
127 | 1 | return $this->index; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * {@inheritDoc} |
||
132 | */ |
||
133 | public function current(): AbstractParser |
||
134 | { |
||
135 | 1 | $key = $this->itemKeys[$this->index]; |
|
136 | |||
137 | 1 | return $this->list[$key]; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * {@inheritDoc} |
||
142 | */ |
||
143 | public function key(): string |
||
144 | { |
||
145 | 1 | return $this->itemKeys[$this->index]; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritDoc} |
||
150 | */ |
||
151 | public function next() |
||
152 | { |
||
153 | 1 | ++$this->index; |
|
154 | 1 | } |
|
155 | |||
156 | /** |
||
157 | * {@inheritDoc} |
||
158 | */ |
||
159 | public function rewind() |
||
160 | { |
||
161 | 1 | $this->index = 0; |
|
162 | 1 | } |
|
163 | |||
164 | /** |
||
165 | * {@inheritDoc} |
||
166 | */ |
||
167 | public function valid(): bool |
||
168 | { |
||
169 | 1 | return array_key_exists($this->index, $this->itemKeys); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * {@inheritDoc} |
||
174 | */ |
||
175 | public function count(): int |
||
176 | { |
||
177 | 1 | return count($this->list); |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Add a new parser for an item to the list |
||
182 | * |
||
183 | * @param string $itemKey The item name (method or property name) |
||
184 | * @param AbstractParser $item The parser for the item |
||
185 | * |
||
186 | * @return void |
||
187 | */ |
||
188 | public function addItem(string $itemKey, AbstractParser $item) |
||
189 | { |
||
190 | 1 | $this->list[$itemKey] = $item; |
|
191 | 1 | $this->itemKeys[] = $itemKey; |
|
192 | 1 | } |
|
193 | |||
194 | /** |
||
195 | * Check if a key exist |
||
196 | * |
||
197 | * @param string $key |
||
198 | * |
||
199 | * @return boolean |
||
200 | */ |
||
201 | public function hasKey(string $key): bool |
||
202 | { |
||
203 | 1 | return array_key_exists($key, $this->list); |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Obtain key value |
||
208 | * |
||
209 | * @param string $key |
||
210 | * |
||
211 | * @return AbstractParser |
||
212 | */ |
||
213 | public function obtainForKey(string $key): AbstractParser |
||
214 | { |
||
215 | 1 | if ($this->hasKey($key) === false) { |
|
216 | 1 | throw new Exception( |
|
217 | 1 | 'The key '.$key.' not exist in the list', |
|
218 | 1 | static::EXCEP_KEY_NOT_EXIST |
|
219 | ); |
||
220 | } |
||
221 | |||
222 | 1 | return $this->list[$key]; |
|
223 | } |
||
224 | } |
||
225 |