Passed
Push — master ( 793f0f...d7ccbf )
by Chris
04:53
created

Checklist::renderItemsFromSelection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 6
rs 10
1
<?php
2
3
namespace WebTheory\Saveyour\Fields;
4
5
use WebTheory\Saveyour\Concerns\IsBasicallyChecklistTrait;
6
use WebTheory\Saveyour\Contracts\CheckableFieldInterface;
7
use WebTheory\Saveyour\Contracts\FormFieldInterface;
8
9
class Checklist extends AbstractChecklist implements FormFieldInterface
10
{
11
    use IsBasicallyChecklistTrait;
12
13
    /**
14
     * Value for hidden input that facilitates unsetting all values on the server
15
     *
16
     * @var string
17
     */
18
    protected $clearControl = '0';
19
20
    /**
21
     * Get value for hidden input that facilitates unsetting all values on the server
22
     *
23
     * @return string
24
     */
25
    public function getClearControl(): string
26
    {
27
        return $this->clearControl;
28
    }
29
30
    /**
31
     * Set value for hidden input that facilitates unsetting all values on the server
32
     *
33
     * @param string
34
     *
35
     * @return self
36
     */
37
    public function setClearControl(string $clearControl)
38
    {
39
        $this->clearControl = $clearControl;
40
41
        return $this;
42
    }
43
44
    /**
45
     *
46
     */
47
    protected function createClearControl(): Hidden
48
    {
49
        return (new Hidden())
50
            ->setName($this->name . "[]")
51
            ->setValue($this->clearControl);
52
    }
53
54
    /**
55
     *
56
     */
57
    protected function createItemCheckBox(array $values): CheckableFieldInterface
58
    {
59
        return (new Checkbox())
60
            ->setId($values['id'] ?? '')
61
            ->setValue($values['value'] ?? '')
62
            ->setName($this->name . '[]');
63
    }
64
65
    /**
66
     *
67
     */
68
    protected function defineChecklistItem(string $item, array $values)
69
    {
70
        $html = '';
71
        $html .= $this->createItemCheckBox($values)->setChecked($this->isItemChecked($item));
72
        $html .= $this->createItemLabel($values)->setFor($values['id'] ?? '');
73
74
        return $html;
75
    }
76
77
    /**
78
     *
79
     */
80
    protected function renderItemsFromProvider(): string
81
    {
82
        $html = '';
83
        $provider = $this->checklistItemProvider;
84
85
        foreach ($provider->provideItemsAsRawData() as $item) {
86
            $values = [
87
                'id' => $provider->provideItemId($item),
88
                'value' => $provider->provideItemValue($item),
89
                'label' => $provider->provideItemLabel($item)
90
            ];
91
92
            $checked = $this->isItemChecked($values['value']);
93
94
            $html .= $this->open('li');
95
            $html .= $this->createItemCheckBox($values)->setChecked($checked);
96
            $html .= $this->createItemLabel($values)->setFor($values['id']);
97
            $html .= $this->close('li');
98
        }
99
100
        return $html;
101
    }
102
103
    /**
104
     *
105
     */
106
    protected function renderItemsFromSelection(): string
107
    {
108
        $html = '';
109
110
        foreach ($this->getItemsToRender() as $item => $values) {
111
            $html .= $this->open('li');
112
            $html .= $this->defineChecklistItem($item, $values);
113
            $html .= $this->close('li');
114
        }
115
116
        return $html;
117
    }
118
119
    /**
120
     *
121
     */
122
    protected function renderItems()
123
    {
124
        return isset($this->checklistItemProvider)
125
            ? $this->renderItemsFromProvider()
126
            : $this->renderItemsFromSelection();
127
    }
128
129
    // /**
130
    //  *
131
    //  */
132
    // public function renderHtmlMarkup(): string
133
    // {
134
    //     $html = '';
135
    //     $html .= $this->open('div', $this->attributes ?? null);
136
    //     $html .= $this->createClearControl();
137
    //     $html .= $this->open('ul');
138
    //     $html .= $this->renderItems();
139
    //     $html .= $this->close('ul');
140
    //     $html .= $this->close('div');
141
142
    //     return $html;
143
    // }
144
145
    /**
146
     *
147
     */
148
    public function renderHtmlMarkup(): string
149
    {
150
        return $this->open('div', $this->attributes ?? null)
151
            . $this->createClearControl()
152
            . $this->open('ul')
153
            . $this->renderItems()
154
            . $this->close('ul')
155
            . $this->close('div');
156
    }
157
}
158