Passed
Push — master ( 446c00...d7e047 )
by Chris
02:29
created

Checklist::renderSelection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 21
ccs 0
cts 16
cp 0
crap 6
rs 9.8333
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
     * @var ChecklistItemsProviderInterface
20
     */
21
    protected $selectionProvider;
22
23
    /**
24
     * Get the value of selectionProvider
25
     *
26
     * @return ChecklistItemsProviderInterface
27
     */
28
    public function getSelectionProvider(): ChecklistItemsProviderInterface
29
    {
30
        return $this->selectionProvider;
31
    }
32
33
    /**
34
     * Set the value of selectionProvider
35
     *
36
     * @param ChecklistItemsProviderInterface $selectionProvider
37
     *
38
     * @return self
39
     */
40
    public function setSelectionProvider(ChecklistItemsProviderInterface $selectionProvider)
41
    {
42
        $this->selectionProvider = $selectionProvider;
43
44
        return $this;
45
    }
46
47
    /**
48
     *
49
     */
50
    protected function getSelectionItemsName(): string
51
    {
52
        return $this->name . '[]';
53
    }
54
55
    /**
56
     *
57
     */
58
    protected function renderHtmlMarkup(): string
59
    {
60
        $clearControl = $this->createClearControlField()
61
            ->setName($this->getSelectionItemsName())
62
            ->setValue('');
63
64
        $list = $this->tag('ul', [], $this->renderSelection());
65
66
        return $this->tag('div', $this->attributes, $clearControl . $list);
67
    }
68
69
    /**
70
     *
71
     */
72
    protected function renderSelection(): string
73
    {
74
        $html = '';
75
76
        foreach ($this->getSelectionData() as $selection) {
77
            $id  = $this->defineSelectionId($selection);
78
            $value = $this->defineSelectionValue($selection);
79
80
            $checkbox = $this->createSelectionCheckbox($selection)
81
                ->setChecked($this->isSelectionSelected($value))
82
                ->setDisabled($this->disabled)
83
                ->setName($this->getSelectionItemsName())
84
                ->setValue($value)
85
                ->setId($id);
86
87
            $label = $this->createSelectionLabel($selection)->setFor($id);
88
89
            $html .= $this->tag('li', [], $checkbox . $label);
90
        }
91
92
        return $html;
93
    }
94
95
    /**
96
     *
97
     */
98
    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

98
    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...
99
    {
100
        return new Checkbox();
101
    }
102
}
103