Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

AbstractElement   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 2
dl 0
loc 231
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 25 2
A setName() 0 14 2
A getName() 0 4 1
A getId() 0 4 1
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 array<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,
56
        string $name,
57
        string $label = null,
58
        array $values = [],
59
        array $valueRange = []
60
    ) {
61 5
        if (!in_array($type, $this->validTypes)) {
62 1
            throw new InvalidArgumentException(
63
                sprintf(
64 1
                    'The given type "%s" is not a valid %s type.',
65
                    $type,
66 1
                    __CLASS__
67
                ),
68 1
                1001
69
            );
70
        }
71
72 5
        $this->setName($name);
73
74 5
        $this->type = $type;
75 5
        $this->label = $label;
76 5
        $this->values = $values;
77 5
        $this->valueRange = $valueRange;
78 5
    }
79
80
    /**
81
     * Sets element's name.
82
     *
83
     * @param string $name
84
     * @throws InvalidArgumentException
85
     * @return ElementInterface
86
     */
87 5
    public function setName(string $name) : ElementInterface
88
    {
89 5
        $name = StringLib::convertCamelCaseToUnderscore($name);
90 5
        $name = StringLib::convertNonAlphanumericToUnderscore($name, '[]');
91
92 5
        if (empty($name)) {
93 1
            throw new InvalidArgumentException('During conversion the argument value become an empty string!', 1002);
94
        }
95
96 5
        $this->name = $name;
97 5
        $this->identifier = 'id_'.StringLib::convertNonAlphanumericToUnderscore($name);
98
99 5
        return $this;
100
    }
101
102
    /**
103
     * Gets the element's name.
104
     *
105
     * @return string
106
     */
107 3
    public function getName() : string
108
    {
109 3
        return $this->name;
110
    }
111
112
    /**
113
     * Gets the element's name.
114
     *
115
     * @return string
116
     */
117 3
    public function getId() : string
118
    {
119 3
        return $this->identifier;
120
    }
121
122
    /**
123
     * Gets the element's type.
124
     *
125
     * @return string
126
     */
127 1
    public function getType() : string
128
    {
129 1
        return $this->type;
130
    }
131
132
    /**
133
     * Sets the element's label.
134
     *
135
     * @param string $label
136
     * @return ElementInterface
137
     */
138 1
    public function setLabel(string $label) : ElementInterface
139
    {
140 1
        $this->label = $label;
141
142 1
        return $this;
143
    }
144
145
    /**
146
     * Gets the element's label.
147
     *
148
     * @return string
149
     */
150 2
    public function getLabel() : string
151
    {
152 2
        return $this->label ?? $this->name;
153
    }
154
155
    /**
156
     * Sets the values.
157
     *
158
     * @param array $values
159
     * @return ElementInterface
160
     */
161 2
    public function setValues(array $values) : ElementInterface
162
    {
163 2
        $this->values = $values;
164
165 2
        return $this;
166
    }
167
168
    /**
169
     * Gets the values.
170
     *
171
     * @return array
172
     */
173 3
    public function getValues() : array
174
    {
175 3
        return $this->values;
176
    }
177
178
    /**
179
     * Sets the range of interpretation. Depends on the element type how it is used: exact element list or a min/max.
180
     *
181
     * @param array $valueRange
182
     * @return ElementInterface
183
     */
184 1
    public function setValueRange(array $valueRange) : ElementInterface
185
    {
186 1
        $this->valueRange = $valueRange;
187
188 1
        return $this;
189
    }
190
191
    /**
192
     * Get the range of interpretation.
193
     *
194
     * @return array
195
     */
196 2
    public function getValueRange() : array
197
    {
198 2
        return $this->valueRange;
199
    }
200
201
    /**
202
     * Adds a validator to the element.
203
     *
204
     * @param ValidatorInterface $validator
205
     * @return ElementInterface
206
     */
207 1
    public function addValidator(ValidatorInterface $validator) : ElementInterface
208
    {
209 1
        $this->validators[] = $validator;
210
211 1
        return $this;
212
    }
213
214
    /**
215
     * Validates the element.
216
     *
217
     * @return ElementInterface
218
     */
219 1
    public function validate() : ElementInterface
220
    {
221
        /** @var ValidatorInterface $validator */
222 1
        foreach ($this->validators as $validator) {
223 1
            if (!$validator->validate($this->values)) {
224 1
                $this->errors[get_class($validator)] = $validator->getErrors();
225
            }
226
        }
227
228 1
        return $this;
229
    }
230
231
    /**
232
     * Set custom error.
233
     *
234
     * @param string $validator
235
     * @param string $error
236
     * @return ElementInterface
237
     */
238 1
    public function setError(string $validator, string $error) : ElementInterface
239
    {
240 1
        $this->errors[$validator] = [$error];
241
242 1
        return $this;
243
    }
244
245
    /**
246
     * Returns the errors collected during the validation.
247
     *
248
     * @return array
249
     */
250 3
    public function getErrors() : array
251
    {
252 3
        return $this->errors;
253
    }
254
}
255