Completed
Push — master ( a41be1...cd8884 )
by wen
12:14
created

Checkbox::prepareValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
class Checkbox extends Select
6
{
7
    protected $type = 'checkbox';
8
9
    protected $max;
10
    protected $min          = 0;
11
    protected $showCheckAll = false;
12
13
    public function getMax()
14
    {
15
        return $this->max;
16
    }
17
18
    public function setMax($value)
19
    {
20
        $this->max = (int)$value;
21
22
        $this->addValidationRule('max:' . $value);
23
24
        return $this;
25
    }
26
27
    public function getMin()
28
    {
29
        return $this->min;
30
    }
31
32
    public function setMin($value)
33
    {
34
        $this->min = (int)$value;
35
36
        $this->addValidationRule('min:' . $value);
37
38
        return $this;
39
    }
40
41
    public function isShowCheckAll()
42
    {
43
        return $this->showCheckAll;
44
    }
45
46
    public function enableShowCheckAll()
47
    {
48
        $this->showCheckAll = true;
49
50
        return $this;
51
    }
52
53
    public function getValue()
54
    {
55
        $value = parent::getValue();
56
        if (empty($value)) {
57
            return [];
58
        }
59
        return explode(',', $value);
60
    }
61
62
    protected function getDefaultValue()
63
    {
64
        return [];
65
    }
66
67
    protected function prepareValue($value)
68
    {
69
        if (empty($value)) {
70
            return '';
71
        }
72
73
        return implode(',', $value);
74
    }
75
76
    public function toArray()
77
    {
78
        return parent::toArray() + [
79
                'min'          => $this->getMin(),
80
                'max'          => $this->getMax(),
81
                'showCheckAll' => $this->isShowCheckAll(),
82
            ];
83
    }
84
}
85