Test Failed
Push — develop ( 4ba53a...c20cce )
by Paul
10:12
created

Transformer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\FusionBuilder;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Integrations\FusionBuilder\Defaults\ControlDefaults;
7
8
class Transformer
9
{
10
    public array $args;
11
    public string $name;
12
    public string $shortcode;
13
14
    public function __construct(string $name, array $args, string $shortcode = '')
15
    {
16
        $args = glsr(ControlDefaults::class)->merge(
17
            wp_parse_args(compact('name'), $args)
18
        );
19
        $this->args = $args;
20
        $this->name = $name;
21
        $this->shortcode = $shortcode;
22
        $method = Helper::buildMethodName('transform', $this->args['type']);
23
        if (method_exists($this, $method)) {
24
            call_user_func([$this, $method]);
25
        }
26
    }
27
28
    public function control(): array
29
    {
30
        $groups = [ // order is intentional
31
            'design' => esc_html_x('Design', 'admin-text', 'site-reviews'),
32
            'general' => esc_html_x('General', 'admin-text', 'site-reviews'),
33
            'advanced' => esc_html_x('Advanced', 'admin-text', 'site-reviews'),
34
        ];
35
        $control = $this->args;
36
        $control['group'] = $groups[$this->args['group']] ?? ucfirst($this->args['group']);
37
        return $control;
38
    }
39
40
    public function transformCheckbox(): void
41
    {
42
        if (!empty($this->args['options'])) {
43
            if ('hide' === $this->name) {
44
                $this->args['heading'] = esc_html_x('Hide', 'admin-text', 'site-reviews');
45
            }
46
            $placeholder = $this->args['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews');
47
            $this->args['placeholder'] = $placeholder;
48
            $this->args['type'] = 'multiple_select';
49
            $this->args['value'] = $this->args['options'];
50
        } else {
51
            $this->args['type'] = 'radio_button_set';
52
            $this->args['value'] = [
53
                0 => esc_html_x('No', 'admin-text', 'site-reviews'),
54
                1 => esc_html_x('Yes', 'admin-text', 'site-reviews'),
55
            ];
56
        }
57
    }
58
59
    public function transformNumber(): void
60
    {
61
        $this->args['type'] = 'range';
62
    }
63
64
    public function transformSelect(): void
65
    {
66
        if (!empty($this->args['options'])) {
67
            $options = $this->args['options'];
68
            if (!array_key_exists('', $options)) {
69
                $placeholder = $this->args['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews');
70
                $options = ['' => $placeholder] + $options;
71
            }
72
            $this->args['value'] = $options;
73
        } elseif (!isset($this->args['options'])) {
74
            $this->args['ajax'] = glsr()->prefix.'fusion_search_query';
75
            $this->args['ajax_params'] = [
76
                'option' => $this->name,
77
                'shortcode' => $this->shortcode,
78
            ];
79
            $this->args['type'] = 'ajax_select';
80
        }
81
    }
82
}
83