1
|
|
|
<?php |
2
|
|
|
namespace Sirius\Input\Traits; |
3
|
|
|
|
4
|
|
|
use Sirius\Input\Specs; |
5
|
|
|
use Sirius\Input\AttributesContainer; |
6
|
|
|
|
7
|
|
|
trait HasHintTrait |
8
|
|
|
{ |
9
|
|
|
|
10
|
2 |
|
protected function ensureHintAttributes() |
11
|
|
|
{ |
12
|
2 |
|
if (!isset($this[Specs::HINT_ATTRIBUTES])) { |
13
|
2 |
|
$this[Specs::HINT_ATTRIBUTES] = new AttributesContainer(); |
14
|
2 |
|
} |
15
|
2 |
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Retrieves the hint's text |
19
|
|
|
* |
20
|
|
|
* @return string|null |
21
|
|
|
*/ |
22
|
1 |
|
public function getHint() |
23
|
|
|
{ |
24
|
1 |
|
return isset($this[Specs::HINT]) ? $this[Specs::HINT] : null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Sets the hint's text |
29
|
|
|
* |
30
|
|
|
* @param string $hint |
31
|
|
|
* |
32
|
|
|
* @return self |
33
|
|
|
*/ |
34
|
1 |
|
public function setHint($hint) |
35
|
|
|
{ |
36
|
1 |
|
$this[Specs::HINT] = $hint; |
37
|
|
|
|
38
|
1 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Retrieve all of the hint's attributes |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
1 |
|
public function getHintAttributes() |
47
|
|
|
{ |
48
|
1 |
|
$this->ensureHintAttributes(); |
49
|
|
|
|
50
|
1 |
|
return $this[Specs::HINT_ATTRIBUTES]->getAll(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets multiple attributes for the hint |
55
|
|
|
* |
56
|
|
|
* @param array $attrs |
57
|
|
|
* |
58
|
|
|
* @return HasHintTrait |
|
|
|
|
59
|
|
|
*/ |
60
|
1 |
|
public function setHintAttributes($attrs) |
61
|
|
|
{ |
62
|
1 |
|
$this->ensureHintAttributes(); |
63
|
1 |
|
$this[Specs::HINT_ATTRIBUTES]->set($attrs); |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve an attribute from the hint |
70
|
|
|
* |
71
|
|
|
* @param string $attr |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
2 |
|
public function getHintAttribute($attr) |
76
|
|
|
{ |
77
|
2 |
|
$this->ensureHintAttributes(); |
78
|
|
|
|
79
|
2 |
|
return $this[Specs::HINT_ATTRIBUTES]->get($attr); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set/Unset a hint attribute |
84
|
|
|
* |
85
|
|
|
* @param string $attr |
86
|
|
|
* @param mixed|null $value |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
1 |
|
public function setHintAttribute($attr, $value = null) |
91
|
|
|
{ |
92
|
1 |
|
$this->ensureHintAttributes(); |
93
|
1 |
|
$this[Specs::HINT_ATTRIBUTES]->set($attr, $value); |
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Adds a CSS class to the hint's "class" attribute |
100
|
|
|
* |
101
|
|
|
* @param string $class |
102
|
|
|
* |
103
|
|
|
* @return self |
104
|
|
|
*/ |
105
|
1 |
|
public function addHintClass($class) |
106
|
|
|
{ |
107
|
1 |
|
$this->ensureHintAttributes(); |
108
|
1 |
|
$this[Specs::HINT_ATTRIBUTES]->addClass($class); |
109
|
|
|
|
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Removes a CSS class from the hint's "class" attribute |
115
|
|
|
* |
116
|
|
|
* @param string $class |
117
|
|
|
* |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
1 |
|
public function removeHintClass($class) |
121
|
|
|
{ |
122
|
1 |
|
$this->ensureHintAttributes(); |
123
|
1 |
|
$this[Specs::HINT_ATTRIBUTES]->removeClass($class); |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Toggles a CSS class to the hint's "class" attribute |
130
|
|
|
* |
131
|
|
|
* @param |
132
|
|
|
* $class |
133
|
|
|
* |
134
|
|
|
* @return self |
135
|
|
|
*/ |
136
|
1 |
|
public function toggleHintClass($class) |
137
|
|
|
{ |
138
|
1 |
|
$this->ensureHintAttributes(); |
139
|
1 |
|
$this[Specs::HINT_ATTRIBUTES]->toggleClass($class); |
140
|
|
|
|
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Checks if the hint has a CSS class on the "class" attribute |
146
|
|
|
* |
147
|
|
|
* @param |
148
|
|
|
* $class |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
1 |
|
public function hasHintClass($class) |
153
|
|
|
{ |
154
|
1 |
|
$this->ensureHintAttributes(); |
155
|
|
|
|
156
|
1 |
|
return $this[Specs::HINT_ATTRIBUTES]->hasClass($class); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.