|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package GetSky\ParserExpressions |
|
4
|
|
|
* @author Alexander Getmanskii <[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 = 0; |
|
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
|
3 |
|
public function current() |
|
88
|
|
|
{ |
|
89
|
3 |
|
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 |
|
foreach ($this->children as $rule) { |
|
139
|
3 |
|
$array['children'][] = $rule->toArray(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
4 |
|
return $array; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Search for the first result by name. |
|
147
|
|
|
* |
|
148
|
|
|
* @param $ruleName |
|
149
|
|
|
* @return Result|null |
|
150
|
|
|
*/ |
|
151
|
4 |
|
public function find($ruleName) |
|
152
|
|
|
{ |
|
153
|
|
|
|
|
154
|
4 |
|
foreach ($this->children as $rule) { |
|
155
|
3 |
|
if ($rule->getName() == $ruleName) { |
|
156
|
2 |
|
return $rule; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
3 |
|
$child = $rule->find($ruleName); |
|
160
|
3 |
|
if ($child !== null) { |
|
161
|
3 |
|
return $child; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
4 |
|
return null; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Search results by name. |
|
170
|
|
|
* |
|
171
|
|
|
* @param $ruleName |
|
172
|
|
|
* @return Result|null |
|
173
|
|
|
*/ |
|
174
|
4 |
|
public function findAll($ruleName) |
|
175
|
|
|
{ |
|
176
|
|
|
|
|
177
|
4 |
|
$result = []; |
|
178
|
|
|
|
|
179
|
4 |
|
foreach ($this->children as $rule) { |
|
180
|
3 |
|
if ($rule->getName() == $ruleName) { |
|
181
|
2 |
|
$result[] = $rule; |
|
182
|
|
|
} else { |
|
183
|
3 |
|
$result = array_merge($result, $rule->findAll($ruleName)); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
4 |
|
return $result; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|