Skip to content

Commit 1d6b0b8

Browse files
committed
feat: add isEmpty and hasItems to collection
1 parent 63d83ee commit 1d6b0b8

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

CHANGELOG.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ Added a couple of functions to the [Collection](./src/Domain/Collection.php) cla
88

99
The following functions were added:
1010
* `fromIterable` - enables users of the collection to construction the collection from an iterable value like iterators, generators, etc.
11-
* `every` - Function that returns true if given callback returns truthy values for all items
12-
* `none` - Function that returns true if given callback returns falsy values for all items
13-
* `some` - Function that returns true if given callback returns truthy values on some items
11+
* `every` - Returns true if given callback returns truthy values for all items
12+
* `none` - Returns true if given callback returns falsy values for all items
13+
* `some` - Returns true if given callback returns truthy values on some items
1414
* `first` - Get the first element of the collection that matches a callback, if given. Throws exception if collection is empty or predicate is never satisfied
1515
* `firstOr` - Same as first but returns $fallbackValue if collection is empty or predicate is never satisfied
1616
* `last` - Get the last element of the collection that matches a callback, if given. Throws exception if collection is empty or predicate is never satisfied
1717
* `lastOr` - Same as last but returns $fallbackValue if collection is empty or predicate is never satisfied
18+
* `isEmpty` - Returns whether the collection is empty
19+
* `hasItems` - Returns whether the collection has items
1820

1921
## [1.4.0](https://github.com/geekcell/php-ddd/compare/v1.3.1...v1.4.0) (2023-12-19)
2022

src/Domain/Collection.php

+16
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ public function lastOr(callable $callback = null, mixed $fallbackValue = null)
231231
return $fallbackValue;
232232
}
233233

234+
/**
235+
* Returns whether the collection is empty (has no items)
236+
*/
237+
public function isEmpty(): bool
238+
{
239+
return $this->items === [];
240+
}
241+
242+
/**
243+
* Returns whether the collection has items
244+
*/
245+
public function hasItems(): bool
246+
{
247+
return $this->items !== [];
248+
}
249+
234250
/**
235251
* Add one or more items to the collection. It **does not** modify the
236252
* current collection, but returns a new one.

tests/Domain/CollectionTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -526,4 +526,16 @@ public function testLastOrReturnsFallbackValueIfCallbackIsNeverSatisfied(): void
526526
$collection = new Collection($items);
527527
$this->assertSame(-1, $collection->lastOr(static fn ($item) => $item > 10, -1));
528528
}
529+
530+
public function testIsEmpty(): void
531+
{
532+
$this->assertFalse((new Collection([1]))->isEmpty());
533+
$this->assertTrue((new Collection([]))->isEmpty());
534+
}
535+
536+
public function testHasItems(): void
537+
{
538+
$this->assertFalse((new Collection([]))->hasItems());
539+
$this->assertTrue((new Collection([1]))->hasItems());
540+
}
529541
}

0 commit comments

Comments
 (0)