1
|
|
|
<?php |
2
|
|
|
namespace Sirius\Input\Traits; |
3
|
|
|
|
4
|
|
|
use Sirius\Input\Specs; |
5
|
|
|
use Sirius\Input\AttributesContainer; |
6
|
|
|
|
7
|
|
|
trait HasAttributesTrait |
8
|
|
|
{ |
9
|
|
|
|
10
|
41 |
|
protected function ensureAttributes() |
11
|
|
|
{ |
12
|
41 |
|
if (!isset($this[Specs::ATTRIBUTES])) { |
13
|
41 |
|
$this[Specs::ATTRIBUTES] = new AttributesContainer(); |
14
|
41 |
|
} |
15
|
41 |
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Retrieve all of the label's attributes |
19
|
|
|
* |
20
|
|
|
* @return mixed |
21
|
|
|
*/ |
22
|
2 |
|
public function getAttributes() |
23
|
|
|
{ |
24
|
2 |
|
$this->ensureAttributes(); |
25
|
|
|
|
26
|
2 |
|
return $this[Specs::ATTRIBUTES]->getAll(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Sets multiple attributes for the label |
31
|
|
|
* |
32
|
|
|
* @param array $attrs |
33
|
|
|
* |
34
|
|
|
* @return HasAttributesTrait |
|
|
|
|
35
|
|
|
*/ |
36
|
9 |
|
public function setAttributes($attrs) |
37
|
|
|
{ |
38
|
9 |
|
$this->ensureAttributes(); |
39
|
9 |
|
$this[Specs::ATTRIBUTES]->set($attrs); |
40
|
|
|
|
41
|
9 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieve an attribute from the label |
46
|
|
|
* |
47
|
|
|
* @param string $attr |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
3 |
|
public function getAttribute($attr) |
52
|
|
|
{ |
53
|
3 |
|
$this->ensureAttributes(); |
54
|
|
|
|
55
|
3 |
|
return $this[Specs::ATTRIBUTES]->get($attr); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set/Unset a label attribute |
60
|
|
|
* |
61
|
|
|
* @param string $attr |
62
|
|
|
* @param string $value |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
35 |
|
public function setAttribute($attr, $value = null) |
67
|
|
|
{ |
68
|
35 |
|
$this->ensureAttributes(); |
69
|
35 |
|
$this[Specs::ATTRIBUTES]->set($attr, $value); |
70
|
|
|
|
71
|
35 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Adds a CSS class to the label's "class" attribute |
76
|
|
|
* |
77
|
|
|
* @param string $class |
78
|
|
|
* |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
1 |
|
public function addClass($class) |
82
|
|
|
{ |
83
|
1 |
|
$this->ensureAttributes(); |
84
|
1 |
|
$this[Specs::ATTRIBUTES]->addClass($class); |
85
|
|
|
|
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Removes a CSS class from the label's "class" attribute |
91
|
|
|
* |
92
|
|
|
* @param string $class |
93
|
|
|
* |
94
|
|
|
* @return self |
95
|
|
|
*/ |
96
|
1 |
|
public function removeClass($class) |
97
|
|
|
{ |
98
|
1 |
|
$this->ensureAttributes(); |
99
|
1 |
|
$this[Specs::ATTRIBUTES]->removeClass($class); |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Toggles a CSS class to the label's "class" attribute |
106
|
|
|
* |
107
|
|
|
* @param |
108
|
|
|
* $class |
109
|
|
|
* |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
1 |
|
public function toggleClass($class) |
113
|
|
|
{ |
114
|
1 |
|
$this->ensureAttributes(); |
115
|
1 |
|
$this[Specs::ATTRIBUTES]->toggleClass($class); |
116
|
|
|
|
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Checks if the label has a CSS class on the "class" attribute |
122
|
|
|
* |
123
|
|
|
* @param |
124
|
|
|
* $class |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
2 |
|
public function hasClass($class) |
129
|
|
|
{ |
130
|
2 |
|
$this->ensureAttributes(); |
131
|
|
|
|
132
|
2 |
|
return $this[Specs::ATTRIBUTES]->hasClass($class); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.