BaseNode::isRequired()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Node;
6
7
use Linio\Component\Input\Constraint\ConstraintInterface;
8
use Linio\Component\Input\Exception\InvalidConstraintException;
9
use Linio\Component\Input\Exception\RequiredFieldException;
10
use Linio\Component\Input\InputHandler;
11
use Linio\Component\Input\Instantiator\InstantiatorInterface;
12
use Linio\Component\Input\Transformer\TransformerInterface;
13
use Linio\Component\Input\TypeHandler;
14
15
class BaseNode
16
{
17
    /**
18
     * @var ConstraintInterface[]
19
     */
20
    protected $constraints = [];
21
22
    /**
23
     * @var TransformerInterface
24
     */
25
    protected $transformer;
26
27
    /**
28
     * @var InstantiatorInterface
29
     */
30
    protected $instantiator;
31
32
    /**
33
     * @var string
34
     */
35
    protected $type = 'array';
36
37
    /**
38
     * @var string
39
     */
40
    protected $typeAlias = 'array';
41
42
    /**
43
     * @var bool
44
     */
45
    protected $required = true;
46
47
    /**
48
     * @var mixed
49
     */
50
    protected $default;
51
52
    /**
53
     * @var BaseNode[]
54
     */
55
    protected $children = [];
56
57
    /**
58
     * @var TypeHandler
59
     */
60
    protected $typeHandler;
61
62
    /**
63
     * @var bool
64
     */
65
    protected $allowNull = false;
66
67
    public function setConstraints(array $constraints)
68
    {
69
        $this->constraints = $constraints;
70
    }
71
72
    public function addConstraint(ConstraintInterface $constraint)
73
    {
74
        $this->constraints[] = $constraint;
75
    }
76
77
    public function addConstraints(array $constraints)
78
    {
79
        $this->constraints = array_merge($this->constraints, $constraints);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->constraints, $constraints) of type array is incompatible with the declared type array<integer,object<Lin...t\ConstraintInterface>> of property $constraints.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
    }
81
82
    public function setTransformer(TransformerInterface $transformer)
83
    {
84
        $this->transformer = $transformer;
85
    }
86
87
    public function setInstantiator(InstantiatorInterface $instantiator)
88
    {
89
        $this->instantiator = $instantiator;
90
    }
91
92
    public function setTypeHandler(TypeHandler $typeHandler)
93
    {
94
        $this->typeHandler = $typeHandler;
95
    }
96
97
    public function setType(string $type)
98
    {
99
        $this->type = $type;
100
    }
101
102
    public function setTypeAlias(string $typeAlias)
103
    {
104
        $this->typeAlias = $typeAlias;
105
    }
106
107
    public function getTypeAlias(): string
108
    {
109
        return $this->typeAlias;
110
    }
111
112
    public function setRequired(bool $required)
113
    {
114
        $this->required = $required;
115
    }
116
117
    public function setDefault($default)
118
    {
119
        $this->default = $default;
120
    }
121
122
    public function setAllowNull(bool $allowNull)
123
    {
124
        $this->allowNull = $allowNull;
125
    }
126
127
    public function getDefault()
128
    {
129
        return $this->default;
130
    }
131
132
    public function hasDefault(): bool
133
    {
134
        return (bool) $this->default;
135
    }
136
137
    public function add(string $key, string $type, array $options = []): BaseNode
138
    {
139
        $child = $this->typeHandler->getType($type);
140
141
        if (isset($options['handler'])) {
142
            /** @var InputHandler $handler */
143
            $handler = $options['handler'];
144
            $handler->setRootType($type);
145
            $handler->define();
146
147
            $child = $handler->getRoot();
148
        }
149
150
        if (isset($options['required'])) {
151
            $child->setRequired($options['required']);
152
        }
153
154
        if (isset($options['default'])) {
155
            $child->setDefault($options['default']);
156
        }
157
158
        if (isset($options['instantiator'])) {
159
            $child->setInstantiator($options['instantiator']);
160
        }
161
162
        if (isset($options['transformer'])) {
163
            $child->setTransformer($options['transformer']);
164
        }
165
166
        if (isset($options['constraints'])) {
167
            $child->addConstraints($options['constraints']);
168
        }
169
170
        if (isset($options['allow_null'])) {
171
            $child->setAllowNull($options['allow_null']);
172
        }
173
174
        $this->children[$key] = $child;
175
176
        return $child;
177
    }
178
179
    public function remove(string $key)
180
    {
181
        unset($this->children[$key]);
182
    }
183
184
    /**
185
     * @return BaseNode[]
186
     */
187
    public function getChildren(): array
188
    {
189
        return $this->children;
190
    }
191
192
    public function hasChildren(): bool
193
    {
194
        return !empty($this->children);
195
    }
196
197
    public function isRequired(): bool
198
    {
199
        if ($this->hasDefault()) {
200
            return false;
201
        }
202
203
        return $this->required;
204
    }
205
206
    public function allowNull(): bool
0 ignored issues
show
Coding Style introduced by
function allowNull() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
207
    {
208
        return $this->allowNull;
209
    }
210
211
    public function getValue(string $field, $value)
212
    {
213
        if ($this->allowNull() && $value === null) {
214
            return $value;
215
        }
216
217
        $this->checkConstraints($field, $value);
218
219
        if ($this->transformer) {
220
            return $this->transformer->transform($value);
221
        }
222
223
        return $value;
224
    }
225
226
    public function walk($input)
227
    {
228
        $result = [];
229
230
        if (!$this->hasChildren()) {
231
            return $input;
232
        }
233
234 View Code Duplication
        foreach ($this->getChildren() as $field => $config) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
            if (!array_key_exists($field, $input)) {
236
                if ($config->isRequired()) {
237
                    throw new RequiredFieldException($field);
238
                }
239
240
                if (!$config->hasDefault()) {
241
                    continue;
242
                }
243
244
                $input[$field] = $config->getDefault();
245
            }
246
247
            $result[$field] = $config->getValue($field, $config->walk($input[$field]));
248
        }
249
250
        return $result;
251
    }
252
253
    protected function checkConstraints(string $field, $value)
254
    {
255
        foreach ($this->constraints as $constraint) {
256
            if (!$constraint->validate($value)) {
257
                throw new InvalidConstraintException($constraint->getErrorMessage($field));
258
            }
259
        }
260
    }
261
}
262