Completed
Pull Request — master (#1184)
by Alexey
12:56
created

TreeNode   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 310
rs 10
wmc 27

21 Methods

Rating   Name   Duplication   Size   Complexity  
A appendChild() 0 5 1
A setId() 0 6 1
A __destruct() 0 5 1
A getChildren() 0 3 1
A setValue() 0 6 1
A childExists() 0 3 1
A setChildren() 0 6 1
A getValue() 0 3 1
A getChildrenNumber() 0 3 1
A getValueToken() 0 6 2
A getData() 0 3 1
A getParent() 0 3 1
A setParent() 0 6 1
A accept() 0 6 1
A prependChild() 0 5 1
A getChild() 0 6 2
A isToken() 0 3 1
A getValueValue() 0 6 2
A __construct() 0 19 3
A getOffset() 0 6 2
A getId() 0 3 1
1
<?php
2
/**
3
 * Hoa
4
 *
5
 *
6
 * @license
7
 *
8
 * BSD 3-Clause License
9
 *
10
 * Copyright © 2007-2017, Hoa community. All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions are met:
14
 *
15
 * 1. Redistributions of source code must retain the above copyright notice, this
16
 *    list of conditions and the following disclaimer.
17
 *
18
 * 2. Redistributions in binary form must reproduce the above copyright notice,
19
 *    this list of conditions and the following disclaimer in the documentation
20
 *    and/or other materials provided with the distribution.
21
 *
22
 * 3. Neither the name of the copyright holder nor the names of its
23
 *    contributors may be used to endorse or promote products derived from
24
 *    this software without specific prior written permission.
25
 *
26
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 */
38
39
namespace JMS\Serializer\Type\Compiler\Llk;
40
41
use Hoa\Visitor;
42
43
/**
44
 * Class \JMS\Serializer\Type\Compiler\Llk\TreeNode.
45
 *
46
 * Provide a generic node for the AST produced by LL(k) parser.
47
 *
48
 * @copyright  Copyright © 2007-2017 Hoa community
49
 * @license    New BSD License
50
 */
51
final class TreeNode implements Visitor\Element
52
{
53
    /**
54
     * ID (should be something like #ruleName or token).
55
     *
56
     * @var string
57
     */
58
    protected $_id       = '';
59
60
    /**
61
     * Value of the node (non-null for token nodes).
62
     *
63
     * @var array|null
64
     */
65
    protected $_value    = null;
66
67
    /**
68
     * Children.
69
     *
70
     * @var array
71
     */
72
    protected $_children = [];
73
74
    /**
75
     * Parent.
76
     *
77
     * @var \JMS\Serializer\Type\Compiler\Llk\TreeNode|null
78
     */
79
    protected $_parent = null;
80
81
    /**
82
     * Attached data.
83
     *
84
     * @var array
85
     */
86
    protected $_data     = [];
87
88
89
90
    /**
91
     * Constructor.
92
     *
93
     * @param   string                      $id          ID.
94
     * @param   array                       $value       Value.
95
     * @param   array                       $children    Children.
96
     * @param   \JMS\Serializer\Type\Compiler\Llk\TreeNode  $parent    Parent.
97
     */
98
    public function __construct(
99
        string $id,
100
        array $value    = null,
101
        array $children = [],
102
        self  $parent   = null
103
    ) {
104
        $this->setId($id);
105
106
        if (!empty($value)) {
107
            $this->setValue($value);
108
        }
109
110
        $this->setChildren($children);
111
112
        if (null !== $parent) {
113
            $this->setParent($parent);
114
        }
115
116
        return;
117
    }
118
119
    /**
120
     * Set ID.
121
     *
122
     * @param   string  $id    ID.
123
     * @return  string
124
     */
125
    public function setId(string $id)
126
    {
127
        $old       = $this->_id;
128
        $this->_id = $id;
129
130
        return $old;
131
    }
132
133
    /**
134
     * Get ID.
135
     *
136
     * @return  string
137
     */
138
    public function getId()
139
    {
140
        return $this->_id;
141
    }
142
143
    /**
144
     * Set value.
145
     *
146
     * @param   array  $value    Value (token & value).
147
     * @return  array|null
148
     */
149
    public function setValue(array $value)
150
    {
151
        $old          = $this->_value;
152
        $this->_value = $value;
153
154
        return $old;
155
    }
156
157
    /**
158
     * Get value.
159
     *
160
     * @return  array|null
161
     */
162
    public function getValue()
163
    {
164
        return $this->_value;
165
    }
166
167
    /**
168
     * Get value token.
169
     *
170
     * @return  string
171
     */
172
    public function getValueToken()
173
    {
174
        return
175
            isset($this->_value['token'])
176
                ? $this->_value['token']
177
                : null;
178
    }
179
180
    /**
181
     * Get value value.
182
     *
183
     * @return  string
184
     */
185
    public function getValueValue()
186
    {
187
        return
188
            isset($this->_value['value'])
189
                ? $this->_value['value']
190
                : null;
191
    }
192
193
    /**
194
     * Get token offset.
195
     *
196
     * @return  int
197
     */
198
    public function getOffset()
199
    {
200
        return
201
            isset($this->_value['offset'])
202
                ? $this->_value['offset']
203
                : 0;
204
    }
205
206
    /**
207
     * Check if the node represents a token or not.
208
     *
209
     * @return  bool
210
     */
211
    public function isToken()
212
    {
213
        return !empty($this->_value);
214
    }
215
216
    /**
217
     * Prepend a child.
218
     *
219
     * @param   \JMS\Serializer\Type\Compiler\Llk\TreeNode  $child    Child.
220
     * @return  \JMS\Serializer\Type\Compiler\Llk\TreeNode
221
     */
222
    public function prependChild(self $child)
223
    {
224
        array_unshift($this->_children, $child);
225
226
        return $this;
227
    }
228
229
    /**
230
     * Append a child.
231
     *
232
     * @param   \JMS\Serializer\Type\Compiler\Llk\TreeNode  $child    Child.
233
     * @return  \JMS\Serializer\Type\Compiler\Llk\TreeNode
234
     */
235
    public function appendChild(self $child)
236
    {
237
        $this->_children[] = $child;
238
239
        return $this;
240
    }
241
242
    /**
243
     * Set children.
244
     *
245
     * @param   array  $children    Children.
246
     * @return  array
247
     */
248
    public function setChildren(array $children)
249
    {
250
        $old             = $this->_children;
251
        $this->_children = $children;
252
253
        return $old;
254
    }
255
256
    /**
257
     * Get child.
258
     *
259
     * @param   int  $i    Index.
260
     * @return  \JMS\Serializer\Type\Compiler\Llk\TreeNode
261
     */
262
    public function getChild($i)
263
    {
264
        return
265
            true === $this->childExists($i)
266
                ? $this->_children[$i]
267
                : null;
268
    }
269
270
    /**
271
     * Get children.
272
     *
273
     * @return  array
274
     */
275
    public function getChildren()
276
    {
277
        return $this->_children;
278
    }
279
280
    /**
281
     * Get number of children.
282
     *
283
     * @return  int
284
     */
285
    public function getChildrenNumber()
286
    {
287
        return count($this->_children);
288
    }
289
290
    /**
291
     * Check if a child exists.
292
     *
293
     * @param   int  $i    Index.
294
     * @return  bool
295
     */
296
    public function childExists($i)
297
    {
298
        return array_key_exists($i, $this->_children);
299
    }
300
301
    /**
302
     * Set parent.
303
     *
304
     * @param   \JMS\Serializer\Type\Compiler\Llk\TreeNode  $parent    Parent.
305
     * @return  \JMS\Serializer\Type\Compiler\Llk\TreeNode|null
306
     */
307
    public function setParent(self $parent)
308
    {
309
        $old           = $this->_parent;
310
        $this->_parent = $parent;
311
312
        return $old;
313
    }
314
315
    /**
316
     * Get parent.
317
     *
318
     * @return  \JMS\Serializer\Type\Compiler\Llk\TreeNode|null
319
     */
320
    public function getParent()
321
    {
322
        return $this->_parent;
323
    }
324
325
    /**
326
     * Get data.
327
     *
328
     * @return  array
329
     */
330
    public function &getData()
331
    {
332
        return $this->_data;
333
    }
334
335
    /**
336
     * Accept a visitor.
337
     *
338
     * @param   \Hoa\Visitor\Visit  $visitor    Visitor.
339
     * @param   mixed               &$handle    Handle (reference).
340
     * @param   mixed               $eldnah     Handle (no reference).
341
     * @return  mixed
342
     */
343
    public function accept(
344
        Visitor\Visit $visitor,
345
        &$handle = null,
346
        $eldnah  = null
347
    ) {
348
        return $visitor->visit($this, $handle, $eldnah);
349
    }
350
351
    /**
352
     * Remove circular reference to the parent (help the garbage collector).
353
     *
354
     * @return  void
355
     */
356
    public function __destruct()
357
    {
358
        unset($this->_parent);
359
360
        return;
361
    }
362
}
363