Skip to content

Commit

Permalink
feat: add isEmpty and hasItems to collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Bl00D4NGEL committed Jan 5, 2024
1 parent 63d83ee commit 1d6b0b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ Added a couple of functions to the [Collection](./src/Domain/Collection.php) cla

The following functions were added:
* `fromIterable` - enables users of the collection to construction the collection from an iterable value like iterators, generators, etc.
* `every` - Function that returns true if given callback returns truthy values for all items
* `none` - Function that returns true if given callback returns falsy values for all items
* `some` - Function that returns true if given callback returns truthy values on some items
* `every` - Returns true if given callback returns truthy values for all items
* `none` - Returns true if given callback returns falsy values for all items
* `some` - Returns true if given callback returns truthy values on some items
* `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
* `firstOr` - Same as first but returns $fallbackValue if collection is empty or predicate is never satisfied
* `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
* `lastOr` - Same as last but returns $fallbackValue if collection is empty or predicate is never satisfied
* `isEmpty` - Returns whether the collection is empty
* `hasItems` - Returns whether the collection has items

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

Expand Down
16 changes: 16 additions & 0 deletions src/Domain/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ public function lastOr(callable $callback = null, mixed $fallbackValue = null)
return $fallbackValue;
}

/**
* Returns whether the collection is empty (has no items)
*/
public function isEmpty(): bool
{
return $this->items === [];
}

/**
* Returns whether the collection has items
*/
public function hasItems(): bool
{
return $this->items !== [];
}

/**
* Add one or more items to the collection. It **does not** modify the
* current collection, but returns a new one.
Expand Down
12 changes: 12 additions & 0 deletions tests/Domain/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,4 +526,16 @@ public function testLastOrReturnsFallbackValueIfCallbackIsNeverSatisfied(): void
$collection = new Collection($items);
$this->assertSame(-1, $collection->lastOr(static fn ($item) => $item > 10, -1));
}

public function testIsEmpty(): void
{
$this->assertFalse((new Collection([1]))->isEmpty());
$this->assertTrue((new Collection([]))->isEmpty());
}

public function testHasItems(): void
{
$this->assertFalse((new Collection([]))->hasItems());
$this->assertTrue((new Collection([1]))->hasItems());
}
}

0 comments on commit 1d6b0b8

Please sign in to comment.