Passed
Push — master ( 100bd0...da6946 )
by Gabor
05:01
created

AbstractElement   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 3
cbo 2
dl 0
loc 245
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A setName() 0 14 2
A getName() 0 4 1
A getId() 0 4 1
A setType() 0 13 2
A getType() 0 4 1
A setLabel() 0 6 1
A getLabel() 0 4 1
A setValues() 0 6 1
A getValues() 0 4 1
A setValueRange() 0 6 1
A getValueRange() 0 4 1
A addValidator() 0 6 1
A validate() 0 11 3
A setError() 0 6 1
A getErrors() 0 4 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Form\Element\Html;
15
16
use InvalidArgumentException;
17
use WebHemi\Form\ElementInterface;
18
use WebHemi\StringLib;
19
use WebHemi\Validator\ServiceInterface as ValidatorInterface;
20
21
/**
22
 * Class AbstractElement.
23
 */
24
abstract class AbstractElement implements ElementInterface
25
{
26
    /** @var string */
27
    private $name;
28
    /** @var string */
29
    private $identifier;
30
    /** @var string */
31
    private $label;
32
    /** @var string */
33
    private $type;
34
    /** @var array */
35
    private $values = [];
36
    /** @var array */
37
    private $valueRange = [];
38
    /** @var ValidatorInterface[] */
39
    private $validators = [];
40
    /** @var array */
41
    private $errors = [];
42
    /** @var array */
43
    protected $validTypes = [];
44
45
    /**
46
     * AbstractElement constructor.
47
     *
48
     * @param string $type       The type of the form element.
49
     * @param string $name       For HTML forms it is the name attribute.
50
     * @param string $label      Optional. The label of the element. If not set the $name should be used.
51
     * @param array  $values     Optional. The values of the element.
52
     * @param array  $valueRange Optional. The range of interpretation.
53
     */
54 5
    public function __construct(
55
        string $type = null,
56
        string $name = null,
57
        string $label = null,
58
        array $values = [],
59
        array $valueRange = []
60
    ) {
61 5
        if (!empty($type)) {
62 5
            $this->setType($type);
63
        }
64
65 5
        if (!empty($name)) {
66 5
            $this->setName($name);
67
        }
68
69 5
        $this->label = $label;
70 5
        $this->values = $values;
71 5
        $this->valueRange = $valueRange;
72 5
    }
73
74
    /**
75
     * Sets element's name.
76
     *
77
     * @param string $name
78
     * @throws InvalidArgumentException
79
     * @return ElementInterface
80
     */
81 5
    public function setName(string $name) : ElementInterface
82
    {
83 5
        $name = StringLib::convertCamelCaseToUnderscore($name);
84 5
        $name = StringLib::convertNonAlphanumericToUnderscore($name, '[]');
85
86 5
        if (empty($name)) {
87 1
            throw new InvalidArgumentException('During conversion the argument value become an empty string!', 1000);
88
        }
89
90 5
        $this->name = $name;
91 5
        $this->identifier = 'id_'.StringLib::convertNonAlphanumericToUnderscore($name);
92
93 5
        return $this;
94
    }
95
96
    /**
97
     * Gets the element's name.
98
     *
99
     * @return string
100
     */
101 3
    public function getName() : string
102
    {
103 3
        return $this->name;
104
    }
105
106
    /**
107
     * Gets the element's name.
108
     *
109
     * @return string
110
     */
111 3
    public function getId() : string
112
    {
113 3
        return $this->identifier;
114
    }
115
116
    /**
117
     * Sets the element's type.
118
     *
119
     * @param string $type
120
     * @return ElementInterface
121
     */
122 5
    public function setType(string $type) : ElementInterface
123
    {
124 5
        if (!in_array($type, $this->validTypes)) {
125 1
            throw new InvalidArgumentException(
126 1
                sprintf('%s is not a valid %s type', $type, get_called_class()),
127 1
                1001
128
            );
129
        }
130
131 5
        $this->type = $type;
132
133 5
        return $this;
134
    }
135
136
    /**
137
     * Gets the element's type.
138
     *
139
     * @return string
140
     */
141 1
    public function getType() : string
142
    {
143 1
        return $this->type;
144
    }
145
146
    /**
147
     * Sets the element's label.
148
     *
149
     * @param string $label
150
     * @return ElementInterface
151
     */
152 1
    public function setLabel(string $label) : ElementInterface
153
    {
154 1
        $this->label = $label;
155
156 1
        return $this;
157
    }
158
159
    /**
160
     * Gets the element's label.
161
     *
162
     * @return string
163
     */
164 2
    public function getLabel() : string
165
    {
166 2
        return $this->label ?? $this->name;
167
    }
168
169
    /**
170
     * Sets the values.
171
     *
172
     * @param array $values
173
     * @return ElementInterface
174
     */
175 2
    public function setValues(array $values) : ElementInterface
176
    {
177 2
        $this->values = $values;
178
179 2
        return $this;
180
    }
181
182
    /**
183
     * Gets the values.
184
     *
185
     * @return array
186
     */
187 3
    public function getValues() : array
188
    {
189 3
        return $this->values;
190
    }
191
192
    /**
193
     * Sets the range of interpretation. Depends on the element type how it is used: exact element list or a min/max.
194
     *
195
     * @param array $valueRange
196
     * @return ElementInterface
197
     */
198 1
    public function setValueRange(array $valueRange) : ElementInterface
199
    {
200 1
        $this->valueRange = $valueRange;
201
202 1
        return $this;
203
    }
204
205
    /**
206
     * Get the range of interpretation.
207
     *
208
     * @return array
209
     */
210 2
    public function getValueRange() : array
211
    {
212 2
        return $this->valueRange;
213
    }
214
215
    /**
216
     * Adds a validator to the element.
217
     *
218
     * @param ValidatorInterface $validator
219
     * @return ElementInterface
220
     */
221 1
    public function addValidator(ValidatorInterface $validator) : ElementInterface
222
    {
223 1
        $this->validators[] = $validator;
224
225 1
        return $this;
226
    }
227
228
    /**
229
     * Validates the element.
230
     *
231
     * @return ElementInterface
232
     */
233 1
    public function validate() : ElementInterface
234
    {
235
        /** @var ValidatorInterface $validator */
236 1
        foreach ($this->validators as $validator) {
237 1
            if (!$validator->validate($this->values)) {
238 1
                $this->errors[get_class($validator)] = $validator->getErrors();
239
            }
240
        }
241
242 1
        return $this;
243
    }
244
245
    /**
246
     * Set custom error.
247
     *
248
     * @param string $validator
249
     * @param string $error
250
     * @return ElementInterface
251
     */
252 1
    public function setError(string $validator, string $error) : ElementInterface
253
    {
254 1
        $this->errors[$validator] = [$error];
255
256 1
        return $this;
257
    }
258
259
    /**
260
     * Returns the errors collected during the validation.
261
     *
262
     * @return array
263
     */
264 3
    public function getErrors() : array
265
    {
266 3
        return $this->errors;
267
    }
268
}
269