Passed
Push — master ( 4c5faf...429320 )
by Chris
04:23
created

Checklist   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 131
ccs 0
cts 54
cp 0
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createClearControlField() 0 3 1
A setSelectionProvider() 0 5 1
A getSelectionProvider() 0 3 1
A renderHtmlMarkup() 0 9 1
A getSelectionItemsName() 0 3 1
A createSelectionCheckbox() 0 3 1
A getClearControl() 0 3 1
A renderSelection() 0 21 2
A setClearControl() 0 5 1
1
<?php
2
3
namespace WebTheory\Saveyour\Fields;
4
5
use WebTheory\Saveyour\Concerns\MultiValueSelectionTrait;
6
use WebTheory\Saveyour\Contracts\ChecklistItemsProviderInterface;
7
use WebTheory\Saveyour\Contracts\FormFieldInterface;
8
9
class Checklist extends AbstractCompositeSelectionField implements FormFieldInterface
10
{
11
    use MultiValueSelectionTrait;
12
13
    /**
14
     * @var array
15
     */
16
    protected $value = [];
17
18
    /**
19
     * Value for hidden input that facilitates unsetting all values on the server
20
     *
21
     * @var string
22
     */
23
    protected $clearControl = '0';
24
25
    /**
26
     * @var ChecklistItemsProviderInterface
27
     */
28
    protected $selectionProvider;
29
30
    /**
31
     * Get value for hidden input that facilitates unsetting all values on the server
32
     *
33
     * @return string
34
     */
35
    public function getClearControl(): string
36
    {
37
        return $this->clearControl;
38
    }
39
40
    /**
41
     * Set value for hidden input that facilitates unsetting all values on the server
42
     *
43
     * @param string
44
     *
45
     * @return self
46
     */
47
    public function setClearControl(string $clearControl)
48
    {
49
        $this->clearControl = $clearControl;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Get the value of selectionProvider
56
     *
57
     * @return ChecklistItemsProviderInterface
58
     */
59
    public function getSelectionProvider(): ChecklistItemsProviderInterface
60
    {
61
        return $this->selectionProvider;
62
    }
63
64
    /**
65
     * Set the value of selectionProvider
66
     *
67
     * @param ChecklistItemsProviderInterface $selectionProvider
68
     *
69
     * @return self
70
     */
71
    public function setSelectionProvider(ChecklistItemsProviderInterface $selectionProvider)
72
    {
73
        $this->selectionProvider = $selectionProvider;
74
75
        return $this;
76
    }
77
78
    /**
79
     *
80
     */
81
    protected function getSelectionItemsName(): string
82
    {
83
        return $this->name . '[]';
84
    }
85
86
    /**
87
     *
88
     */
89
    protected function renderHtmlMarkup(): string
90
    {
91
        $clearControl = $this->createClearControlField()
92
            ->setName($this->getSelectionItemsName())
93
            ->setValue($this->clearControl);
94
95
        $list = $this->tag('ul', [], $this->renderSelection());
96
97
        return $this->tag('div', $this->attributes, $clearControl . $list);
98
    }
99
100
    /**
101
     *
102
     */
103
    protected function createClearControlField(): Hidden
104
    {
105
        return new Hidden();
106
    }
107
108
    /**
109
     *
110
     */
111
    protected function renderSelection(): string
112
    {
113
        $html = '';
114
115
        foreach ($this->getSelectionData() as $selection) {
116
            $id  = $this->defineSelectionId($selection);
117
            $value = $this->defineSelectionValue($selection);
118
119
            $checkbox = $this->createSelectionCheckbox($selection)
120
                ->setChecked($this->isSelectionSelected($value))
121
                ->setDisabled($this->disabled)
122
                ->setName($this->getSelectionItemsName())
123
                ->setValue($value)
124
                ->setId($id);
125
126
            $label = $this->createSelectionLabel($selection)->setFor($id);
127
128
            $html .= $this->tag('li', [], $checkbox . $label);
129
        }
130
131
        return $html;
132
    }
133
134
    /**
135
     *
136
     */
137
    protected function createSelectionCheckbox($selection): Checkbox
0 ignored issues
show
Unused Code introduced by
The parameter $selection is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

137
    protected function createSelectionCheckbox(/** @scrutinizer ignore-unused */ $selection): Checkbox

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
    {
139
        return new Checkbox();
140
    }
141
}
142