Completed
Push — master ( 73de08...39d3cf )
by Gabor
03:53
created

FormElement   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 111
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setNodes() 0 11 2
A addNode() 0 7 1
A hasNodes() 0 4 1
A getNodes() 0 4 1
A getType() 0 4 1
A setValue() 0 16 3
A getValue() 0 15 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 array<FormElementInterface> */
23
    protected $nodes = [];
24
25
    /**
26
     * Set the child nodes for the element.
27
     *
28
     * @param array<FormElementInterface> $nodeElements
29
     * @return FormElement
30
     */
31
    public function setNodes(array $nodeElements)
32
    {
33
        /** @var NestedElementInterface $this */
34
        $this->nodes = [];
35
36
        foreach ($nodeElements as $nodeElement) {
37
            $this->addNode($nodeElement);
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * Set child node for the element.
45
     *
46
     * @param FormElementInterface $nodeElement
47
     * @return FormElement
48
     */
49
    protected function addNode(FormElementInterface $nodeElement)
50
    {
51
        $nodeElement->setParentNode($this);
52
        $this->nodes[$nodeElement->getName(false)] = $nodeElement;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Checks if there are child elements.
59
     *
60
     * @return boolean
61
     */
62
    public function hasNodes()
63
    {
64
        return !empty($this->nodes);
65
    }
66
67
    /**
68
     * Gets the child nodes of the element.
69
     *
70
     * @return array<FormElementInterface>
71
     */
72
    public function getNodes()
73
    {
74
        return $this->nodes;
75
    }
76
77
    /**
78
     * Returns the element type.
79
     *
80
     * @return string
81
     */
82
    public function getType()
83
    {
84
        return 'form';
85
    }
86
87
    /**
88
     * Sets element value.
89
     *
90
     * @param mixed $value
91
     * @return FormElement
92
     */
93
    public function setValue($value)
94
    {
95
        $children = $this->getNodes();
96
97
        /**
98
         * @var string               $simpleName
99
         * @var FormElementInterface $child
100
         */
101
        foreach ($children as $simpleName => $child) {
102
            if (isset($value[$simpleName])) {
103
                $child->setValue($value[$simpleName]);
104
            }
105
        }
106
107
        return $this;
108
    }
109
110
    /**
111
     * Returns element value.
112
     *
113
     * @return mixed
114
     */
115
    public function getValue()
116
    {
117
        $children = $this->getNodes();
118
        $value = [];
119
120
        /**
121
         * @var string               $simpleName
122
         * @var FormElementInterface $child
123
         */
124
        foreach ($children as $simpleName => $child) {
125
            $value[$simpleName] = $child->getValue();
126
        }
127
128
        return $value;
129
    }
130
}
131