1 | <?php |
||
7 | trait HasLabelTrait |
||
8 | { |
||
9 | |||
10 | 2 | protected function ensureLabelAttributes() |
|
11 | { |
||
12 | 2 | if (!isset($this[Specs::LABEL_ATTRIBUTES])) { |
|
13 | 2 | $this[Specs::LABEL_ATTRIBUTES] = new AttributesContainer(); |
|
14 | } |
||
15 | 2 | } |
|
16 | |||
17 | /** |
||
18 | * Retrieves the label's text |
||
19 | * |
||
20 | * @return string null |
||
21 | */ |
||
22 | 6 | public function getLabel() |
|
26 | |||
27 | /** |
||
28 | * Sets the label's text |
||
29 | * |
||
30 | * @param string $label |
||
31 | * |
||
32 | * @return self |
||
33 | */ |
||
34 | 2 | public function setLabel($label) |
|
40 | |||
41 | /** |
||
42 | * Retrieve all of the label's attributes |
||
43 | * |
||
44 | * @return mixed |
||
45 | */ |
||
46 | 1 | public function getLabelAttributes() |
|
52 | |||
53 | /** |
||
54 | * Sets multiple attributes for the label |
||
55 | * |
||
56 | * @param array $attrs |
||
57 | * |
||
58 | * @return HasLabelTrait |
||
|
|||
59 | */ |
||
60 | 1 | public function setLabelAttributes($attrs) |
|
67 | |||
68 | /** |
||
69 | * Retrieve an attribute from the label |
||
70 | * |
||
71 | * @param string $attr |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | 2 | public function getLabelAttribute($attr) |
|
81 | |||
82 | /** |
||
83 | * Set/Unset a label attribute |
||
84 | * |
||
85 | * @param string $attr |
||
86 | * @param mixed|null $value |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | 1 | public function setLabelAttribute($attr, $value = null) |
|
98 | |||
99 | /** |
||
100 | * Adds a CSS class to the label's "class" attribute |
||
101 | * |
||
102 | * @param string $class |
||
103 | * |
||
104 | * @return self |
||
105 | */ |
||
106 | 1 | public function addLabelClass($class) |
|
113 | |||
114 | /** |
||
115 | * Removes a CSS class from the label's "class" attribute |
||
116 | * |
||
117 | * @param string $class |
||
118 | * |
||
119 | * @return self |
||
120 | */ |
||
121 | 1 | public function removeLabelClass($class) |
|
128 | |||
129 | /** |
||
130 | * Toggles a CSS class to the label's "class" attribute |
||
131 | * |
||
132 | * @param |
||
133 | * $class |
||
134 | * |
||
135 | * @return self |
||
136 | */ |
||
137 | 1 | public function toggleLabelClass($class) |
|
144 | |||
145 | /** |
||
146 | * Checks if the label has a CSS class on the "class" attribute |
||
147 | * |
||
148 | * @param |
||
149 | * $class |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 1 | public function hasLabelClass($class) |
|
159 | } |
||
160 |
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.