Skip to content

Commit deb522f

Browse files
authored
Added isEmpty / isNotEmpty / getIterator methods to Fluent class (#7466)
1 parent 659e404 commit deb522f

File tree

2 files changed

+498
-1
lines changed

2 files changed

+498
-1
lines changed

src/Fluent.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
namespace Hyperf\Support;
1414

1515
use ArrayAccess;
16+
use ArrayIterator;
1617
use Closure;
1718
use Hyperf\Contract\Arrayable;
1819
use Hyperf\Contract\Jsonable;
20+
use Hyperf\Macroable\Macroable;
21+
use IteratorAggregate;
1922
use JsonSerializable;
23+
use Traversable;
2024

2125
/**
2226
* Most of the methods in this file come from illuminate/support,
@@ -28,8 +32,12 @@
2832
* @implements \Hyperf\Contract\Arrayable<TKey, TValue>
2933
* @implements ArrayAccess<TKey, TValue>
3034
*/
31-
class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
35+
class Fluent implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, JsonSerializable
3236
{
37+
use Macroable{
38+
__call as macroCall;
39+
}
40+
3341
/**
3442
* All the attributes set on the fluent instance.
3543
*
@@ -58,6 +66,10 @@ public function __construct($attributes = [])
5866
*/
5967
public function __call($method, $parameters)
6068
{
69+
if (static::hasMacro($method)) {
70+
return $this->macroCall($method, $parameters);
71+
}
72+
6173
$this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true;
6274

6375
return $this;
@@ -139,6 +151,26 @@ public function getAttributes()
139151
return $this->attributes;
140152
}
141153

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+
142174
/**
143175
* Convert the fluent instance to an array.
144176
*
@@ -211,4 +243,14 @@ public function offsetUnset(mixed $offset): void
211243
{
212244
unset($this->attributes[$offset]);
213245
}
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+
}
214256
}

0 commit comments

Comments
 (0)