Checkboxes::setUnchecked()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 14
loc 14
c 0
b 0
f 0
rs 9.2
ccs 8
cts 8
cp 1
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php 
2
3
namespace Helmut\Forms\Fields\Checkboxes;
4
5
use Helmut\Forms\Field;
6
7
class Checkboxes extends Field {
8
9
    protected $default = [];
10
    protected $options = [];
11
    protected $values = [];
12
13 11
    public function setOptions($options)
14
    {
15 11
        $this->options = $options;
16 11
        $this->values = [];
17 11
        foreach (array_keys($this->options) as $key) {
18 11
            $this->values[$key] = false;
19
        }
20 11
        return $this;
21
    }
22
23 7 View Code Duplication
    public function setChecked($keys = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25 7
        if (count($keys)) {
26 3
            foreach ($keys as $key) {
27 3
                $this->default[$key] = true;
28
            }
29
        } else {
30 4
            $this->defaults = [];
0 ignored issues
show
Bug introduced by
The property defaults does not seem to exist. Did you mean default?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31 4
            foreach (array_keys($this->options) as $key) {
32 4
                $this->default[$key] = true;
33
            }
34
        }
35 7
        return $this;
36
    }
37
38 3 View Code Duplication
    public function setUnchecked($keys = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40 3
        if (count($keys)) {
41 1
            foreach ($keys as $key) {
42 1
                $this->default[$key] = false;
43
            }
44
        } else {
45 2
            $this->defaults = [];
0 ignored issues
show
Bug introduced by
The property defaults does not seem to exist. Did you mean default?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46 2
            foreach (array_keys($this->options) as $key) {
47 2
                $this->default[$key] = false;
48
            }
49
        }
50 3
        return $this;
51
    }       
52
53 9
    public function getValue() 
54
    {
55 9
        $values = [];
56 9 View Code Duplication
        foreach (array_keys($this->options) as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57 9
            $values[$this->name.'_'.$key] = $this->values[$key];
58
        }
59 9
        return $values;
60
    }
61
62 11
    public function getButtonName()
63
    {
64 11
        //
65
    }           
66
67 7
    public function renderWith() 
68
    {
69 7
        $properties = [];
70
71 7
        $options = [];
72 7
        foreach ($this->options as $name => $label) {
73 7
            $options[] = [  
74 7
                'name' => $this->name.'_'.$name, 
75 7
                'label'=>$label, 
76 7
                'checked' => $this->values[$name] ? true : false
77
            ];
78
        }
79 7
        $properties['options'] = $options;
80
81
        return $properties;
82 11
    }
83
84 11
    public function setValueFromDefault()
85 11
    {
86
        foreach (array_keys($this->options) as $key) {
87 11
            if (isset($this->default[$key])) $this->values[$key] = $this->default[$key] ? true : false;
88
        }
89 1
    }
90
91 1
    public function setValueFromModel($model)
92 1
    {
93
        foreach (array_keys($this->options) as $key) {
94
            if (isset($model->{$this->name.'_'.$key})) $this->values[$key] = $model->{$this->name.'_'.$key} ? true : false;
95 1
        }
96
97
    }   
98
99
    public function setValueFromRequest($request)
100
    {
101
        foreach (array_keys($this->options) as $key) {      
102
            $this->values[$key] = $request->get($this->name.'_'.$key) ? true : false;
103
        }
104 1
    }
105
106 1
    public function fillModelWithValue($model)
107 1
    {
108 View Code Duplication
        foreach (array_keys($this->options) as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109 1
            $model->{$this->name.'_'.$key} = $this->values[$key] ? 1 : 0;
110
        }
111 1
    }
112
113 1
    public function validateRequired()
114
    {
115 1
        $checked = false;
116 1
117
        foreach (array_keys($this->options) as $key) {
118
            if ($this->values[$key]) $checked = true;
119 1
        }
120
121
        return $checked;
122
    }
123
124
}
125