1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Funivan\PhpTokenizer\Strategy; |
6
|
|
|
|
7
|
|
|
use Funivan\PhpTokenizer\Exception\InvalidArgumentException; |
8
|
|
|
use Funivan\PhpTokenizer\Query\Query; |
9
|
|
|
use Funivan\PhpTokenizer\Query\QueryInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
class Section extends QueryStrategy { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var QueryInterface |
19
|
|
|
*/ |
20
|
|
|
private $startQuery; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var QueryInterface |
24
|
|
|
*/ |
25
|
|
|
private $endQuery; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $start |
30
|
|
|
* @param string $end |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
174 |
|
public function setDelimiters($start, $end) { |
34
|
174 |
|
$startQuery = new Query(); |
35
|
174 |
|
$startQuery->valueIs($start); |
36
|
174 |
|
$this->setStartQuery($startQuery); |
37
|
|
|
|
38
|
174 |
|
$endQuery = new Query(); |
39
|
174 |
|
$endQuery->valueIs($end); |
40
|
174 |
|
$this->setEndQuery($endQuery); |
41
|
|
|
|
42
|
174 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
180 |
|
public function process(\Funivan\PhpTokenizer\Collection $collection, $currentIndex) { |
50
|
|
|
|
51
|
180 |
|
$this->requireQueries(); |
52
|
|
|
|
53
|
174 |
|
$result = new StrategyResult(); |
54
|
174 |
|
$token = $collection->offsetGet($currentIndex); |
55
|
174 |
|
if (empty($token) or $this->startQuery->isValid($token) === false) { |
56
|
3 |
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
171 |
|
$blockEndFlag = null; |
60
|
171 |
|
$startIndex = null; |
61
|
171 |
|
$endIndex = null; |
62
|
171 |
|
foreach ($collection as $tokenIndex => $token) { |
63
|
171 |
|
if ($tokenIndex < $currentIndex) { |
64
|
171 |
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
171 |
|
if ($this->startQuery->isValid($token)) { |
68
|
171 |
|
$blockEndFlag++; |
69
|
171 |
|
if ($blockEndFlag === 1) { |
70
|
171 |
|
$startIndex = $tokenIndex; |
71
|
|
|
} |
72
|
|
|
|
73
|
171 |
|
} elseif ($startIndex !== null and $this->endQuery->isValid($token)) { |
74
|
171 |
|
$blockEndFlag--; |
75
|
|
|
} |
76
|
|
|
|
77
|
171 |
|
if ($blockEndFlag === 0) { |
78
|
171 |
|
$endIndex = $tokenIndex; |
79
|
171 |
|
break; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
171 |
|
if ($startIndex !== null and $endIndex !== null) { |
84
|
171 |
|
$result = new StrategyResult(); |
85
|
171 |
|
$result->setValid(true); |
86
|
171 |
|
$result->setNexTokenIndex(++$endIndex); |
87
|
171 |
|
$result->setToken($token); |
88
|
|
|
} |
89
|
|
|
|
90
|
171 |
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param QueryInterface $startQuery |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
177 |
|
public function setStartQuery(QueryInterface $startQuery) { |
99
|
177 |
|
$this->startQuery = $startQuery; |
100
|
177 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param QueryInterface $endQuery |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
174 |
|
public function setEndQuery(QueryInterface $endQuery) { |
109
|
174 |
|
$this->endQuery = $endQuery; |
110
|
174 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
180 |
|
protected function requireQueries() { |
115
|
180 |
|
if (empty($this->startQuery)) { |
116
|
3 |
|
throw new InvalidArgumentException('Empty start Query. '); |
117
|
|
|
} |
118
|
|
|
|
119
|
177 |
|
if (empty($this->endQuery)) { |
120
|
3 |
|
throw new InvalidArgumentException('Empty end Query. '); |
121
|
|
|
} |
122
|
174 |
|
} |
123
|
|
|
|
124
|
|
|
} |