Nip_Form_Element_Checkbox::getDataFromModel()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
class Nip_Form_Element_Checkbox extends Nip_Form_Element_Input_Abstract
4
{
5
    protected $_type = 'checkbox';
6
7
    public function init()
8
    {
9
        parent::init();
10
        $this->setAttrib('type', 'checkbox');
11
    }
12
13
    /**
14
     * @param $request
15
     * @return $this
16
     */
17
    public function getDataFromRequest($request)
18
    {
19
        $this->setChecked($request != null);
20
        return parent::getDataFromRequest($request);
21
    }
22
23
    /**
24
     * @param boolean $checked
25
     * @return $this
26
     */
27
    public function setChecked($checked)
28
    {
29
        if ($checked === true) {
30
            $this->setAttrib('checked', 'checked');
31
        } else {
32
            $this->delAttrib('checked');
33
        }
34
        return $this;
35
    }
36
37
    /**
38
     * @param $data
39
     * @return $this
40
     */
41
    public function getDataFromModel($value)
42
    {
43
        $inputValue = $this->getValue();
44
        if ($inputValue == null && $value) {
45
            $this->setChecked(true);
46
        }
47
        return parent::getDataFromModel($value);
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function isChecked()
54
    {
55
        return $this->getAttrib('checked') == 'checked';
56
    }
57
}
58