AbstractNode::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the Borobudur-Config package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Config\Definition;
12
13
use Borobudur\Config\Exception\InvalidConfigurationException;
14
use Borobudur\Config\Exception\InvalidTypeException;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/10/15
19
 */
20
abstract class AbstractNode implements NodeInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $name;
26
27
    /**
28
     * @var string
29
     */
30
    protected $path;
31
32
    /**
33
     * @var NodeInterface
34
     */
35
    protected $parent;
36
37
    /**
38
     * @var mixed
39
     */
40
    protected $defaultValue;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $defaultValueSet;
46
47
    /**
48
     * @var bool
49
     */
50
    protected $required;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $allowOverwrite;
56
57
    /**
58
     * Constructor.
59
     *
60
     * @param string $name
61
     * @param NodeInterface|null $parent
62
     */
63
    public function __construct($name, NodeInterface $parent = null)
64
    {
65
        $this->name = $name;
66
        $this->parent = $parent;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getName()
73
    {
74
        return $this->name;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function setPath($path)
81
    {
82
        $this->path = $path;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getPath()
89
    {
90
        $path = $this->name;
91
92
        if (null !== $this->parent) {
93
            $path = $this->parent->getPath().'.'.$path;
94
        }
95
96
        return $path;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function setParent(NodeInterface $parent)
103
    {
104
        $this->parent = $parent;
105
106
        return $this;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getParent()
113
    {
114
        return $this->parent;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function setAllowOverwrite($allowOverwrite)
121
    {
122
        $this->allowOverwrite = $allowOverwrite;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function setDefaultValue($value)
129
    {
130
        $this->defaultValue = $value;
131
        $this->defaultValueSet = true;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getDefaultValue()
138
    {
139
        return $this->defaultValue;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function setRequired($required)
146
    {
147
        $this->required = $required;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function isRequired()
154
    {
155
        return $this->required;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function hasDefaultValue()
162
    {
163
        return null !== $this->defaultValue;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    final public function normalize($value)
170
    {
171
        $this->validateType($value);
172
173
        return $this->normalizeValue($value);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    final public function merge($left, $right)
180
    {
181
        if (false === $this->allowOverwrite) {
182
            throw new InvalidConfigurationException(sprintf(
183
                'Configuration path "%s" cannot be overwritten.',
184
                $this->getPath()
185
            ));
186
        }
187
188
        $this->validateType($left);
189
        $this->validateType($right);
190
191
        return $this->mergeValues($left, $right);
192
    }
193
194
    /**
195
     * Finalize and validate type and value.
196
     *
197
     * @param mixed $value
198
     *
199
     * @return mixed
200
     */
201
    final public function finalize($value)
202
    {
203
        $this->validateType($value);
204
205
        return $this->finalizeValue($value);
206
    }
207
208
    /**
209
     * Validate type of a Node.
210
     *
211
     * @param mixed $value
212
     *
213
     * @throws InvalidTypeException
214
     */
215
    abstract protected function validateType($value);
216
217
    /**
218
     * Normalize value.
219
     *
220
     * @param mixed $value
221
     *
222
     * @return mixed
223
     */
224
    abstract protected function normalizeValue($value);
225
226
    /**
227
     * Merge between two elements.
228
     *
229
     * @param mixed $left
230
     * @param mixed $right
231
     *
232
     * @return mixed
233
     */
234
    abstract protected function mergeValues($left, $right);
235
236
    /**
237
     * Finalize and validate value.
238
     *
239
     * @param mixed $value
240
     */
241
    abstract protected function finalizeValue($value);
242
}
243