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
|
6 |
|
public function getName() |
36
|
|
|
{ |
37
|
6 |
|
return $this->getAttrib('name'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $name |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
5 |
|
public function setName($name) |
46
|
|
|
{ |
47
|
5 |
|
$this->setAttrib('name', $name); |
48
|
|
|
|
49
|
5 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
5 |
|
public function getLabel() |
56
|
|
|
{ |
57
|
5 |
|
return $this->getAttrib('label'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $label |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
5 |
|
public function setLabel($label) |
65
|
|
|
{ |
66
|
5 |
|
$this->setAttrib('label', $label); |
67
|
|
|
|
68
|
5 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $requester |
73
|
|
|
* @return null |
74
|
|
|
*/ |
75
|
4 |
|
public function getValue($requester = 'abstract') |
|
|
|
|
76
|
|
|
{ |
77
|
4 |
|
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
|
4 |
|
public function setValue($value) |
93
|
|
|
{ |
94
|
4 |
|
$this->setAttrib('value', $value); |
95
|
|
|
|
96
|
4 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
3 |
View Code Duplication |
public function addClass() |
|
|
|
|
103
|
|
|
{ |
104
|
3 |
|
$classes = func_get_args(); |
105
|
3 |
|
if (is_array($classes)) { |
106
|
3 |
|
$oldClasses = explode(' ', $this->getAttrib('class')); |
107
|
3 |
|
$classes = array_merge($classes, $oldClasses); |
108
|
3 |
|
$this->setAttrib('class', implode(' ', $classes)); |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
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
|
6 |
|
public function setAttrib($key, $value) |
140
|
|
|
{ |
141
|
6 |
|
$key = (string)$key; |
142
|
6 |
|
$this->_attribs[$key] = $value; |
143
|
|
|
|
144
|
6 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $key |
149
|
|
|
* @return null |
150
|
|
|
*/ |
151
|
8 |
View Code Duplication |
public function getAttrib($key) |
|
|
|
|
152
|
|
|
{ |
153
|
8 |
|
$key = (string)$key; |
154
|
8 |
|
if (!isset($this->_attribs[$key])) { |
155
|
5 |
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
6 |
|
return $this->_attribs[$key]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $key |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function delAttrib($key) |
166
|
|
|
{ |
167
|
|
|
$key = (string)$key; |
168
|
|
|
unset($this->_attribs[$key]); |
169
|
|
|
|
170
|
|
|
return true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
3 |
|
public function getAttribs() |
177
|
|
|
{ |
178
|
3 |
|
return $this->_attribs; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param array $attribs |
183
|
|
|
* @return static |
184
|
|
|
*/ |
185
|
|
|
public function setAttribs(array $attribs) |
186
|
|
|
{ |
187
|
|
|
$this->clearAttribs(); |
188
|
|
|
|
189
|
|
|
return $this->addAttribs($attribs); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return static |
194
|
|
|
*/ |
195
|
|
|
public function clearAttribs() |
196
|
|
|
{ |
197
|
|
|
$this->_attribs = []; |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param array $attribs |
204
|
|
|
* @return static |
205
|
|
|
*/ |
206
|
3 |
|
public function addAttribs(array $attribs) |
207
|
|
|
{ |
208
|
3 |
|
foreach ($attribs as $key => $value) { |
209
|
|
|
$this->setAttrib($key, $value); |
210
|
|
|
} |
211
|
|
|
|
212
|
3 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param $key |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
View Code Duplication |
public function removeAttrib($key) |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
if (isset($this->_attribs[$key])) { |
222
|
|
|
unset($this->_attribs[$key]); |
223
|
|
|
|
224
|
|
|
return true; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.