1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Form\Elements\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait HasAttributesTrait |
7
|
|
|
* @package Nip\Form\Elements\Traits |
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
|
1 |
|
public function getName() |
36
|
|
|
{ |
37
|
1 |
|
return $this->getAttrib('name'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $name |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
1 |
|
public function setName($name) |
46
|
|
|
{ |
47
|
1 |
|
$this->setAttrib('name', $name); |
48
|
|
|
|
49
|
1 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
1 |
|
public function getLabel() |
56
|
|
|
{ |
57
|
1 |
|
return $this->getAttrib('label'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $label |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
1 |
|
public function setLabel($label) |
65
|
|
|
{ |
66
|
1 |
|
$this->setAttrib('label', $label); |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $requester |
73
|
|
|
* @return null |
74
|
|
|
*/ |
75
|
1 |
|
public function getValue($requester = 'abstract') |
|
|
|
|
76
|
|
|
{ |
77
|
1 |
|
return $this->getAttrib('value'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $value |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
1 |
|
public function setValue($value) |
85
|
|
|
{ |
86
|
1 |
|
$this->setAttrib('value', $value); |
87
|
|
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function addClass() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$classes = func_get_args(); |
97
|
|
|
if (is_array($classes)) { |
98
|
|
|
$oldClasses = explode(' ', $this->getAttrib('class')); |
99
|
|
|
$classes = array_merge($classes, $oldClasses); |
100
|
|
|
$this->setAttrib('class', implode(' ', $classes)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function removeClass() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$removeClasses = func_get_args(); |
112
|
|
|
if (is_array($removeClasses)) { |
113
|
|
|
$classes = explode(' ', $this->getAttrib('class')); |
114
|
|
|
foreach ($removeClasses as $class) { |
115
|
|
|
$key = array_search($class, $classes); |
116
|
|
|
if ($key !== false) { |
117
|
|
|
unset($classes[$key]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
$this->setAttrib('class', implode(' ', $classes)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $key |
128
|
|
|
* @param $value |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
2 |
|
public function setAttrib($key, $value) |
132
|
|
|
{ |
133
|
2 |
|
$key = (string)$key; |
134
|
2 |
|
$this->_attribs[$key] = $value; |
135
|
|
|
|
136
|
2 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $key |
141
|
|
|
* @return null |
142
|
|
|
*/ |
143
|
2 |
View Code Duplication |
public function getAttrib($key) |
|
|
|
|
144
|
|
|
{ |
145
|
2 |
|
$key = (string)$key; |
146
|
2 |
|
if (!isset($this->_attribs[$key])) { |
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
return $this->_attribs[$key]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $key |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function delAttrib($key) |
158
|
|
|
{ |
159
|
|
|
$key = (string)$key; |
160
|
|
|
unset($this->_attribs[$key]); |
161
|
|
|
|
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
public function getAttribs() |
169
|
|
|
{ |
170
|
|
|
return $this->_attribs; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param array $attribs |
175
|
|
|
* @return static |
176
|
|
|
*/ |
177
|
|
|
public function setAttribs(array $attribs) |
178
|
|
|
{ |
179
|
|
|
$this->clearAttribs(); |
180
|
|
|
|
181
|
|
|
return $this->addAttribs($attribs); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return static |
186
|
|
|
*/ |
187
|
|
|
public function clearAttribs() |
188
|
|
|
{ |
189
|
|
|
$this->_attribs = []; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $attribs |
196
|
|
|
* @return static |
197
|
|
|
*/ |
198
|
|
|
public function addAttribs(array $attribs) |
199
|
|
|
{ |
200
|
|
|
foreach ($attribs as $key => $value) { |
201
|
|
|
$this->setAttrib($key, $value); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
public function removeAttrib($key) |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
if (isset($this->_attribs[$key])) { |
213
|
|
|
unset($this->_attribs[$key]); |
214
|
|
|
|
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.