Passed
Push — master ( 3c0766...793f0f )
by Chris
05:28
created

Checklist::renderHtmlMarkup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 17
ccs 0
cts 14
cp 0
crap 6
rs 9.9
1
<?php
2
3
namespace WebTheory\Saveyour\Fields;
4
5
use WebTheory\Saveyour\Contracts\FormFieldInterface;
6
7
class Checklist extends AbstractChecklist implements FormFieldInterface
8
{
9
    /**
10
     * Value for hidden input that facilitates unsetting all values on the server
11
     *
12
     * @var string
13
     */
14
    protected $clearControl = '0';
15
16
    /**
17
     * Get value for hidden input that facilitates unsetting all values on the server
18
     *
19
     * @return string
20
     */
21
    public function getClearControl(): string
22
    {
23
        return $this->clearControl;
24
    }
25
26
    /**
27
     * Set value for hidden input that facilitates unsetting all values on the server
28
     *
29
     * @param string
30
     *
31
     * @return self
32
     */
33
    public function setClearControl(string $clearControl)
34
    {
35
        $this->clearControl = $clearControl;
36
37
        return $this;
38
    }
39
40
    /**
41
     *
42
     */
43
    protected function createClearControl(): Hidden
44
    {
45
        return (new Hidden())
46
            ->setName($this->name . "[]")
47
            ->setValue($this->clearControl);
48
    }
49
50
    /**
51
     *
52
     */
53
    protected function createItemCheckBox(array $values): Checkbox
54
    {
55
        return (new Checkbox())
56
            ->setId($values['id'] ?? '')
57
            ->setValue($values['value'] ?? '')
58
            ->setName($this->name . '[]');
59
    }
60
61
    /**
62
     *
63
     */
64
    protected function defineChecklistItem(string $item, array $values)
65
    {
66
        $html = '';
67
        $html .= $this->createItemCheckBox($values)->setChecked($this->isItemChecked($item));
68
        $html .= $this->createItemLabel($values)->setFor($values['id'] ?? '');
69
70
        return $html;
71
    }
72
73
    /**
74
     *
75
     */
76
    public function renderHtmlMarkup(): string
77
    {
78
        $html = '';
79
        $html .= $this->open('div', $this->attributes ?? null);
80
        $html .= $this->createClearControl();
81
        $html .= $this->open('ul');
82
83
        foreach ($this->getItemsToRender() as $item => $values) {
84
            $html .= $this->open('li');
85
            $html .= $this->defineChecklistItem($item, $values);
86
            $html .= $this->close('li');
87
        }
88
89
        $html .= $this->close('ul');
90
        $html .= $this->close('div');
91
92
        return $html;
93
    }
94
}
95