1 | <?php |
||
18 | class QuerySequence implements QuerySequenceInterface { |
||
19 | |||
20 | /** |
||
21 | * @var bool |
||
22 | */ |
||
23 | private $valid = true; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $position = 0; |
||
29 | |||
30 | /** |
||
31 | * @var Collection |
||
32 | */ |
||
33 | private $collection; |
||
34 | |||
35 | /** |
||
36 | * @var |
||
37 | */ |
||
38 | private $skipWhitespaces = false; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | 245 | public function __construct(Collection $collection, $initialPosition = 0) { |
|
48 | |||
49 | |||
50 | /** |
||
51 | * @return Collection |
||
52 | */ |
||
53 | 9 | public function getCollection() { |
|
56 | |||
57 | |||
58 | /** |
||
59 | * @param int $position |
||
60 | * @return QuerySequence |
||
61 | */ |
||
62 | 191 | public function setPosition($position) { |
|
66 | |||
67 | |||
68 | /** |
||
69 | * @return int |
||
70 | */ |
||
71 | 203 | public function getPosition() { |
|
74 | |||
75 | |||
76 | /** |
||
77 | * Strict validation of condition |
||
78 | * |
||
79 | * @param int|string|Strict $condition |
||
80 | * @return Token |
||
81 | */ |
||
82 | 140 | public function strict($condition) { |
|
86 | |||
87 | |||
88 | /** |
||
89 | * Check if token possible valid for our condition |
||
90 | * |
||
91 | * @param int|string|Possible $condition |
||
92 | * @return Token |
||
93 | */ |
||
94 | 63 | public function possible($condition) { |
|
98 | |||
99 | |||
100 | /** |
||
101 | * @param string $start |
||
102 | * @param string $end |
||
103 | * @return Collection |
||
104 | */ |
||
105 | 62 | public function section($start, $end) { |
|
106 | |||
107 | 62 | $token = $this->strict($start); |
|
108 | 62 | if (!$token->isValid()) { |
|
109 | # cant find start position |
||
110 | 62 | return new Collection(); |
|
111 | } |
||
112 | |||
113 | 53 | $this->moveToToken($token); |
|
114 | |||
115 | 53 | $section = new \Funivan\PhpTokenizer\Strategy\Section(); |
|
116 | 53 | $section->setDelimiters($start, $end); |
|
117 | 53 | $lastToken = $this->process($section); |
|
118 | 53 | if (!$lastToken->isValid()) { |
|
119 | return new Collection(); |
||
120 | } |
||
121 | |||
122 | 53 | return $this->collection->extractByTokens($token, $lastToken); |
|
123 | } |
||
124 | |||
125 | |||
126 | /** |
||
127 | * By default we search forward |
||
128 | * |
||
129 | * @param int|string|Search $condition |
||
130 | * @param null $direction |
||
131 | * @return Token |
||
132 | */ |
||
133 | 47 | public function search($condition, $direction = null) { |
|
134 | 47 | $strategy = Search::create(); |
|
135 | 47 | if ($direction !== null) { |
|
136 | 3 | $strategy->setDirection($direction); |
|
137 | 2 | } |
|
138 | 47 | $query = $this->buildStrategyCondition($condition, $strategy); |
|
139 | 47 | return $this->process($query); |
|
140 | } |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Relative move |
||
145 | * +10 move forward 10 tokens |
||
146 | * -5 move backward 5 tokens |
||
147 | * |
||
148 | * @param int $steps |
||
149 | * @return Token |
||
150 | */ |
||
151 | 3 | public function move($steps) { |
|
154 | |||
155 | |||
156 | /** |
||
157 | * Move to specific position |
||
158 | * |
||
159 | * @param Token $token |
||
160 | * @return Token|null |
||
161 | */ |
||
162 | 62 | public function moveToToken(Token $token) { |
|
182 | |||
183 | |||
184 | /** |
||
185 | * Array may contain Int, String or any StrategyInterface object |
||
186 | * |
||
187 | * @param array $conditions |
||
188 | * @return Collection |
||
189 | */ |
||
190 | 75 | public function sequence(array $conditions) { |
|
191 | 75 | $range = new Collection(); |
|
192 | 75 | foreach ($conditions as $value) { |
|
193 | 75 | $range[] = $this->checkFromSequence($value); |
|
194 | 50 | } |
|
195 | |||
196 | 75 | return $range; |
|
197 | } |
||
198 | |||
199 | |||
200 | /** |
||
201 | * @param string|int|StrategyInterface $value |
||
202 | * @return Token |
||
203 | */ |
||
204 | 75 | private function checkFromSequence($value) { |
|
205 | 75 | if ($value instanceof StrategyInterface) { |
|
206 | 51 | $query = $value; |
|
207 | 34 | } else { |
|
208 | 45 | $query = $this->buildStrategyCondition($value, Strict::create()); |
|
209 | } |
||
210 | |||
211 | 75 | $token = $this->process($query); |
|
212 | 75 | return $token; |
|
213 | } |
||
214 | |||
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | 203 | public function process(StrategyInterface $strategy) { |
|
220 | |||
221 | 203 | if ($this->isValid() === false) { |
|
222 | 149 | return new Token(); |
|
223 | } |
||
224 | |||
225 | 203 | $result = $strategy->process($this->collection, $this->getPosition()); |
|
226 | |||
227 | 203 | if ($result->isValid() === false) { |
|
228 | 173 | $this->setValid(false); |
|
229 | 173 | return new Token(); |
|
230 | } |
||
231 | |||
232 | 185 | $position = $result->getNexTokenIndex(); |
|
233 | 185 | $this->setPosition($position); |
|
234 | |||
235 | 185 | $token = $result->getToken(); |
|
236 | 185 | if ($token === null) { |
|
237 | 78 | $token = new Token(); |
|
238 | 52 | } |
|
239 | |||
240 | 185 | if ($this->skipWhitespaces and isset($this->collection[$position]) and $this->collection[$position]->getType() === T_WHITESPACE) { |
|
241 | # skip whitespaces in next check |
||
242 | 33 | $this->setPosition(($position + 1)); |
|
243 | 22 | } |
|
244 | |||
245 | 185 | return $token; |
|
246 | } |
||
247 | |||
248 | |||
249 | /** |
||
250 | * |
||
251 | * @param StrategyInterface|string|int $value |
||
252 | * @param QueryStrategy $defaultStrategy |
||
253 | * @return QueryStrategy |
||
254 | */ |
||
255 | 188 | private function buildStrategyCondition($value, QueryStrategy $defaultStrategy) { |
|
256 | |||
257 | 188 | if (is_object($value) and get_class($value) == get_class($defaultStrategy)) { |
|
258 | 12 | return $value; |
|
259 | } |
||
260 | |||
261 | 176 | $query = $defaultStrategy; |
|
262 | |||
263 | 176 | if (is_string($value) or $value === null) { |
|
264 | 143 | $query->valueIs($value); |
|
265 | 143 | return $query; |
|
266 | } |
||
267 | |||
268 | 131 | if (is_int($value)) { |
|
269 | 110 | $query->typeIs($value); |
|
270 | 110 | return $query; |
|
271 | } |
||
272 | |||
273 | |||
274 | 21 | throw new InvalidArgumentException("Invalid token condition. Expect string or int or StrategyInterface"); |
|
275 | } |
||
276 | |||
277 | |||
278 | /** |
||
279 | * @param boolean $valid |
||
280 | * @return $this |
||
281 | */ |
||
282 | 182 | public function setValid($valid) { |
|
283 | 182 | if (!is_bool($valid)) { |
|
284 | 3 | throw new InvalidArgumentException("Invalid flag. Expect boolean. Given:" . gettype($valid)); |
|
285 | } |
||
286 | 179 | $this->valid = $valid; |
|
287 | 179 | return $this; |
|
288 | } |
||
289 | |||
290 | |||
291 | /** |
||
292 | * Indicate state of all conditions |
||
293 | * |
||
294 | * @return bool |
||
295 | */ |
||
296 | 209 | public function isValid() { |
|
299 | |||
300 | |||
301 | /** |
||
302 | * @param boolean $skipWhitespaces |
||
303 | * @return $this |
||
304 | */ |
||
305 | 36 | public function setSkipWhitespaces($skipWhitespaces) { |
|
309 | |||
310 | } |