Passed
Branch master (41990d)
by Gabor
03:31
created

FormElement::setNodes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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\Element\Web;
13
14
use WebHemi\Form\Element\FormElementInterface;
15
use WebHemi\Form\Element\NestedElementInterface;
16
17
/**
18
 * Class FormElement.
19
 */
20
class FormElement extends AbstractElement implements NestedElementInterface
21
{
22
    /** @var string */
23
    protected $type = 'form';
24
    /** @var array<FormElementInterface> */
25
    protected $nodes = [];
26
27
    /**
28
     * Resets the object when cloning.
29
     */
30 1
    public function __clone()
31
    {
32 1
        parent::__clone();
33
34 1
        $this->nodes = [];
35 1
    }
36
37
    /**
38
     * Set the child nodes for the element.
39
     *
40
     * @param array<FormElementInterface> $nodeElements
41
     * @return FormElement
42
     */
43 10
    public function setNodes(array $nodeElements)
44
    {
45
        /** @var NestedElementInterface $this */
46 10
        $this->nodes = [];
47
48 10
        foreach ($nodeElements as $nodeElement) {
49 10
            $this->addNode($nodeElement);
50 10
        }
51
52 10
        return $this;
53
    }
54
55
    /**
56
     * Set child node for the element.
57
     *
58
     * @param FormElementInterface $nodeElement
59
     * @return FormElement
60
     */
61 10
    protected function addNode(FormElementInterface $nodeElement)
62
    {
63 10
        $nodeElement->setParentNode($this);
64 10
        $this->nodes[$nodeElement->getName(false)] = $nodeElement;
65
66 10
        return $this;
67
    }
68
69
    /**
70
     * Checks if there are child elements.
71
     *
72
     * @return boolean
73
     */
74 1
    public function hasNodes()
75
    {
76 1
        return !empty($this->nodes);
77
    }
78
79
    /**
80
     * Gets the child nodes of the element.
81
     *
82
     * @return array<FormElementInterface>
83
     */
84 4
    public function getNodes()
85
    {
86 4
        return $this->nodes;
87
    }
88
89
    /**
90
     * Sets element value.
91
     *
92
     * @param mixed $value
93
     * @return FormElement
94
     */
95 1
    public function setValue($value)
96
    {
97 1
        $children = $this->getNodes();
98
99
        /**
100
         * @var string               $simpleName
101
         * @var FormElementInterface $child
102
         */
103 1
        foreach ($children as $simpleName => $child) {
104 1
            if (isset($value[$simpleName])) {
105 1
                $child->setValue($value[$simpleName]);
106 1
            }
107 1
        }
108
109 1
        return $this;
110
    }
111
112
    /**
113
     * Returns element value.
114
     *
115
     * @return mixed
116
     */
117 1
    public function getValue()
118
    {
119 1
        $children = $this->getNodes();
120 1
        $value = [];
121
122
        /**
123
         * @var string               $simpleName
124
         * @var FormElementInterface $child
125
         */
126 1
        foreach ($children as $simpleName => $child) {
127 1
            $value[$simpleName] = $child->getValue();
128 1
        }
129
130 1
        return $value;
131
    }
132
133
    /**
134
     * Validates element value.
135
     *
136
     * @param bool $reValidate
137
     * @return bool
138
     */
139 2
    public function isValid($reValidate = false)
140
    {
141 2
        $children = $this->getNodes();
142 2
        $isValid = true;
143
144
        /**
145
         * @var string               $simpleName
146
         * @var FormElementInterface $child
147
         */
148 2
        foreach ($children as $child) {
149 2
            $isValid = $isValid && $child->isValid($reValidate);
150 2
        }
151
152 2
        return $isValid;
153
    }
154
155
    /**
156
     * Returns element value.
157
     *
158
     * @return mixed
159
     */
160 1
    public function getErrors()
161
    {
162 1
        $children = $this->getNodes();
163 1
        $error = [];
164
165
        /**
166
         * @var string               $simpleName
167
         * @var FormElementInterface $child
168
         */
169 1
        foreach ($children as $simpleName => $child) {
170 1
            $childErrors = $child->getErrors();
171 1
            if (!empty($childErrors)) {
172 1
                $error[$simpleName] = $childErrors;
173 1
            }
174 1
        }
175
176 1
        return $error;
177
    }
178
}
179