13
13
namespace Hyperf \Support ;
14
14
15
15
use ArrayAccess ;
16
+ use Closure ;
16
17
use Hyperf \Contract \Arrayable ;
17
18
use Hyperf \Contract \Jsonable ;
18
19
use JsonSerializable ;
19
20
20
21
/**
21
22
* Most of the methods in this file come from illuminate/support,
22
23
* thanks Laravel Team provide such a useful class.
24
+ *
25
+ * @template TKey of array-key
26
+ * @template TValue
27
+ *
28
+ * @implements \Hyperf\Contract\Arrayable<TKey, TValue>
29
+ * @implements \ArrayAccess<TKey, TValue>
23
30
*/
24
31
class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
25
32
{
26
33
/**
27
34
* All the attributes set on the fluent instance.
28
35
*
29
- * @var array
36
+ * @var array<TKey, TValue>
30
37
*/
31
38
protected $ attributes = [];
32
39
33
40
/**
34
41
* Create a new fluent instance.
35
42
*
36
- * @param array|object $attributes
43
+ * @param iterable<TKey, TValue> $attributes
37
44
*/
38
45
public function __construct ($ attributes = [])
39
46
{
@@ -45,7 +52,7 @@ public function __construct($attributes = [])
45
52
/**
46
53
* Handle dynamic calls to the fluent instance to set attributes.
47
54
*
48
- * @param string $method
55
+ * @param TKey $method
49
56
* @param array $parameters
50
57
* @return $this
51
58
*/
@@ -59,7 +66,8 @@ public function __call($method, $parameters)
59
66
/**
60
67
* Dynamically retrieve the value of an attribute.
61
68
*
62
- * @param string $key
69
+ * @param TKey $key
70
+ * @return null|TValue
63
71
*/
64
72
public function __get ($ key )
65
73
{
@@ -69,8 +77,8 @@ public function __get($key)
69
77
/**
70
78
* Dynamically set the value of an attribute.
71
79
*
72
- * @param string $key
73
- * @param mixed $value
80
+ * @param TKey $key
81
+ * @param TValue $value
74
82
*/
75
83
public function __set ($ key , $ value )
76
84
{
@@ -80,7 +88,7 @@ public function __set($key, $value)
80
88
/**
81
89
* Dynamically check if an attribute is set.
82
90
*
83
- * @param string $key
91
+ * @param TKey $key
84
92
* @return bool
85
93
*/
86
94
public function __isset ($ key )
@@ -91,7 +99,7 @@ public function __isset($key)
91
99
/**
92
100
* Dynamically unset an attribute.
93
101
*
94
- * @param string $key
102
+ * @param TKey $key
95
103
*/
96
104
public function __unset ($ key )
97
105
{
@@ -106,8 +114,11 @@ public function __toString(): string
106
114
/**
107
115
* Get an attribute from the fluent instance.
108
116
*
109
- * @param string $key
110
- * @param null|mixed $default
117
+ * @template TGetDefault
118
+ *
119
+ * @param TKey $key
120
+ * @param (Closure(): TGetDefault)|TGetDefault $default
121
+ * @return TGetDefault|TValue
111
122
*/
112
123
public function get ($ key , $ default = null )
113
124
{
@@ -121,7 +132,7 @@ public function get($key, $default = null)
121
132
/**
122
133
* Get the attributes from the fluent instance.
123
134
*
124
- * @return array
135
+ * @return array<TKey, TValue>
125
136
*/
126
137
public function getAttributes ()
127
138
{
@@ -130,6 +141,8 @@ public function getAttributes()
130
141
131
142
/**
132
143
* Convert the fluent instance to an array.
144
+ *
145
+ * @return array<TKey, TValue>
133
146
*/
134
147
public function toArray (): array
135
148
{
@@ -138,6 +151,8 @@ public function toArray(): array
138
151
139
152
/**
140
153
* Convert the object into something JSON serializable.
154
+ *
155
+ * @return array<TKey, TValue>
141
156
*/
142
157
public function jsonSerialize (): mixed
143
158
{
@@ -157,6 +172,8 @@ public function toJson($options = 0)
157
172
158
173
/**
159
174
* Determine if the given offset exists.
175
+ *
176
+ * @param TKey $offset
160
177
*/
161
178
public function offsetExists (mixed $ offset ): bool
162
179
{
@@ -165,6 +182,9 @@ public function offsetExists(mixed $offset): bool
165
182
166
183
/**
167
184
* Get the value for a given offset.
185
+ *
186
+ * @param TKey $offset
187
+ * @return null|TValue
168
188
*/
169
189
public function offsetGet (mixed $ offset ): mixed
170
190
{
@@ -173,6 +193,9 @@ public function offsetGet(mixed $offset): mixed
173
193
174
194
/**
175
195
* Set the value at the given offset.
196
+ *
197
+ * @param TKey $offset
198
+ * @param TValue $value
176
199
*/
177
200
public function offsetSet (mixed $ offset , mixed $ value ): void
178
201
{
@@ -181,6 +204,8 @@ public function offsetSet(mixed $offset, mixed $value): void
181
204
182
205
/**
183
206
* Unset the value at the given offset.
207
+ *
208
+ * @param TKey $offset
184
209
*/
185
210
public function offsetUnset (mixed $ offset ): void
186
211
{
0 commit comments