1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package GetSky\ParserExpressions |
4
|
|
|
* @author Alexander Getmansky <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace GetSky\ParserExpressions; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Result class is used to store the result of the parse. |
10
|
|
|
*/ |
11
|
|
|
class Result implements \Iterator, ResultInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string Result string |
16
|
|
|
*/ |
17
|
|
|
protected $name; |
18
|
|
|
/** |
19
|
|
|
* @var string Result string |
20
|
|
|
*/ |
21
|
|
|
protected $value; |
22
|
|
|
/** |
23
|
|
|
* @var integer Start position |
24
|
|
|
*/ |
25
|
|
|
protected $start; |
26
|
|
|
/** |
27
|
|
|
* @var integer Final position |
28
|
|
|
*/ |
29
|
|
|
protected $end; |
30
|
|
|
/** |
31
|
|
|
* @var Result[] |
32
|
|
|
*/ |
33
|
|
|
protected $children; |
34
|
|
|
/** |
35
|
|
|
* @var integer |
36
|
|
|
*/ |
37
|
|
|
private $key; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
*/ |
42
|
27 |
|
public function __construct($name) |
43
|
|
|
{ |
44
|
27 |
|
$this->name = $name; |
45
|
27 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
3 |
|
public function getValue() |
51
|
|
|
{ |
52
|
3 |
|
return $this->value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $value |
57
|
|
|
* @param integer $start |
58
|
|
|
*/ |
59
|
9 |
|
public function setValue($value, $start) |
60
|
|
|
{ |
61
|
9 |
|
$this->value = (string)$value; |
62
|
9 |
|
$this->start = $start; |
63
|
9 |
|
$this->end = $start + strlen($value); |
64
|
9 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
8 |
|
public function getName() |
70
|
|
|
{ |
71
|
8 |
|
return $this->name; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Added child result |
76
|
|
|
* |
77
|
|
|
* @param Result $child |
78
|
|
|
*/ |
79
|
17 |
|
public function addChild(Result $child) |
80
|
|
|
{ |
81
|
17 |
|
$this->children[] = $child; |
82
|
17 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return Result |
86
|
|
|
*/ |
87
|
4 |
|
public function current() |
88
|
|
|
{ |
89
|
4 |
|
return $this->children[$this->key]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
4 |
|
public function next() |
96
|
|
|
{ |
97
|
4 |
|
++$this->key; |
98
|
4 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
4 |
|
public function key() |
104
|
|
|
{ |
105
|
4 |
|
return $this->key; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
4 |
|
public function valid() |
112
|
|
|
{ |
113
|
4 |
|
return isset($this->children[$this->key]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
4 |
|
public function rewind() |
120
|
|
|
{ |
121
|
4 |
|
$this->key = 0; |
122
|
4 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Outstanding results in an array. |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
4 |
|
public function toArray() |
130
|
|
|
{ |
131
|
|
|
$array = [ |
132
|
4 |
|
'name' => $this->name, |
133
|
4 |
|
'value' => $this->value, |
134
|
4 |
|
'start' => $this->start, |
135
|
4 |
|
'end' => $this->end, |
136
|
|
|
]; |
137
|
|
|
|
138
|
4 |
|
if (!empty($this->children)) { |
139
|
3 |
|
foreach ($this->children as $rule) { |
140
|
3 |
|
$array['children'][] = $rule->toArray(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
4 |
|
return $array; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Search for the first result by name. |
149
|
|
|
* |
150
|
|
|
* @param $ruleName |
151
|
|
|
* @return Result|null |
152
|
|
|
*/ |
153
|
4 |
|
public function find($ruleName) |
154
|
|
|
{ |
155
|
4 |
|
if (empty($this->children)) { |
156
|
4 |
|
return null; |
157
|
|
|
} |
158
|
|
|
|
159
|
3 |
|
foreach ($this->children as $rule) { |
160
|
3 |
|
if ($rule->getName() == $ruleName) { |
161
|
2 |
|
return $rule; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
$child = $rule->find($ruleName); |
165
|
3 |
|
if ($child !== null) { |
166
|
3 |
|
return $child; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
return null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Search results by name. |
175
|
|
|
* |
176
|
|
|
* @param $ruleName |
177
|
|
|
* @return Result|null |
178
|
|
|
*/ |
179
|
4 |
|
public function findAll($ruleName) |
180
|
|
|
{ |
181
|
4 |
|
if (empty($this->children)) { |
182
|
4 |
|
return null; |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
$result = []; |
186
|
|
|
|
187
|
3 |
|
foreach ($this->children as $rule) { |
188
|
3 |
|
if ($rule->getName() == $ruleName) { |
189
|
2 |
|
$result[] = $rule; |
190
|
|
|
} |
191
|
|
|
|
192
|
3 |
|
$child = $rule->findAll($ruleName); |
193
|
3 |
|
if ($child !== null) { |
194
|
3 |
|
$result = array_merge($result, $child); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
3 |
|
return $result; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|