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

ReviewField   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 32
eloc 104
dl 0
loc 190
ccs 114
cts 132
cp 0.8636
rs 9.84
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFieldLabel() 0 12 3
A buildFieldElement() 0 5 1
A buildFieldErrors() 0 8 1
A normalizeValidation() 0 9 3
A normalize() 0 6 1
A classAttrDescription() 0 8 1
A classAttrField() 0 24 5
A normalizeDataConditions() 0 13 3
A buildField() 0 19 2
A classAttrElement() 0 16 4
A normalizeRequired() 0 9 3
A classAttrLabel() 0 8 1
A buildFieldDescription() 0 11 2
A location() 0 3 1
A classAttrErrors() 0 4 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
use GeminiLabs\SiteReviews\Modules\Sanitizer;
8
use GeminiLabs\SiteReviews\Modules\Style;
9
10
class ReviewField extends Field
11
{
12 21
    public function buildField(): string
13
    {
14 21
        if ($this->is_raw) {
15 1
            return $this->builder()->build($this->tag(), $this->toArray());
16
        }
17 20
        $field = glsr(Template::class)->build("templates/form/field_{$this->original_type}", [
18 20
            'context' => [
19 20
                'class' => $this->classAttrField(),
20 20
                'description' => $this->buildFieldDescription(),
21 20
                'errors' => $this->buildFieldErrors(),
22 20
                'field' => $this->buildFieldElement().$this->after,
23 20
                'field_label' => esc_attr($this->label),
24 20
                'field_name' => $this->original_name,
25 20
                'field_type' => $this->original_type,
26 20
                'label' => $this->buildFieldLabel(),
27 20
            ],
28 20
            'field' => $this,
29 20
        ]);
30 20
        return glsr()->filterString('rendered/field', $field, $this->original_type, $this);
31
    }
32
33 20
    public function buildFieldDescription(): string
34
    {
35 20
        if (empty($this->description)) {
36 20
            return '';
37
        }
38
        return glsr(Template::class)->build('templates/form/field-description', [
39
            'context' => [
40
                'class' => $this->classAttrDescription(),
41
                'description' => $this->description,
42
            ],
43
            'field' => $this,
44
        ]);
45
    }
46
47 20
    public function buildFieldElement(): string
48
    {
49 20
        return $this->fieldElement()->build([
50 20
            'class' => $this->classAttrElement(), // merge the styled class attribute
51 20
            'label' => '', // prevent the field label from being built
52 20
        ]);
53
    }
54
55 20
    public function buildFieldErrors(): string
56
    {
57 20
        return glsr(Template::class)->build('templates/form/field-errors', [
58 20
            'context' => [
59 20
                'class' => $this->classAttrErrors(),
60 20
                'errors' => implode('<br>', Arr::consolidate($this->errors)), // because <br> is used in validation.js
61 20
            ],
62 20
            'field' => $this,
63 20
        ]);
64
    }
65
66 20
    public function buildFieldLabel(): string
67
    {
68 20
        if (empty($this->label)) {
69 3
            return '';
70
        }
71 17
        return glsr(Template::class)->build('templates/form/field-label', [
72 17
            'context' => [
73 17
                'class' => $this->classAttrLabel(),
74 17
                'for' => !$this->isChoiceField() ? $this->id : '',
75 17
                'text' => $this->label,
76 17
            ],
77 17
            'field' => $this,
78 17
        ]);
79
    }
80
81 34
    public function location(): string
82
    {
83 34
        return 'review';
84
    }
85
86
    protected function classAttrDescription(): string
87
    {
88
        $classes = [
89
            glsr(Style::class)->classes('description'),
90
            glsr(Style::class)->defaultClasses('description'),
91
        ];
92
        $classes = implode(' ', $classes);
93
        return glsr(Sanitizer::class)->sanitizeAttrClass($classes);
94
    }
95
96 20
    protected function classAttrElement(): string
97
    {
98 20
        $classes = [
99 20
            glsr(Style::class)->fieldClass($this),
100 20
        ];
101 20
        if ('yes_no' === $this->original_type) {
102 1
            $classes[] = "glsr-input-{$this->type}";
103 19
        } elseif ($this->isChoiceField()) {
104 9
            $classes[] = "glsr-input-{$this->original_type}";
105 10
        } elseif (glsr(Attributes::class)->isInputType($this->type)) {
106 6
            $classes[] = "glsr-input glsr-input-{$this->type}";
107
        } else {
108 4
            $classes[] = "glsr-{$this->tag()}";
109
        }
110 20
        $classes = implode(' ', $classes);
111 20
        return glsr(Sanitizer::class)->sanitizeAttrClass($classes);
112
    }
113
114 20
    protected function classAttrErrors(): string
115
    {
116 20
        return glsr(Sanitizer::class)->sanitizeAttrClass(
117 20
            glsr(Style::class)->validation('field_message')
118 20
        );
119
    }
120
121 20
    protected function classAttrField(): string
122
    {
123 20
        $suffix = $this->isChoiceField() ? '-choice' : "-{$this->original_type}";
124 20
        $classes = [
125 20
            glsr(Style::class)->classes('field'),
126 20
            glsr(Style::class)->defaultClasses('field'),
127 20
            Str::suffix(glsr(Style::class)->defaultClasses('field'), $suffix),
128 20
        ];
129 20
        if (!empty($this->errors)) {
130
            $classes[] = glsr(Style::class)->validation('field_error');
131
        }
132 20
        if ($this->is_hidden) {
133
            $classes[] = glsr(Style::class)->validation('field_hidden');
134
        }
135 20
        if (!empty($this->required)) {
136 1
            $classes[] = glsr(Style::class)->validation('field_required');
137
        }
138 20
        $classes = implode(' ', $classes);
139 20
        $classes = explode(' ', $classes);
140 20
        $classes = array_values(array_filter(array_unique($classes)));
141 20
        $classes = glsr()->filterArrayUnique('rendered/field/classes', $classes, $this);
142 20
        $classes = implode(' ', $classes);
143 20
        $classes = glsr(Sanitizer::class)->sanitizeAttrClass($classes);
144 20
        return $classes;
145
    }
146
147 17
    protected function classAttrLabel(): string
148
    {
149 17
        $classes = [
150 17
            glsr(Style::class)->classes('label'),
151 17
            glsr(Style::class)->defaultClasses('label'),
152 17
        ];
153 17
        $classes = implode(' ', $classes);
154 17
        return glsr(Sanitizer::class)->sanitizeAttrClass($classes);
155
    }
156
157 36
    protected function normalize(): void
158
    {
159 36
        parent::normalize();
160 36
        $this->normalizeDataConditions();
161 36
        $this->normalizeRequired(); // do this before normalizing validation
162 36
        $this->normalizeValidation();
163
    }
164
165 36
    protected function normalizeDataConditions(): void
166
    {
167 36
        $conditions = wp_parse_args($this->conditions(), [
168 36
            'criteria' => '',
169 36
            'conditions' => [],
170 36
        ]);
171 36
        if (in_array($conditions['criteria'], ['', 'always'])) {
172 36
            return;
173
        }
174 2
        if (empty($conditions['conditions'])) {
175
            return;
176
        }
177 2
        $this['data-conditions'] = wp_json_encode($conditions);
178
    }
179
180 36
    protected function normalizeRequired(): void
181
    {
182 36
        if ($this->is_custom) {
183
            return; // don't modify the required attribute in custom fields
184
        }
185 36
        if (!in_array($this->original_name, glsr_get_option('forms.required', [], 'array'))) {
186 36
            return;
187
        }
188 12
        $this->required = true;
189
    }
190
191 36
    protected function normalizeValidation(): void
192
    {
193 36
        $rules = explode('|', $this->validation);
194 36
        $rules = array_filter(array_diff($rules, ['accepted', 'required']));
195 36
        if ($this->required) {
196 15
            $rule = 'terms' === $this->original_name ? 'accepted' : 'required';
197 15
            array_unshift($rules, $rule);
198
        }
199 36
        $this->validation = implode('|', $rules);
200
    }
201
}
202