|
13 | 13 | namespace Hyperf\Support; |
14 | 14 |
|
15 | 15 | use ArrayAccess; |
| 16 | +use ArrayIterator; |
16 | 17 | use Closure; |
17 | 18 | use Hyperf\Collection\Arr; |
18 | 19 | use Hyperf\Contract\Arrayable; |
19 | 20 | use Hyperf\Contract\Jsonable; |
20 | 21 | use Hyperf\Macroable\Macroable; |
| 22 | +use IteratorAggregate; |
21 | 23 | use JsonSerializable; |
| 24 | +use Traversable; |
22 | 25 |
|
23 | 26 | use function Hyperf\Collection\data_get; |
24 | 27 | use function Hyperf\Collection\data_set; |
|
33 | 36 | * @implements \Hyperf\Contract\Arrayable<TKey, TValue> |
34 | 37 | * @implements ArrayAccess<TKey, TValue> |
35 | 38 | */ |
36 | | -class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable |
| 39 | +class Fluent implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, JsonSerializable |
37 | 40 | { |
38 | 41 | use Traits\InteractsWithData; |
39 | | - use Macroable{ |
| 42 | + use Macroable { |
40 | 43 | __call as macroCall; |
41 | 44 | } |
42 | 45 |
|
@@ -227,6 +230,26 @@ public function getAttributes() |
227 | 230 | return $this->attributes; |
228 | 231 | } |
229 | 232 |
|
| 233 | + /** |
| 234 | + * Determine if the fluent instance is empty. |
| 235 | + * |
| 236 | + * @return bool |
| 237 | + */ |
| 238 | + public function isEmpty() |
| 239 | + { |
| 240 | + return empty($this->attributes); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Determine if the fluent instance is not empty. |
| 245 | + * |
| 246 | + * @return bool |
| 247 | + */ |
| 248 | + public function isNotEmpty() |
| 249 | + { |
| 250 | + return ! $this->isEmpty(); |
| 251 | + } |
| 252 | + |
230 | 253 | /** |
231 | 254 | * Convert the fluent instance to an array. |
232 | 255 | * |
@@ -297,6 +320,16 @@ public function offsetUnset(mixed $offset): void |
297 | 320 | unset($this->attributes[$offset]); |
298 | 321 | } |
299 | 322 |
|
| 323 | + /** |
| 324 | + * Get an iterator for the attributes. |
| 325 | + * |
| 326 | + * @return ArrayIterator<TKey, TValue> |
| 327 | + */ |
| 328 | + public function getIterator(): Traversable |
| 329 | + { |
| 330 | + return new ArrayIterator($this->attributes); |
| 331 | + } |
| 332 | + |
300 | 333 | /** |
301 | 334 | * Get data from the fluent instance. |
302 | 335 | * |
|
0 commit comments