Completed
Push — master ( 594724...afa8c7 )
by Derek Stephen
02:00
created

MultiCheckBox::uncheckValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 19/11/2016
5
 * Time: 21:37
6
 */
7
8
namespace Del\Form\Field;
9
10
use Del\Form\Renderer\Field\MultiCheckboxRender;
11
use Del\Form\Traits\CanRenderInlineTrait;
12
use Del\Form\Traits\HasOptionsTrait;
13
14
class MultiCheckBox extends FieldAbstract implements ArrayValueInterface
15
{
16
17
    use CanRenderInlineTrait;
18
    use HasOptionsTrait;
19
20
    /**
21
     * We end up ignoring this during rendering Checkboxes, see the renderer for info
22
     *
23
     * @return string
24
     */
25
    public function getTag()
26
    {
27
        return 'div';
28
    }
29
30
    public function init()
31
    {
32
        $this->setValue([]);
33
        $this->setRenderInline(false);
34
        $this->setRenderer(new MultiCheckboxRender());
35
    }
36
37
    /**
38
     * @param $key
39
     * @return $this
40
     */
41
    public function checkValue($key)
42
    {
43
        $values = $this->getValue();
44
        $values[$key] = true;
45
        $this->setValue($values);
46
        return $this;
47
    }
48
49
    /**
50
     * @param $key
51
     * @return $this
52
     */
53
    public function uncheckValue($key)
54
    {
55
        $values = $this->getValue();
56
        if (in_array($key, $values)) {
57
            $index = array_search($key, $values);
58
            unset($values[$index]);
59
        }
60
        $this->setValue($values);
61
        return $this;
62
    }
63
}