1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Form\Utility; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait HasAttributesTrait |
7
|
|
|
* @package Nip\Form\Utility |
8
|
|
|
*/ |
9
|
|
|
trait HasAttributesTrait |
10
|
|
|
{ |
11
|
|
|
protected $_attribs; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return mixed |
15
|
|
|
*/ |
16
|
|
|
public function getId() |
17
|
|
|
{ |
18
|
|
|
return $this->getAttrib('id'); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param $id |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
|
|
public function setId($id) |
26
|
|
|
{ |
27
|
|
|
$this->setAttrib('id', $id); |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return null |
34
|
|
|
*/ |
35
|
13 |
|
public function getName() |
36
|
|
|
{ |
37
|
13 |
|
return $this->getAttrib('name'); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $name |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
12 |
|
public function setName($name) |
46
|
|
|
{ |
47
|
12 |
|
$this->setAttrib('name', $name); |
48
|
|
|
|
49
|
12 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
10 |
|
public function getLabel() |
56
|
|
|
{ |
57
|
10 |
|
return $this->getAttrib('label'); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $label |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
10 |
|
public function setLabel($label) |
65
|
|
|
{ |
66
|
10 |
|
$this->setAttrib('label', $label); |
67
|
|
|
|
68
|
10 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $requester |
73
|
|
|
* @return null |
74
|
|
|
*/ |
75
|
5 |
|
public function getValue($requester = 'abstract') |
|
|
|
|
76
|
|
|
{ |
77
|
5 |
|
return $this->getAttrib('value'); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
5 |
|
public function hasValue() |
84
|
|
|
{ |
85
|
5 |
|
return !empty($this->getAttrib('value')); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $value |
90
|
|
|
* @return static|self |
91
|
|
|
*/ |
92
|
5 |
|
public function setValue($value) |
93
|
|
|
{ |
94
|
5 |
|
$this->setAttrib('value', $value); |
95
|
|
|
|
96
|
5 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
5 |
|
public function addClass() |
103
|
|
|
{ |
104
|
5 |
|
$classes = func_get_args(); |
105
|
5 |
|
if (is_array($classes)) { |
|
|
|
|
106
|
5 |
|
$oldClasses = explode(' ', $this->getAttrib('class')); |
|
|
|
|
107
|
5 |
|
$classes = array_merge($classes, $oldClasses); |
108
|
5 |
|
$this->setAttrib('class', implode(' ', $classes)); |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function removeClass() |
118
|
|
|
{ |
119
|
|
|
$removeClasses = func_get_args(); |
120
|
|
|
if (is_array($removeClasses)) { |
|
|
|
|
121
|
|
|
$classes = explode(' ', $this->getAttrib('class')); |
|
|
|
|
122
|
|
|
foreach ($removeClasses as $class) { |
123
|
|
|
$key = array_search($class, $classes); |
124
|
|
|
if ($key !== false) { |
125
|
|
|
unset($classes[$key]); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
$this->setAttrib('class', implode(' ', $classes)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $key |
136
|
|
|
* @param $value |
137
|
|
|
* @return static |
138
|
|
|
*/ |
139
|
14 |
|
public function setDataAttrib($key, $value) |
140
|
|
|
{ |
141
|
14 |
|
$key = 'data-' . (string)$key; |
142
|
14 |
|
$this->_attribs[$key] = $value; |
143
|
|
|
|
144
|
14 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $key |
149
|
|
|
* @param $value |
150
|
|
|
* @return static |
151
|
15 |
|
*/ |
152
|
|
|
public function setAttrib($key, $value) |
153
|
15 |
|
{ |
154
|
15 |
|
$key = (string)$key; |
155
|
9 |
|
$this->_attribs[$key] = $value; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
12 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $key |
162
|
|
|
* @return null |
163
|
|
|
*/ |
164
|
|
|
public function getAttrib($key) |
165
|
|
|
{ |
166
|
|
|
$key = (string)$key; |
167
|
|
|
if (!isset($this->_attribs[$key])) { |
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->_attribs[$key]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $key |
176
|
5 |
|
* @return bool |
177
|
|
|
*/ |
178
|
5 |
|
public function delAttrib($key) |
179
|
|
|
{ |
180
|
|
|
$key = (string)$key; |
181
|
|
|
unset($this->_attribs[$key]); |
182
|
|
|
|
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public function getAttribs() |
190
|
|
|
{ |
191
|
|
|
return $this->_attribs; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $attribs |
196
|
|
|
* @return static |
197
|
|
|
*/ |
198
|
|
|
public function setAttribs(array $attribs) |
199
|
|
|
{ |
200
|
|
|
$this->clearAttribs(); |
201
|
|
|
|
202
|
|
|
return $this->addAttribs($attribs); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
3 |
|
* @return static |
207
|
|
|
*/ |
208
|
3 |
|
public function clearAttribs() |
209
|
|
|
{ |
210
|
|
|
$this->_attribs = []; |
211
|
|
|
|
212
|
3 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param array $attribs |
217
|
|
|
* @return static |
218
|
|
|
*/ |
219
|
|
|
public function addAttribs(array $attribs) |
220
|
|
|
{ |
221
|
|
|
foreach ($attribs as $key => $value) { |
222
|
|
|
$this->setAttrib($key, $value); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param $key |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function removeAttrib($key) |
233
|
|
|
{ |
234
|
|
|
if (isset($this->_attribs[$key])) { |
235
|
|
|
unset($this->_attribs[$key]); |
236
|
|
|
|
237
|
|
|
return true; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.