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

MultiCheckBox   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTag() 0 4 1
A init() 0 6 1
A checkValue() 0 7 1
A uncheckValue() 0 10 2
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
}