Completed
Push — master ( 54a589...ba00a5 )
by Klaus
07:44
created

BaseNode::setType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
4
namespace Linio\Component\Input\Node;
5
6
use Linio\Component\Input\Constraint\ConstraintInterface;
7
use Linio\Component\Input\Exception\InvalidConstraintException;
8
use Linio\Component\Input\Exception\RequiredFieldException;
9
use Linio\Component\Input\Instantiator\InstantiatorInterface;
10
use Linio\Component\Input\Transformer\TransformerInterface;
11
use Linio\Component\Input\TypeHandler;
12
13
class BaseNode
14
{
15
    /**
16
     * @var ConstraintInterface[]
17
     */
18
    protected $constraints = [];
19
20
    /**
21
     * @var TransformerInterface
22
     */
23
    protected $transformer;
24
25
    /**
26
     * @var InstantiatorInterface
27
     */
28
    protected $instantiator;
29
30
    /**
31
     * @var string
32
     */
33
    protected $type = 'array';
34
35
    /**
36
     * @var bool
37
     */
38
    protected $required = true;
39
40
    /**
41
     * @var mixed
42
     */
43
    protected $default;
44
45
    /**
46
     * @var BaseNode[]
47
     */
48
    protected $children = [];
49
50
    /**
51
     * @var TypeHandler
52
     */
53
    protected $typeHandler;
54
55
    public function setConstraints(array $constraints)
56
    {
57
        $this->constraints = $constraints;
58
    }
59
60
    public function addConstraint(ConstraintInterface $constraint)
61
    {
62
        $this->constraints[] = $constraint;
63
    }
64
65
    public function setTransformer(TransformerInterface $transformer)
66
    {
67
        $this->transformer = $transformer;
68
    }
69
70
    public function setInstantiator(InstantiatorInterface $instantiator)
71
    {
72
        $this->instantiator = $instantiator;
73
    }
74
75
    public function setTypeHandler(TypeHandler $typeHandler)
76
    {
77
        $this->typeHandler = $typeHandler;
78
    }
79
80
    public function setType(string $type)
81
    {
82
        $this->type = $type;
83
    }
84
85
    public function setRequired(bool $required)
86
    {
87
        $this->required = $required;
88
    }
89
90
    public function setDefault($default)
91
    {
92
        $this->default = $default;
93
    }
94
95
    public function getDefault()
96
    {
97
        return $this->default;
98
    }
99
100
    public function hasDefault(): bool
101
    {
102
        return (bool) $this->default;
103
    }
104
105
    public function add(string $key, string $type, array $options = []): BaseNode
106
    {
107
        $child = $this->typeHandler->getType($type);
108
        $child->setTypeHandler($this->typeHandler);
109
110
        if (isset($options['required'])) {
111
            $child->setRequired($options['required']);
112
        }
113
114
        if (isset($options['default'])) {
115
            $child->setDefault($options['default']);
116
        }
117
118
        if (isset($options['instantiator'])) {
119
            $child->setInstantiator($options['instantiator']);
120
        }
121
122
        if (isset($options['transformer'])) {
123
            $child->setTransformer($options['transformer']);
124
        }
125
126
        if (isset($options['constraints'])) {
127
            $child->setConstraints($options['constraints']);
128
        }
129
130
        $this->children[$key] = $child;
131
132
        return $child;
133
    }
134
135
    public function remove(string $key)
136
    {
137
        unset($this->children[$key]);
138
    }
139
140
    /**
141
     * @return BaseNode[]
142
     */
143
    public function getChildren(): array
144
    {
145
        return $this->children;
146
    }
147
148
    public function hasChildren(): bool
149
    {
150
        return !empty($this->children);
151
    }
152
153
    public function isRequired(): bool
154
    {
155
        if ($this->hasDefault()) {
156
            return false;
157
        }
158
159
        return $this->required;
160
    }
161
162
    public function getValue(string $field, $value)
1 ignored issue
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
163
    {
164
        $this->checkConstraints($field, $value);
165
166
        if ($this->transformer) {
167
            return $this->transformer->transform($value);
168
        }
169
170
        return $value;
171
    }
172
173
    public function walk($input)
1 ignored issue
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
174
    {
175
        $result = [];
176
177
        if (!$this->hasChildren()) {
178
            return $input;
179
        }
180
181 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...
182
            if (!array_key_exists($field, $input)) {
183
                if ($config->isRequired()) {
184
                    throw new RequiredFieldException($field);
185
                }
186
187
                if (!$config->hasDefault()) {
188
                    continue;
189
                }
190
191
                $input[$field] = $config->getDefault();
192
            }
193
194
            $result[$field] = $config->getValue($field, $config->walk($input[$field]));
195
        }
196
197
        return $result;
198
    }
199
200
    protected function checkConstraints(string $field, $value)
201
    {
202
        foreach ($this->constraints as $constraint) {
203
            if (!$constraint->validate($value)) {
204
                throw new InvalidConstraintException($constraint->getErrorMessage($field));
205
            }
206
        }
207
    }
208
}
209