Skip to content

Commit 89207fb

Browse files
authored
Add 'isEmpty' and 'isNotEmpty' to Fluent (#56370)
1 parent 3319568 commit 89207fb

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Illuminate/Support/Fluent.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ public function toJson($options = 0)
203203
return json_encode($this->jsonSerialize(), $options);
204204
}
205205

206+
/**
207+
* Determine if the fluent instance is empty.
208+
*
209+
* @return bool
210+
*/
211+
public function isEmpty(): bool
212+
{
213+
return empty($this->attributes);
214+
}
215+
216+
/**
217+
* Determine if the fluent instance is not empty.
218+
*
219+
* @return bool
220+
*/
221+
public function isNotEmpty(): bool
222+
{
223+
return ! $this->isEmpty();
224+
}
225+
206226
/**
207227
* Determine if the given offset exists.
208228
*

tests/Support/SupportFluentTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,25 @@ public function testFluentIsIterable()
471471
'role' => 'admin',
472472
], $result);
473473
}
474+
475+
public function testFluentIsEmpty()
476+
{
477+
$fluent = new Fluent;
478+
479+
$this->assertTrue($fluent->isEmpty());
480+
$this->assertFalse($fluent->isNotEmpty());
481+
}
482+
483+
public function testFluentIsNotEmpty()
484+
{
485+
$fluent = new Fluent([
486+
'name' => 'Taylor',
487+
'role' => 'admin',
488+
]);
489+
490+
$this->assertTrue($fluent->isNotEmpty());
491+
$this->assertFalse($fluent->isEmpty());
492+
}
474493
}
475494

476495
class FluentArrayIteratorStub implements IteratorAggregate

0 commit comments

Comments
 (0)