Test Failed
Push — develop ( 6f947a...7aea1a )
by Paul
18:36
created

Checkbox::buildReviewField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
ccs 6
cts 6
cp 1
cc 1
nc 1
nop 1
crap 1
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 32
    public function defaults(): array
14
    {
15 32
        return [
16 32
            'checked' => false,
17 32
        ];
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 32
    public function tag(): string
35
    {
36 32
        return 'input';
37
    }
38
39 7
    protected function buildInput(array $args): string
40
    {
41 7
        return $this->field->builder()->span([
42 7
            'class' => "glsr-{$this->inputType()}",
43 7
            'text' => $this->field->builder()->input($args),
44 7
        ]);
45
    }
46
47 9
    protected function buildOption(string $value, int $index, Arguments $args): string
48
    {
49 9
        $type = $this->inputType();
50 9
        $input = [
51 9
            'checked' => in_array($value, Cast::toArray($args->value)),
52 9
            'class' => $args->class,
53 9
            'disabled' => $args->disabled,
54 9
            'id' => Helper::ifTrue(!empty($args->id), "{$args->id}-{$index}"),
55 9
            'name' => $args->name,
56 9
            'required' => $args->required,
57 9
            'tabindex' => $args->tabindex,
58 9
            'type' => $args->type,
59 9
            'value' => $value,
60 9
        ];
61 9
        if (!empty($args['data-conditions'])) {
62
            $input['data-conditions'] = $args['data-conditions'];
63
        }
64 9
        $html = glsr(Template::class)->build("templates/form/type-{$type}", [
65 9
            'context' => [
66 9
                'class' => glsr(Style::class)->defaultClasses('field')."-{$type}", // only use the default class here!
67 9
                'id' => $input['id'],
68 9
                'input' => $this->buildInput($input),
69 9
                'text' => $args->options[$value],
70 9
            ],
71 9
            'field' => $this->field,
72 9
            'input' => $input,
73 9
        ]);
74 9
        return glsr()->filterString('rendered/field', $html, $type, $input);
75
    }
76
77 9
    protected function buildReviewField(Arguments $args): string
78
    {
79 9
        $index = 0;
80 9
        $optionKeys = array_keys($args->options);
81 9
        return array_reduce($optionKeys, function ($carry, $value) use (&$index, $args) {
82 9
            return $carry.$this->buildOption((string) $value, ++$index, $args);
83 9
        }, '');
84
    }
85
86 30
    protected function normalizeOptions(): void
87
    {
88 30
        if (empty($this->field->options)) {
89 20
            $label = $this->field->label ?: $this->field->text;
90 20
            $value = $this->field->value ?: 1;
91 20
            $this->field->label = ''; // clear the label
92 20
            $this->field->text = ''; // clear the text
93 20
            $this->field->options = [$value => $label];
94 20
            return;
95
        }
96 10
        $options = [];
97 10
        foreach ($this->field->options as $key => $values) {
98 10
            if (!is_array($values)) {
99 9
                $options[$key] = $values;
100 9
                continue;
101
            }
102 1
            $values = array_slice(array_filter($values, 'is_string'), 0, 2);
103 1
            if (1 === count($values)) {
104
                $options[$key] = Cast::toString($values);
105 1
            } elseif (2 === count($values)) {
106
                // this allows individual input label descriptions
107 1
                $level = 0;
108 1
                $values = array_reduce($values, function ($carry, $text) use (&$level) {
109 1
                    return $carry . $this->field->builder()->span([
110 1
                        'data-type' => $level++ ? 'description' : 'label',
111 1
                        'text' => $text,
112 1
                    ]);
113 1
                }, '');
114 1
                $options[$key] = $values;
115
            }
116
        }
117 10
        $this->field->options = $options;
118
    }
119
120 32
    protected function normalizeValue(): void
121
    {
122 32
        if (!$this->field->isMultiField()) {
123 24
            return;
124
        }
125 8
        if ('' === $this->field->value && $this->field->checked) {
126
            $this->field->value = array_keys($this->field->options); // all options are checked
127
            return;
128
        }
129 8
        $this->field->value = Cast::toArray($this->field->value); // cast value to array as the field accepts multiple values
130
    }
131
}
132