Passed
Push — develop ( c34e50...445d6d )
by Paul
06:42
created

Checkbox::buildInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2.0002

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 28
ccs 25
cts 26
cp 0.9615
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.0002
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\FieldElements;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Modules\Style;
10
11
class Checkbox extends AbstractFieldElement
12
{
13 30
    public function defaults(): array
14
    {
15 30
        return [
16 30
            'checked' => false,
17 30
        ];
18
    }
19
20 9
    public function inputType(): string
21
    {
22 9
        return in_array($this->field->original_type, ['checkbox', 'radio'])
23 5
            ? $this->field->type
24 9
            : $this->field->original_type;
25
    }
26
27 9
    public function required(): array
28
    {
29 9
        return [
30 9
            'type' => 'checkbox',
31 9
        ];
32
    }
33
34 30
    public function tag(): string
35
    {
36 30
        return 'input';
37
    }
38
39 9
    protected function buildInput(string $value, int $index, Arguments $args): string
40
    {
41 9
        $type = $this->inputType();
42 9
        $input = [
43 9
            'checked' => in_array($value, Cast::toArray($args->value)),
44 9
            'class' => $args->class,
45 9
            'disabled' => $args->disabled,
46 9
            'id' => Helper::ifTrue(!empty($args->id), "{$args->id}-{$index}"),
47 9
            'name' => $args->name,
48 9
            'required' => $args->required,
49 9
            'tabindex' => $args->tabindex,
50 9
            'type' => $args->type,
51 9
            'value' => $value,
52 9
        ];
53 9
        if (!empty($args['data-conditions'])) {
54
            $input['data-conditions'] = $args['data-conditions'];
55
        }
56 9
        $html = glsr(Template::class)->build("templates/form/type-{$type}", [
57 9
            'context' => [
58 9
                'class' => glsr(Style::class)->defaultClasses('field')."-{$type}", // only use the default class here!
59 9
                'id' => $input['id'],
60 9
                'input' => $this->field->builder()->input($input),
61 9
                'text' => $args->options[$value],
62 9
            ],
63 9
            'field' => $this->field,
64 9
            'input' => $input,
65 9
        ]);
66 9
        return glsr()->filterString('rendered/field', $html, $type, $input);
67
    }
68
69 9
    protected function buildReviewField(Arguments $args): string
70
    {
71 9
        $index = 0;
72 9
        $optionKeys = array_keys($args->options);
73 9
        return array_reduce($optionKeys, function ($carry, $value) use (&$index, $args) {
74 9
            return $carry.$this->buildInput((string) $value, ++$index, $args);
75 9
        }, '');
76
    }
77
78 28
    protected function normalizeOptions(): void
79
    {
80 28
        if (empty($this->field->options)) {
81 18
            $label = $this->field->label ?: $this->field->text;
82 18
            $value = $this->field->value ?: 1;
83 18
            $this->field->label = ''; // clear the label
84 18
            $this->field->text = ''; // clear the text
85 18
            $this->field->options = [$value => $label];
86 18
            return;
87
        }
88 10
        $options = [];
89 10
        foreach ($this->field->options as $key => $values) {
90 10
            if (!is_array($values)) {
91 9
                $options[$key] = $values;
92 9
                continue;
93
            }
94 1
            $values = array_slice(array_filter($values, 'is_string'), 0, 2);
95 1
            if (1 === count($values)) {
96
                $options[$key] = Cast::toString($values);
97 1
            } elseif (2 === count($values)) {
98 1
                $values = array_reduce($values, fn ($carry, $val) => $carry.$this->field->builder()->span($val), '');
99 1
                $options[$key] = $values;
100
            }
101
        }
102 10
        $this->field->options = $options;
103
    }
104
105 30
    protected function normalizeValue(): void
106
    {
107 30
        if (!$this->field->isMultiField()) {
108 22
            return;
109
        }
110 8
        if ('' === $this->field->value && $this->field->checked) {
111
            $this->field->value = array_keys($this->field->options); // all options are checked
112
            return;
113
        }
114 8
        $this->field->value = Cast::toArray($this->field->value); // cast value to array as the field accepts multiple values
115
    }
116
}
117