FieldSet::get()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 1
crap 5
1
<?php
2
3
/**
4
 * This file is part of slick/form package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Form\Element;
11
12
use Slick\Common\Utils\Collection\AbstractCollection;
13
use Slick\Form\ElementInterface;
14
use Slick\Form\Input\ValidationAwareInterface;
15
use Slick\Form\InputInterface;
16
use Slick\Form\Renderer\FieldSet as FieldSetRenderer;
17
use Slick\Form\Renderer\RendererInterface;
18
19
/**
20
 * FieldSet
21
 *
22
 * @package Slick\Form\Element
23
 */
24
class FieldSet extends AbstractCollection implements ContainerInterface
25
{
26
27
    /**
28
     * @var array
29
     */
30
    protected $settings = [];
31
    
32
    /**
33
     * Add attribute manipulation methods
34
     */
35
    use AttributesAwareMethods;
36
37
    /**
38
     * Add render capabilities to element
39
     */
40
    use RenderAwareMethods;
41
42
    /**
43
     * @var string
44
     */
45
    protected $name;
46
47
    /**
48
     * @var string
49
     */
50
    protected $value;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $rendering = false;
56
57
    /**
58
     * Checks if all elements are valid
59
     *
60
     * @return mixed
61
     */
62 4
    public function isValid()
63
    {
64 4
        $valid = true;
65 4
        $this->validate();
66 4
        foreach ($this->getIterator() as $element) {
67 4
            if ($element instanceof ValidationAwareInterface) {
68 4
                $valid = $element->isValid()
69 4
                    ? $valid
70 4
                    : false;
71 4
            }
72 4
        }
73 4
        return $valid;
74
    }
75
76
    /**
77
     * Adds an element to the container
78
     *
79
     * @param ElementInterface $element
80
     *
81
     * @return self|$this|ContainerInterface
82
     */
83 52
    public function add(ElementInterface $element)
84
    {
85 52
        if (null === $element->getName()) {
86 4
            $this->data[] = $element;
87 4
            return $this;
88
        }
89 52
        $this->data[$element->getName()] = $element;
90 52
        return $this;
91
    }
92
93
    /**
94
     * Gets element by name
95
     *
96
     * @param string $name
97
     *
98
     * @return null|ElementInterface|InputInterface
99
     */
100 14
    public function get($name)
101
    {
102 14
        $selected = null;
103
        /** @var ElementInterface|ContainerInterface $element */
104 14
        foreach ($this as $element) {
105 14
            if ($element->getName() == $name) {
106 14
                $selected = $element;
107 14
                break;
108
            }
109
110 4
            if ($element instanceof ContainerInterface) {
111 2
                $selected = $element->get($name);
112 2
                if (null !== $selected) {
113 2
                    break;
114
                }
115 2
            }
116 14
        }
117
118 14
        return $selected;
119
    }
120
121
    /**
122
     * Sets the values form matched elements
123
     *
124
     * The passed array is a key/value array where keys are used to
125
     * match against element names. It will only assign the value to
126
     * the marched element only.
127
     *
128
     * @param array $values
129
     *
130
     * @return self|$this|ContainerInterface
131
     */
132 2
    public function setValues(array $values)
133
    {
134 2
        foreach ($values as $name => $value) {
135 2
            if ($element = $this->get($name)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $element is correct as $this->get($name) (which targets Slick\Form\Element\FieldSet::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
136 2
                $element->setValue($value);
137 2
            }
138 2
        }
139 2
        return $this;
140
    }
141
142
    /**
143
     * Gets the element value
144
     *
145
     * This value here is just a string and it can be the content that
146
     * goes inside <label> tags for example.
147
     *
148
     * @return mixed
149
     */
150 4
    public function getValue()
151
    {
152 4
        return $this->value;
153
    }
154
155
    /**
156
     * Returns element name.
157
     *
158
     * This name can be null however a form or field set will use it
159
     * in the FormInterface::get() method to retrieve it.
160
     *
161
     * @return mixed
162
     */
163 36
    public function getName()
164
    {
165 36
        return $this->name;
166
    }
167
168
    /**
169
     * Set element name
170
     *
171
     * @param string $name
172
     * @return $this|self|FieldSet
173
     */
174 32
    public function setName($name)
175
    {
176 32
        $this->name = $name;
177 32
        return $this;
178
    }
179
180
    /**
181
     * Sets the value or content of the element
182
     *
183
     * @param mixed $value
184
     *
185
     * @return self|$this|FieldSet
186
     */
187 4
    public function setValue($value)
188
    {
189 4
        $this->value = $value;
190 4
        return $this;
191
    }
192
193
    /**
194
     * Adds an element to the collection
195
     *
196
     * @param mixed $offset unused
197
     * @param ElementInterface $value
198
     */
199 2
    public function offsetSet($offset, $value)
200
    {
201 2
        $this->add($value);
202 2
    }
203
204
    /**
205
     * Gets the HTML renderer for this element
206
     *
207
     * @return RendererInterface
208
     */
209 2
    protected function getRenderer()
210
    {
211 2
        return new FieldSetRenderer($this);
212
    }
213
214
    /**
215
     * Runs validation chain in all its elements
216
     *
217
     * @return self|$this|ElementInterface
218
     */
219 4
    public function validate()
220
    {
221 4
        foreach ($this as $element) {
222
223
            if (
224 4
                $element instanceof ValidationAwareInterface ||
225
                $element instanceof ContainerInterface
226 4
            ) {
227 4
                $element->validate();
228 4
            }
229 4
        }
230 4
        return $this;
231
    }
232
233
    /**
234
     * Set other input settings
235
     *
236
     * @param array $settings
237
     * @return self|AbstractElement
238
     */
239
    public function setSettings(array $settings)
240
    {
241
        $this->settings = array_merge($this->settings, $settings);
242
        return $this;
243
    }
244
245
    /**
246
     * Set rendering state
247
     *
248
     * @param bool $state
249
     *
250
     * @return FieldSet
251
     */
252 4
    public function setRendering($state)
253
    {
254 4
        $this->rendering = $state;
255 4
        return $this;
256
    }
257
}