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

AbstractChecklist   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 64
ccs 0
cts 22
cp 0
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getItems() 0 3 1
A isItemChecked() 0 3 1
A createItemLabel() 0 3 1
A getItemsToRender() 0 5 2
A setItems() 0 5 1
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
use WebTheory\Saveyour\Elements\Label;
9
10
abstract class AbstractChecklist extends AbstractFormField implements FormFieldInterface
11
{
12
    use HasLabelsTrait;
13
    use IsSelectionFieldTrait;
14
15
    /**
16
     * Associative array of item definitions with the value as the key
17
     *
18
     * @var array
19
     */
20
    protected $items = [];
21
22
    /**
23
     * @var string[]
24
     */
25
    protected $value = [];
26
27
    /**
28
     * Get the value of items
29
     *
30
     * @return mixed
31
     */
32
    public function getItems()
33
    {
34
        return $this->items;
35
    }
36
37
    /**
38
     * Set the value of items
39
     *
40
     * @param mixed $items
41
     *
42
     * @return self
43
     */
44
    public function setItems($items)
45
    {
46
        $this->items = $items;
47
48
        return $this;
49
    }
50
51
    /**
52
     *
53
     */
54
    protected function getItemsToRender(): array
55
    {
56
        return $this->selectionProvider
57
            ? $this->selectionProvider->getSelection()
58
            : $this->items;
59
    }
60
    /**
61
     *
62
     */
63
    protected function isItemChecked(string $item): bool
64
    {
65
        return in_array($item, $this->value);
66
    }
67
68
    /**
69
     *
70
     */
71
    protected function createItemLabel(array $values): Label
72
    {
73
        return $this->createLabel($values['label'] ?? '', $this->labelOptions);
74
    }
75
}
76