Completed
Push — master ( b9b773...5bbb0b )
by Gabor
64:05
created

FormElement::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 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
namespace WebHemi\Form;
13
14
use Iterator;
15
use InvalidArgumentException;
16
use RuntimeException;
17
use WebHemi\Form\Validator\FormValidatorInterface;
18
19
/**
20
 * Class FormElement
21
 */
22
class FormElement implements Iterator
23
{
24
    const TAG_INPUT_TEXT = 'text';
25
    const TAG_INPUT_PASSWORD = 'password';
26
    const TAG_FIELDSET = 'fieldset';
27
    const TAG_BUTTON_SUBMIT = 'submit';
28
    const TAG_BUTTON_RESET = 'reset';
29
    const TAG_BUTTON = 'button';
30
31
    /** @var int */
32
    protected static $tabIndex = 1;
33
    /** @var string */
34
    private $tagName;
35
    /** @var string */
36
    private $name;
37
    /** @var string */
38
    private $label;
39
    /** @var mixed */
40
    private $value;
41
    /** @var array */
42
    private $attributes;
43
    /** @var FormElement */
44
    private $parentNode;
45
    /** @var FormElement[] */
46
    private $childNodes;
47
    /** @var FormValidatorInterface[] */
48
    private $validators;
49
50
    /**
51
     * FormElement constructor.
52
     *
53
     * @param string $tagName
54
     * @param string $name
55
     * @param string $label
56
     */
57
    public function __construct($tagName, $name, $label = '')
58
    {
59
        $this->tagName = $tagName;
60
        $this->name = $name;
61
        $this->label = $label;
62
        $this->attributes['tabindex'] = self::$tabIndex++;
63
    }
64
65
    /**
66
     * Returns the element tag name.
67
     *
68
     * @return string
69
     */
70
    public function getTagName()
71
    {
72
        return $this->tagName;
73
    }
74
75
    /**
76
     * Sets parent element name
77
     *
78
     * @param FormElement $formElement
79
     * @return FormElement
80
     */
81
    public function setParentNode(FormElement $formElement)
82
    {
83
        $this->parentNode = $formElement;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Returns the element name.
90
     *
91
     * @return string
92
     */
93
    public function getName()
94
    {
95
        if (isset($this->parentNode)) {
96
            return $this->parentNode->getName() . '[' . $this->name . ']';
97
        }
98
99
        return $this->name;
100
    }
101
102
    /**
103
     * Returns the element label.
104
     *
105
     * @return string
106
     */
107
    public function getLabel()
108
    {
109
        return $this->label;
110
    }
111
112
    /**
113
     * Sets element value.
114
     *
115
     * @param mixed $value
116
     * @return FormElement
117
     */
118
    public function setValue($value)
119
    {
120
        $this->value = $value;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Returns element value.
127
     *
128
     * @return mixed
129
     */
130
    public function getValue()
131
    {
132
        return $this->value;
133
    }
134
135
    /**
136
     * Set child node for the element.
137
     *
138
     * @param FormElement $childNode
139
     * @return FormElement
140
     */
141
    public function addChildNode(FormElement $childNode)
142
    {
143
        $childNode->setParentNode($this);
144
145
        $this->childNodes[] = $childNode;
146
147
        return $this;
148
    }
149
150
    /**
151
     * Gets the child nodes of the element.
152
     *
153
     * @return FormElement[]
154
     */
155
    public function getChildNodes()
156
    {
157
        return $this->childNodes;
158
    }
159
160
    /**
161
     * Sets element attribute.
162
     *
163
     * @param string $key
164
     * @param string $value
165
     * @throws InvalidArgumentException
166
     * @return FormElement
167
     */
168
    public function setAttribute($key, $value)
169
    {
170
        if ($key == 'name') {
171
            throw new InvalidArgumentException('Cannot change element name after it has been initialized.');
172
        }
173
174
        if (!is_scalar($value)) {
175
            throw new InvalidArgumentException('Element attribute can hold scalar data only.');
176
        }
177
178
        $this->attributes[$key] = $value;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Sets multiple attributes.
185
     *
186
     * @param array $attributes
187
     * @return FormElement
188
     */
189
    public function setAttributes(array $attributes)
190
    {
191
        foreach ($attributes as $key => $value) {
192
            $this->setAttribute($key, $value);
193
        }
194
195
        return $this;
196
    }
197
198
    /**
199
     * Gets element attribute.
200
     *
201
     * @param string $name
202
     * @return mixed
203
     */
204
    public function getAttribute($name)
205
    {
206
        if (!isset($this->attributes[$name])) {
207
            throw new RuntimeException('Invalid attribute: "' . $name . '"');
208
        }
209
210
        return $this->attributes[$name];
211
    }
212
213
    /**
214
     * Gets all the attributes.
215
     *
216
     * @return array
217
     */
218
    public function getAttributes()
219
    {
220
        return $this->attributes;
221
    }
222
223
    /**
224
     * Adds validator to the form.
225
     *
226
     * @param FormValidatorInterface $validator
227
     * @return FormElement
228
     */
229
    public function addValidator(FormValidatorInterface $validator)
230
    {
231
        $this->validators[] = $validator;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Validates element value.
238
     *
239
     * @return bool
240
     */
241
    public function isValid()
242
    {
243
        foreach ($this->validators as $validator) {
244
            if (!$validator->validate($this->value)) {
245
                return false;
246
            }
247
        }
248
249
        return true;
250
    }
251
252
    /**
253
     * Return the current element.
254
     *
255
     * @return FormElement
256
     */
257
    final public function current()
258
    {
259
        return current($this->childNodes);
260
    }
261
262
    /**
263
     * Moves the pointer forward to next element.
264
     *
265
     * @return void
266
     */
267
    final public function next()
268
    {
269
        next($this->childNodes);
270
    }
271
272
    /**
273
     * Returns the key of the current element.
274
     *
275
     * @return mixed
276
     */
277
    final public function key()
278
    {
279
        return key($this->childNodes);
280
    }
281
282
    /**
283
     * Checks if current position is valid.
284
     *
285
     * @return boolean
286
     */
287
    final public function valid()
288
    {
289
        $key = key($this->childNodes);
290
291
        return ($key !== NULL && $key !== FALSE);
292
    }
293
294
    /**
295
     * Rewinds the Iterator to the first element.
296
     *
297
     * @return void
298
     */
299
    final public function rewind()
300
    {
301
        reset($this->childNodes);
302
    }
303
}
304
305