Passed
Push — develop ( f6f5f6...710bfb )
by Paul
14:13
created

Transformer::transformText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Flatsome;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Integrations\Flatsome\Defaults\ControlDefaults;
7
8
class Transformer
9
{
10
    public string $name;
11
    public string $shortcode;
12
13
    protected array $control;
14
15
    public function __construct(string $name, array $control, string $shortcode = '')
16
    {
17
        $this->name = $name;
18
        $this->shortcode = $shortcode;
19
        $control = wp_parse_args(compact('name'), $control);
20
        $control = glsr(ControlDefaults::class)->merge($control);
21
        $method = Helper::buildMethodName('transform', $control['type']);
22
        $this->control = method_exists($this, $method)
23
            ? call_user_func([$this, $method], $control)
24
            : $control;
25
    }
26
27
    public function control(): array
28
    {
29
        return $this->control;
30
    }
31
32
    protected function transformCheckbox(array $control): array
33
    {
34
        if ('hide' === $this->name) {
35
            $control['description'] = esc_html_x('Flatsome does not support multiple checkboxes here so use the dropdown to select fields that you want to hide.', 'admin-text', 'site-reviews');
36
        }
37
        if (!empty($control['options'])) {
38
            $control['config'] = [
39
                'multiple' => true,
40
                'options' => $control['options'],
41
                'placeholder' => $control['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews'),
42
                'sortable' => false,
43
            ];
44
            $control['type'] = 'select';
45
            unset($control['options']);
46
            return $control;
47
        }
48
        if (!isset($control['options'])) {
49
            $control['options'] = [
50
                '' => ['title' => _x('No', 'admin-text', 'site-reviews')],
51
                'true' => ['title' => _x('Yes', 'admin-text', 'site-reviews')],
52
            ];
53
            $control['type'] = 'radio-buttons';
54
        }
55
        return $control;
56
    }
57
58
    protected function transformNumber(array $control): array
59
    {
60
        $control['type'] = 'slider';
61
        return $control;
62
    }
63
64
    protected function transformSelect(array $control): array
65
    {
66
        $control['config'] = [
67
            'sortable' => false,
68
        ];
69
        if (!empty($control['options'])) {
70
            if (!isset($control['options'][''])) {
71
                $placeholder = $control['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews');
72
                $control['options'] = ['' => $placeholder] + $control['options'];
73
            }
74
            return $control;
75
        }
76
        if (!isset($control['options'])) {
77
            if ('assigned_terms' === $this->name) {
78
                $control['config']['termSelect'] = [
79
                    'taxonomies' => glsr()->taxonomy,
80
                ];
81
            } else {
82
                $control['config']['postSelect'] = glsr()->prefix.$this->name;
83
            }
84
            if (str_starts_with($this->name, 'assigned_')) {
85
                $control['multiple'] = true;
86
            }
87
            $control['config']['multiple'] = $control['multiple'] ?? false;
88
            $control['config']['placeholder'] = $control['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews');
89
            unset($control['multiple']);
90
            unset($control['placeholder']);
91
            return $control;
92
        }
93
        return []; // invalid select control
94
    }
95
96
    protected function transformText(array $control): array
97
    {
98
        $control['type'] = 'textfield';
99
        return $control;
100
    }
101
}
102