Select2   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 105
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getTheme() 0 3 1
A __construct() 0 5 1
A getConfig() 0 3 1
A getWidth() 0 3 1
A setTheme() 0 5 1
A setWidth() 0 5 1
A resolveAttributes() 0 7 1
A getConfiguration() 0 5 1
A resolveConfiguration() 0 8 1
1
<?php
2
3
namespace WebTheory\Saveyour\Field\Type;
4
5
use WebTheory\Saveyour\Contracts\Field\FormFieldInterface;
6
7
class Select2 extends Select implements FormFieldInterface
8
{
9
    protected ?string $width = null;
10
11
    protected string $theme = 'default';
12
13
    protected array $config = [];
14
15
    public const EXPECTED_CLASS = 'saveyour--select2';
16
17
    public const EXPECTED_DATA_KEY = 'data-saveyour__select2';
18
19
    public function __construct(array $config = [])
20
    {
21
        $this->config = $config + $this->config;
22
23
        parent::__construct();
24
    }
25
26
    /**
27
     * Get the value of config
28
     *
29
     * @return mixed
30
     */
31
    public function getConfig()
32
    {
33
        return $this->config;
34
    }
35
36
    /**
37
     * Get the value of width
38
     *
39
     * @return string
40
     */
41
    public function getWidth(): string
42
    {
43
        return $this->width;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->width could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
44
    }
45
46
    /**
47
     * Set the value of width
48
     *
49
     * @param string $width
50
     *
51
     * @return $this
52
     */
53
    public function setWidth(string $width): Select2
54
    {
55
        $this->width = $width;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get the value of theme
62
     *
63
     * @return string
64
     */
65
    public function getTheme(): string
66
    {
67
        return $this->theme;
68
    }
69
70
    /**
71
     * Set the value of theme
72
     *
73
     * @param string $theme
74
     *
75
     * @return $this
76
     */
77
    public function setTheme(string $theme): Select2
78
    {
79
        $this->theme = $theme;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @return $this
86
     */
87
    protected function resolveAttributes(): Select2
88
    {
89
        parent::resolveAttributes()
90
            ->addAttribute(static::EXPECTED_DATA_KEY, $this->getConfiguration())
91
            ->addClass(static::EXPECTED_CLASS);
92
93
        return $this;
94
    }
95
96
    protected function getConfiguration(): string
97
    {
98
        return json_encode(
99
            $this->resolveConfiguration() + $this->config,
100
            JSON_THROW_ON_ERROR
101
        );
102
    }
103
104
    protected function resolveConfiguration(): array
105
    {
106
        return [
107
            'disabled' => $this->disabled,
108
            'multiple' => $this->multiple,
109
            'placeholder' => $this->placeholder,
110
            'theme' => $this->theme,
111
            'width' => $this->width,
112
        ];
113
    }
114
}
115