Completed
Push — master ( 8219b7...c7f806 )
by Gabor
03:21
created

FormElement::getNodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     * Set the child nodes for the element.
29
     *
30
     * @param array<FormElementInterface> $nodeElements
31
     * @return FormElement
32
     */
33 7
    public function setNodes(array $nodeElements)
34
    {
35
        /** @var NestedElementInterface $this */
36 7
        $this->nodes = [];
37
38 7
        foreach ($nodeElements as $nodeElement) {
39 7
            $this->addNode($nodeElement);
40
        }
41
42 7
        return $this;
43
    }
44
45
    /**
46
     * Set child node for the element.
47
     *
48
     * @param FormElementInterface $nodeElement
49
     * @return FormElement
50
     */
51 7
    protected function addNode(FormElementInterface $nodeElement)
52
    {
53 7
        $nodeElement->setParentNode($this);
54 7
        $this->nodes[$nodeElement->getName(false)] = $nodeElement;
55
56 7
        return $this;
57
    }
58
59
    /**
60
     * Checks if there are child elements.
61
     *
62
     * @return boolean
63
     */
64
    public function hasNodes()
65
    {
66
        return !empty($this->nodes);
67
    }
68
69
    /**
70
     * Gets the child nodes of the element.
71
     *
72
     * @return array<FormElementInterface>
73
     */
74 2
    public function getNodes()
75
    {
76 2
        return $this->nodes;
77
    }
78
79
    /**
80
     * Sets element value.
81
     *
82
     * @param mixed $value
83
     * @return FormElement
84
     */
85 1
    public function setValue($value)
86
    {
87 1
        $children = $this->getNodes();
88
89
        /**
90
         * @var string               $simpleName
91
         * @var FormElementInterface $child
92
         */
93 1
        foreach ($children as $simpleName => $child) {
94 1
            if (isset($value[$simpleName])) {
95 1
                $child->setValue($value[$simpleName]);
96
            }
97
        }
98
99 1
        return $this;
100
    }
101
102
    /**
103
     * Returns element value.
104
     *
105
     * @return mixed
106
     */
107 1
    public function getValue()
108
    {
109 1
        $children = $this->getNodes();
110 1
        $value = [];
111
112
        /**
113
         * @var string               $simpleName
114
         * @var FormElementInterface $child
115
         */
116 1
        foreach ($children as $simpleName => $child) {
117 1
            $value[$simpleName] = $child->getValue();
118
        }
119
120 1
        return $value;
121
    }
122
}
123