Completed
Push — master ( 72a246...6ab249 )
by Helmut
05:19
created

Checkbox::fillModelWithValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
ccs 4
cts 4
cp 1
cc 3
eloc 3
nc 3
nop 1
crap 3
1
<?php 
2
3
namespace Helmut\Forms\Fields\Checkbox;
4
5
use Helmut\Forms\Field;
6
7
class Checkbox extends Field {
8
9
    protected $value = false;
10
11 7
    public function getValue() 
12
    {
13 7
        return $this->value;
14
    }
15
16 9
    public function getButtonName()
17
    {
18 9
        //
19
    }    
20
21 5
    public function renderWith() 
22
    {
23 5
        return ['checked' => $this->value];
24
    }       
25
26 5
    public function setChecked()
27
    {
28 5
        $this->default = true;
29 5
        return $this;
30
    }
31
32 2
    public function setUnchecked()
33
    {
34 2
        $this->default = false;
35 2
        return $this;
36
    }   
37
38 9
    public function setValueFromDefault()
39
    {
40 9
        $this->value = $this->default;
41 9
    }
42
43 1
    public function setValueFromModel($model)
44
    {
45 1
        if (property_exists($model, $this->name)) {
46 1
            $this->value = $model->{$this->name} ? true : false;
47
        }
48
    }   
49
50
    public function setValueFromRequest($request)
51
    {
52
        $this->value = $request->get($this->name) ? true : false;
53 1
    }
54
55 1
    public function fillModelWithValue($model)
56 1
    {
57
        if (property_exists($model, $this->name)) {
58 1
            $model->{$this->name} = $this->value ? 1 : 0;
59
        }
60 1
    }
61
62
    public function validateRequired()
63
    {
64
        return $this->value === true;
65
    }
66
67
}
68