|
13 | 13 | namespace Hyperf\Support; |
14 | 14 |
|
15 | 15 | use ArrayAccess; |
| 16 | +use ArrayIterator; |
16 | 17 | use Closure; |
17 | 18 | use Hyperf\Contract\Arrayable; |
18 | 19 | use Hyperf\Contract\Jsonable; |
| 20 | +use Hyperf\Macroable\Macroable; |
| 21 | +use IteratorAggregate; |
19 | 22 | use JsonSerializable; |
| 23 | +use Traversable; |
20 | 24 |
|
21 | 25 | /** |
22 | 26 | * Most of the methods in this file come from illuminate/support, |
|
28 | 32 | * @implements \Hyperf\Contract\Arrayable<TKey, TValue> |
29 | 33 | * @implements ArrayAccess<TKey, TValue> |
30 | 34 | */ |
31 | | -class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable |
| 35 | +class Fluent implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, JsonSerializable |
32 | 36 | { |
| 37 | + use Macroable{ |
| 38 | + __call as macroCall; |
| 39 | + } |
| 40 | + |
33 | 41 | /** |
34 | 42 | * All the attributes set on the fluent instance. |
35 | 43 | * |
@@ -58,6 +66,10 @@ public function __construct($attributes = []) |
58 | 66 | */ |
59 | 67 | public function __call($method, $parameters) |
60 | 68 | { |
| 69 | + if (static::hasMacro($method)) { |
| 70 | + return $this->macroCall($method, $parameters); |
| 71 | + } |
| 72 | + |
61 | 73 | $this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true; |
62 | 74 |
|
63 | 75 | return $this; |
@@ -139,6 +151,26 @@ public function getAttributes() |
139 | 151 | return $this->attributes; |
140 | 152 | } |
141 | 153 |
|
| 154 | + /** |
| 155 | + * Determine if the fluent instance is empty. |
| 156 | + * |
| 157 | + * @return bool |
| 158 | + */ |
| 159 | + public function isEmpty() |
| 160 | + { |
| 161 | + return empty($this->attributes); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Determine if the fluent instance is not empty. |
| 166 | + * |
| 167 | + * @return bool |
| 168 | + */ |
| 169 | + public function isNotEmpty() |
| 170 | + { |
| 171 | + return ! $this->isEmpty(); |
| 172 | + } |
| 173 | + |
142 | 174 | /** |
143 | 175 | * Convert the fluent instance to an array. |
144 | 176 | * |
@@ -211,4 +243,14 @@ public function offsetUnset(mixed $offset): void |
211 | 243 | { |
212 | 244 | unset($this->attributes[$offset]); |
213 | 245 | } |
| 246 | + |
| 247 | + /** |
| 248 | + * Get an iterator for the attributes. |
| 249 | + * |
| 250 | + * @return ArrayIterator<TKey, TValue> |
| 251 | + */ |
| 252 | + public function getIterator(): Traversable |
| 253 | + { |
| 254 | + return new ArrayIterator($this->attributes); |
| 255 | + } |
214 | 256 | } |
0 commit comments