|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Hoa |
|
7
|
|
|
* |
|
8
|
|
|
* |
|
9
|
|
|
* |
|
10
|
|
|
* |
|
11
|
|
|
* BSD 3-Clause License |
|
12
|
|
|
* |
|
13
|
|
|
* Copyright © 2007-2017, Hoa community. All rights reserved. |
|
14
|
|
|
* |
|
15
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
16
|
|
|
* modification, are permitted provided that the following conditions are met: |
|
17
|
|
|
* |
|
18
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this |
|
19
|
|
|
* list of conditions and the following disclaimer. |
|
20
|
|
|
* |
|
21
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
22
|
|
|
* this list of conditions and the following disclaimer in the documentation |
|
23
|
|
|
* and/or other materials provided with the distribution. |
|
24
|
|
|
* |
|
25
|
|
|
* 3. Neither the name of the copyright holder nor the names of its |
|
26
|
|
|
* contributors may be used to endorse or promote products derived from |
|
27
|
|
|
* this software without specific prior written permission. |
|
28
|
|
|
* |
|
29
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
30
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
31
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
32
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
33
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
34
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
35
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
36
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
37
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
38
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
namespace JMS\Serializer\Type\Compiler\Llk; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Class \JMS\Serializer\Type\Compiler\Llk\Rule. |
|
45
|
|
|
* |
|
46
|
|
|
* Rule parent. |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract class Rule |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* Rule name. |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $_name = null; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Rule's children. Can be an array of names or a single name. |
|
59
|
|
|
* |
|
60
|
|
|
* @var mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $_children = null; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Node ID. |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $_nodeId = null; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Node options. |
|
73
|
|
|
* |
|
74
|
|
|
* @var array |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $_nodeOptions = []; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Default ID. |
|
80
|
|
|
* |
|
81
|
|
|
* @var string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $_defaultId = null; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Default options. |
|
87
|
|
|
* |
|
88
|
|
|
* @var array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $_defaultOptions = []; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* For non-transitional rule: PP representation. |
|
94
|
|
|
* |
|
95
|
|
|
* @var string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected $_pp = null; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Whether the rule is transitional or not (i.e. not declared in the grammar |
|
101
|
|
|
* but created by the analyzer). |
|
102
|
|
|
* |
|
103
|
|
|
* @var bool |
|
104
|
|
|
*/ |
|
105
|
|
|
protected $_transitional = true; |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string|int $name Rule name. |
|
111
|
|
|
* @param mixed $children Children. |
|
112
|
|
|
* @param string $nodeId Node ID. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function __construct($name, $children, $nodeId = null) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->setName($name); |
|
117
|
|
|
$this->setChildren($children); |
|
118
|
|
|
$this->setNodeId($nodeId); |
|
119
|
|
|
|
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set rule name. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string|int|null $name Rule name. |
|
127
|
|
|
* |
|
128
|
|
|
* @return string|int|null |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setName($name) |
|
131
|
|
|
{ |
|
132
|
|
|
$old = $this->_name; |
|
133
|
|
|
$this->_name = $name; |
|
134
|
|
|
|
|
135
|
|
|
return $old; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get rule name. |
|
140
|
|
|
* |
|
141
|
|
|
* @return string|int|null |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getName() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->_name; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Set rule's children. |
|
150
|
|
|
* |
|
151
|
|
|
* @param mixed $children Children. |
|
152
|
|
|
* |
|
153
|
|
|
* @return mixed |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function setChildren($children) |
|
156
|
|
|
{ |
|
157
|
|
|
$old = $this->_children; |
|
158
|
|
|
$this->_children = $children; |
|
159
|
|
|
|
|
160
|
|
|
return $old; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get rule's children. |
|
165
|
|
|
* |
|
166
|
|
|
* @return mixed |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getChildren() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->_children; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Set node ID. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $nodeId Node ID. |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setNodeId(?string $nodeId): ?string |
|
179
|
|
|
{ |
|
180
|
|
|
$old = $this->_nodeId; |
|
181
|
|
|
|
|
182
|
|
|
if (null !== $nodeId && false !== $pos = strpos($nodeId, ':')) { |
|
183
|
|
|
$this->_nodeId = substr($nodeId, 0, $pos); |
|
184
|
|
|
$this->_nodeOptions = str_split(substr($nodeId, $pos + 1)); |
|
185
|
|
|
} else { |
|
186
|
|
|
$this->_nodeId = $nodeId; |
|
187
|
|
|
$this->_nodeOptions = []; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $old; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Get node ID. |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getNodeId(): ?string |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->_nodeId; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get node options. |
|
203
|
|
|
* |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getNodeOptions(): array |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->_nodeOptions; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Set default ID. |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $defaultId Default ID. |
|
215
|
|
|
*/ |
|
216
|
|
|
public function setDefaultId(string $defaultId): ?string |
|
217
|
|
|
{ |
|
218
|
|
|
$old = $this->_defaultId; |
|
219
|
|
|
|
|
220
|
|
|
if (false !== $pos = strpos($defaultId, ':')) { |
|
221
|
|
|
$this->_defaultId = substr($defaultId, 0, $pos); |
|
222
|
|
|
$this->_defaultOptions = str_split(substr($defaultId, $pos + 1)); |
|
223
|
|
|
} else { |
|
224
|
|
|
$this->_defaultId = $defaultId; |
|
225
|
|
|
$this->_defaultOptions = []; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $old; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Get default ID. |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getDefaultId(): ?string |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->_defaultId; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Get default options. |
|
241
|
|
|
* |
|
242
|
|
|
* @return array |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getDefaultOptions(): array |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->_defaultOptions; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Set PP representation of the rule. |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $pp PP representation. |
|
253
|
|
|
*/ |
|
254
|
|
|
public function setPPRepresentation(string $pp): ?string |
|
255
|
|
|
{ |
|
256
|
|
|
$old = $this->_pp; |
|
257
|
|
|
$this->_pp = $pp; |
|
258
|
|
|
$this->_transitional = false; |
|
259
|
|
|
|
|
260
|
|
|
return $old; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Get PP representation of the rule. |
|
265
|
|
|
*/ |
|
266
|
|
|
public function getPPRepresentation(): string |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->_pp; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Check whether the rule is transitional or not. |
|
273
|
|
|
*/ |
|
274
|
|
|
public function isTransitional(): bool |
|
275
|
|
|
{ |
|
276
|
|
|
return $this->_transitional; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|