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
|
|
|
public function setName($name) |
129
|
|
|
{ |
130
|
|
|
$old = $this->_name; |
131
|
|
|
$this->_name = $name; |
132
|
|
|
|
133
|
|
|
return $old; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get rule name. |
138
|
|
|
* |
139
|
|
|
* @return string|int|null |
140
|
|
|
*/ |
141
|
|
|
public function getName() |
142
|
|
|
{ |
143
|
|
|
return $this->_name; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set rule's children. |
148
|
|
|
* |
149
|
|
|
* @param mixed $children Children. |
150
|
|
|
* |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
protected function setChildren($children) |
154
|
|
|
{ |
155
|
|
|
$old = $this->_children; |
156
|
|
|
$this->_children = $children; |
157
|
|
|
|
158
|
|
|
return $old; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get rule's children. |
163
|
|
|
* |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
public function getChildren() |
167
|
|
|
{ |
168
|
|
|
return $this->_children; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set node ID. |
173
|
|
|
* |
174
|
|
|
* @param string $nodeId Node ID. |
175
|
|
|
*/ |
176
|
|
|
public function setNodeId(?string $nodeId): ?string |
177
|
|
|
{ |
178
|
|
|
$old = $this->_nodeId; |
179
|
|
|
|
180
|
|
|
if (null !== $nodeId && false !== $pos = strpos($nodeId, ':')) { |
181
|
|
|
$this->_nodeId = substr($nodeId, 0, $pos); |
182
|
|
|
$this->_nodeOptions = str_split(substr($nodeId, $pos + 1)); |
183
|
|
|
} else { |
184
|
|
|
$this->_nodeId = $nodeId; |
185
|
|
|
$this->_nodeOptions = []; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $old; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get node ID. |
193
|
|
|
*/ |
194
|
|
|
public function getNodeId(): ?string |
195
|
|
|
{ |
196
|
|
|
return $this->_nodeId; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get node options. |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function getNodeOptions(): array |
205
|
|
|
{ |
206
|
|
|
return $this->_nodeOptions; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Set default ID. |
211
|
|
|
* |
212
|
|
|
* @param string $defaultId Default ID. |
213
|
|
|
*/ |
214
|
|
|
public function setDefaultId(string $defaultId): ?string |
215
|
|
|
{ |
216
|
|
|
$old = $this->_defaultId; |
217
|
|
|
|
218
|
|
|
if (false !== $pos = strpos($defaultId, ':')) { |
219
|
|
|
$this->_defaultId = substr($defaultId, 0, $pos); |
220
|
|
|
$this->_defaultOptions = str_split(substr($defaultId, $pos + 1)); |
221
|
|
|
} else { |
222
|
|
|
$this->_defaultId = $defaultId; |
223
|
|
|
$this->_defaultOptions = []; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $old; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get default ID. |
231
|
|
|
*/ |
232
|
|
|
public function getDefaultId(): ?string |
233
|
|
|
{ |
234
|
|
|
return $this->_defaultId; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get default options. |
239
|
|
|
* |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
public function getDefaultOptions(): array |
243
|
|
|
{ |
244
|
|
|
return $this->_defaultOptions; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Set PP representation of the rule. |
249
|
|
|
* |
250
|
|
|
* @param string $pp PP representation. |
251
|
|
|
*/ |
252
|
|
|
public function setPPRepresentation(string $pp): ?string |
253
|
|
|
{ |
254
|
|
|
$old = $this->_pp; |
255
|
|
|
$this->_pp = $pp; |
256
|
|
|
$this->_transitional = false; |
257
|
|
|
|
258
|
|
|
return $old; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get PP representation of the rule. |
263
|
|
|
*/ |
264
|
|
|
public function getPPRepresentation(): string |
265
|
|
|
{ |
266
|
|
|
return $this->_pp; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Check whether the rule is transitional or not. |
271
|
|
|
*/ |
272
|
|
|
public function isTransitional(): bool |
273
|
|
|
{ |
274
|
|
|
return $this->_transitional; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|