Passed
Push — master ( a90def...5de986 )
by Chris
04:49
created

Select2::getWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace WebTheory\Saveyour\Fields;
4
5
use WebTheory\Html\AbstractHtmlElement;
6
use WebTheory\Saveyour\Contracts\FormFieldInterface;
7
8
class Select2 extends Select implements FormFieldInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $width;
14
15
    /**
16
     * @var string
17
     */
18
    protected $theme = 'default';
19
20
    /**
21
     *
22
     */
23
    protected $config = [];
24
25
    /**
26
     *
27
     */
28
    public const EXPECTED_CLASS = 'saveyour--select2';
29
30
    /**
31
     *
32
     */
33
    public const EXPECTED_DATA_KEY = 'data-saveyour__select2';
34
35
    /**
36
     *
37
     */
38
    public function __construct(array $config = [])
39
    {
40
        $this->config = $config + $this->config;
41
42
        parent::__construct();
43
    }
44
45
    /**
46
     * Get the value of config
47
     *
48
     * @return mixed
49
     */
50
    public function getConfig()
51
    {
52
        return $this->config;
53
    }
54
55
    /**
56
     * Get the value of width
57
     *
58
     * @return string
59
     */
60
    public function getWidth(): string
61
    {
62
        return $this->width;
63
    }
64
65
    /**
66
     * Set the value of width
67
     *
68
     * @param string $width
69
     *
70
     * @return self
71
     */
72
    public function setWidth(string $width)
73
    {
74
        $this->width = $width;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Get the value of theme
81
     *
82
     * @return string
83
     */
84
    public function getTheme(): string
85
    {
86
        return $this->theme;
87
    }
88
89
    /**
90
     * Set the value of theme
91
     *
92
     * @param string $theme
93
     *
94
     * @return self
95
     */
96
    public function setTheme(string $theme)
97
    {
98
        $this->theme = $theme;
99
100
        return $this;
101
    }
102
103
    /**
104
     *
105
     */
106
    protected function resolveAttributes(): AbstractHtmlElement
107
    {
108
        $this->addClass(static::EXPECTED_CLASS);
109
110
        return parent::resolveAttributes()
111
            ->addAttribute(static::EXPECTED_DATA_KEY, $this->getConfiguration());
0 ignored issues
show
Bug introduced by
static::EXPECTED_DATA_KEY of type string is incompatible with the type array expected by parameter $attribute of WebTheory\Html\AbstractHtmlElement::addAttribute(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
            ->addAttribute(/** @scrutinizer ignore-type */ static::EXPECTED_DATA_KEY, $this->getConfiguration());
Loading history...
112
    }
113
114
    /**
115
     *
116
     */
117
    protected function getConfiguration(): string
118
    {
119
        return json_encode($this->resolveConfiguration() + $this->config);
120
    }
121
122
    /**
123
     *
124
     */
125
    protected function resolveConfiguration(): array
126
    {
127
        return [
128
            'disabled' => $this->disabled,
129
            'multiple' => $this->multiple,
130
            'placeholder' => $this->placeholder,
131
            'theme' => $this->theme,
132
            'width' => $this->width
133
        ];
134
    }
135
}
136