Issues (138)

src/Utility/HasAttributesTrait.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\Form\Utility;
6
7
/**
8
 * Trait HasAttributesTrait
9
 * @package Nip\Form\Utility
10
 */
11
trait HasAttributesTrait
12
{
13
    protected $_attribs;
14
15
    /**
16
     * @return mixed
17
     */
18
    public function getId()
19
    {
20
        return $this->getAttrib('id');
21
    }
22
23
    /**
24
     * @param $id
25
     * @return $this
26
     */
27
    public function setId($id)
28
    {
29
        $this->setAttrib('id', $id);
30
31
        return $this;
32
    }
33
34
    /**
35 13
     * @return null
36
     */
37 13
    public function getName()
38
    {
39
        return $this->getAttrib('name');
40
    }
41
42
43
    /**
44
     * @param $name
45 12
     * @return $this
46
     */
47 12
    public function setName($name)
48
    {
49 12
        $this->setAttrib('name', $name);
50
51
        return $this;
52
    }
53
54
    /**
55 10
     * @return string
56
     */
57 10
    public function getLabel()
58
    {
59
        return $this->getAttrib('label');
60
    }
61
62
    /**
63
     * @param $label
64 10
     * @return $this
65
     */
66 10
    public function setLabel($label)
67
    {
68 10
        $this->setAttrib('label', $label);
69
70
        return $this;
71
    }
72
73
    /**
74
     * @param string $requester
75 5
     * @return null|mixed
76
     */
77 5
    public function getValue($requester = 'abstract')
0 ignored issues
show
The parameter $requester is not used and could be removed. ( Ignorable by Annotation )

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

77
    public function getValue(/** @scrutinizer ignore-unused */ $requester = 'abstract')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        return $this->getAttrib('value');
80
    }
81
82
    /**
83 5
     * @return bool
84
     */
85 5
    public function hasValue()
86
    {
87
        return !empty($this->getAttrib('value'));
88
    }
89
90
    /**
91
     * @param $value
92 5
     * @return static|self
93
     */
94 5
    public function setValue($value)
95
    {
96 5
        $this->setAttrib('value', $value);
97
98
        return $this;
99
    }
100
101
    /**
102 5
     * @return $this
103
     */
104 5
    public function addClass()
105 5
    {
106 5
        $classes = func_get_args();
107 5
        if (is_array($classes)) {
0 ignored issues
show
The condition is_array($classes) is always true.
Loading history...
108 5
            $oldClasses = $this->getAttrib('class');
109
            $oldClasses = empty($oldClasses) ? [] : explode(' ', $oldClasses);
110
            $classes = array_merge($classes, $oldClasses);
111 5
            $this->setAttrib('class', implode(' ', $classes));
112
        }
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return $this
119
     */
120
    public function removeClass()
121
    {
122
        $removeClasses = func_get_args();
123
        if (is_array($removeClasses)) {
0 ignored issues
show
The condition is_array($removeClasses) is always true.
Loading history...
124
            $classes = $this->getAttrib('class');
125
            $classes = empty($classes) ? [] : explode(' ', $classes);
126
            foreach ($removeClasses as $class) {
127
                $key = array_search($class, $classes);
128
                if ($key !== false) {
129
                    unset($classes[$key]);
130
                }
131
            }
132
            $this->setAttrib('class', implode(' ', $classes));
133
        }
134
135
        return $this;
136
    }
137
138
    /**
139 14
     * @param $key
140
     * @param $value
141 14
     * @return static
142 14
     */
143
    public function setDataAttrib($key, $value)
144 14
    {
145
        $key = 'data-' . (string)$key;
146
        $this->_attribs[$key] = $value;
147
148
        return $this;
149
    }
150
151 15
    /**
152
     * @param $key
153 15
     * @param $value
154 15
     * @return static
155 9
     */
156
    public function setAttrib($key, $value)
157
    {
158 12
        $key = (string)$key;
159
        $this->_attribs[$key] = $value;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @param string $key
166
     * @return null|mixed
167
     */
168
    public function getAttrib($key)
169
    {
170
        $key = (string)$key;
171
        if (!isset($this->_attribs[$key])) {
172
            return null;
173
        }
174
175
        return $this->_attribs[$key];
176 5
    }
177
178 5
    /**
179
     * @param string $key
180
     * @return bool
181
     */
182
    public function delAttrib($key)
183
    {
184
        $key = (string)$key;
185
        unset($this->_attribs[$key]);
186
187
        return true;
188
    }
189
190
    /**
191
     * @return mixed
192
     */
193
    public function getAttribs()
194
    {
195
        return $this->_attribs;
196
    }
197
198
    /**
199
     * @param array $attribs
200
     * @return static
201
     */
202
    public function setAttribs(array $attribs)
203
    {
204
        $this->clearAttribs();
205
206 3
        return $this->addAttribs($attribs);
207
    }
208 3
209
    /**
210
     * @return static
211
     */
212 3
    public function clearAttribs()
213
    {
214
        $this->_attribs = [];
215
216
        return $this;
217
    }
218
219
    /**
220
     * @param array $attribs
221
     * @return static
222
     */
223
    public function addAttribs(array $attribs)
224
    {
225
        foreach ($attribs as $key => $value) {
226
            $this->setAttrib($key, $value);
227
        }
228
229
        return $this;
230
    }
231
232
    /**
233
     * @param $key
234
     * @return bool
235
     */
236
    public function removeAttrib($key)
237
    {
238
        if (isset($this->_attribs[$key])) {
239
            unset($this->_attribs[$key]);
240
241
            return true;
242
        }
243
244
        return false;
245
    }
246
}
247