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

Rule::getDefaultId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
/**
42
 * Class \JMS\Serializer\Type\Compiler\Llk\Rule.
43
 *
44
 * Rule parent.
45
 *
46
 * @copyright  Copyright © 2007-2017 Hoa community
47
 * @license    New BSD License
48
 */
49
abstract class Rule
50
{
51
    /**
52
     * Rule name.
53
     *
54
     * @var string
55
     */
56
    protected $_name           = null;
57
58
    /**
59
     * Rule's children. Can be an array of names or a single name.
60
     *
61
     * @var mixed
62
     */
63
    protected $_children       = null;
64
65
    /**
66
     * Node ID.
67
     *
68
     * @var string
69
     */
70
    protected $_nodeId         = null;
71
72
    /**
73
     * Node options.
74
     *
75
     * @var array
76
     */
77
    protected $_nodeOptions    = [];
78
79
    /**
80
     * Default ID.
81
     *
82
     * @var string
83
     */
84
    protected $_defaultId      = null;
85
86
    /**
87
     * Default options.
88
     *
89
     * @var array
90
     */
91
    protected $_defaultOptions = [];
92
93
    /**
94
     * For non-transitional rule: PP representation.
95
     *
96
     * @var string
97
     */
98
    protected $_pp             = null;
99
100
    /**
101
     * Whether the rule is transitional or not (i.e. not declared in the grammar
102
     * but created by the analyzer).
103
     *
104
     * @var bool
105
     */
106
    protected $_transitional   = true;
107
108
109
110
    /**
111
     * Constructor.
112
     *
113
     * @param   string|int  $name        Rule name.
114
     * @param   mixed   $children    Children.
115
     * @param   string  $nodeId      Node ID.
116
     */
117
    public function __construct($name, $children, $nodeId = null)
118
    {
119
        $this->setName($name);
120
        $this->setChildren($children);
121
        $this->setNodeId($nodeId);
122
123
        return;
124
    }
125
126
    /**
127
     * Set rule name.
128
     *
129
     * @param   string  $name    Rule name.
130
     * @return  string
131
     */
132
    public function setName($name)
133
    {
134
        $old         = $this->_name;
135
        $this->_name = $name;
136
137
        return $old;
138
    }
139
140
    /**
141
     * Get rule name.
142
     *
143
     * @return  string
144
     */
145
    public function getName()
146
    {
147
        return $this->_name;
148
    }
149
150
    /**
151
     * Set rule's children.
152
     *
153
     * @param   mixed  $children    Children.
154
     * @return  mixed
155
     */
156
    protected function setChildren($children)
157
    {
158
        $old             = $this->_children;
159
        $this->_children = $children;
160
161
        return $old;
162
    }
163
164
    /**
165
     * Get rule's children.
166
     *
167
     * @return  mixed
168
     */
169
    public function getChildren()
170
    {
171
        return $this->_children;
172
    }
173
174
    /**
175
     * Set node ID.
176
     *
177
     * @param   string  $nodeId    Node ID.
178
     * @return  string
179
     */
180
    public function setNodeId($nodeId)
181
    {
182
        $old = $this->_nodeId;
183
184
        if (false !== $pos = strpos($nodeId, ':')) {
185
            $this->_nodeId      = substr($nodeId, 0, $pos);
186
            $this->_nodeOptions = str_split(substr($nodeId, $pos + 1));
187
        } else {
188
            $this->_nodeId      = $nodeId;
189
            $this->_nodeOptions = [];
190
        }
191
192
        return $old;
193
    }
194
195
    /**
196
     * Get node ID.
197
     *
198
     * @return  string
199
     */
200
    public function getNodeId()
201
    {
202
        return $this->_nodeId;
203
    }
204
205
    /**
206
     * Get node options.
207
     *
208
     * @return array
209
     */
210
    public function getNodeOptions(): array
211
    {
212
        return $this->_nodeOptions;
213
    }
214
215
    /**
216
     * Set default ID.
217
     *
218
     * @param   string  $defaultId    Default ID.
219
     * @return  string
220
     */
221
    public function setDefaultId($defaultId)
222
    {
223
        $old = $this->_defaultId;
224
225
        if (false !== $pos = strpos($defaultId, ':')) {
226
            $this->_defaultId      = substr($defaultId, 0, $pos);
227
            $this->_defaultOptions = str_split(substr($defaultId, $pos + 1));
228
        } else {
229
            $this->_defaultId      = $defaultId;
230
            $this->_defaultOptions = [];
231
        }
232
233
        return $old;
234
    }
235
236
    /**
237
     * Get default ID.
238
     *
239
     * @return  string
240
     */
241
    public function getDefaultId()
242
    {
243
        return $this->_defaultId;
244
    }
245
246
    /**
247
     * Get default options.
248
     *
249
     * @return  array
250
     */
251
    public function getDefaultOptions()
252
    {
253
        return $this->_defaultOptions;
254
    }
255
256
    /**
257
     * Set PP representation of the rule.
258
     *
259
     * @param   string  $pp    PP representation.
260
     * @return  string
261
     */
262
    public function setPPRepresentation($pp)
263
    {
264
        $old                 = $this->_pp;
265
        $this->_pp           = $pp;
266
        $this->_transitional = false;
267
268
        return $old;
269
    }
270
271
    /**
272
     * Get PP representation of the rule.
273
     *
274
     * @return  string
275
     */
276
    public function getPPRepresentation()
277
    {
278
        return $this->_pp;
279
    }
280
281
    /**
282
     * Check whether the rule is transitional or not.
283
     *
284
     * @return  bool
285
     */
286
    public function isTransitional()
287
    {
288
        return $this->_transitional;
289
    }
290
}
291