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

RadioSelection::renderHtmlMarkup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 18
ccs 0
cts 13
cp 0
crap 12
rs 9.9332
1
<?php
2
3
namespace WebTheory\Saveyour\Fields;
4
5
use WebTheory\Saveyour\Concerns\HasLabelsTrait;
6
use WebTheory\Saveyour\Concerns\IsSelectionFieldTrait;
7
use WebTheory\Saveyour\Contracts\FormFieldInterface;
8
9
class RadioSelection extends AbstractFormField implements FormFieldInterface
10
{
11
    use HasLabelsTrait;
12
    use IsSelectionFieldTrait;
13
14
    /**
15
     *
16
     */
17
    protected $items = [];
18
19
    /**
20
     * @var bool
21
     */
22
    protected $isInline = false;
23
24
    /**
25
     *
26
     */
27
    public function __construct(array $items)
28
    {
29
        $this->items = $items;
30
        parent::__construct();
31
    }
32
33
    /**
34
     * Get the value of items
35
     *
36
     * @return mixed
37
     */
38
    public function getItems()
39
    {
40
        return $this->items;
41
    }
42
43
    /**
44
     * Set the value of items
45
     *
46
     * @param mixed $items
47
     *
48
     * @return self
49
     */
50
    public function setItems($items)
51
    {
52
        $this->items = $items;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Get the value of isInline
59
     *
60
     * @return bool
61
     */
62
    public function isInline(): bool
63
    {
64
        return $this->isInline;
65
    }
66
67
    /**
68
     * Set the value of isInline
69
     *
70
     * @param bool $isInline
71
     *
72
     * @return self
73
     */
74
    public function setIsInline(bool $isInline)
75
    {
76
        $this->isInline = $isInline;
77
78
        return $this;
79
    }
80
81
    /**
82
     *
83
     */
84
    protected function getItemsToRender(): array
85
    {
86
        return $this->selectionProvider
87
            ? $this->selectionProvider->getSelection()
88
            : $this->items;
89
    }
90
91
    /**
92
     *
93
     */
94
    protected function isItemSelected(string $item)
95
    {
96
        return $item === $this->value;
97
    }
98
99
    /**
100
     *
101
     */
102
    public function renderHtmlMarkup(): string
103
    {
104
        $html = '';
105
106
        foreach ($this->getItemsToRender() as $item => $label) {
107
108
            $radio = (new Radio())
109
                ->setChecked($this->isItemSelected($item))
110
                ->setValue($item)
111
                ->setName($this->name)
112
                ->setClasslist(explode(' ', $this->classlist->parse()));
113
114
            $html .= $this->createLabel($radio . $label, $this->labelOptions);
115
116
            $html .= !$this->isInline ? '<br>' : '';
117
        }
118
119
        return $html;
120
    }
121
}
122