diff --git a/src/Collection.php b/src/Collection.php index 5b459d8..c5dcc07 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -4,12 +4,13 @@ use BapCat\Collection\Interfaces\WritableCollection as WritableCollectionInterface; use BapCat\Collection\Traits\CollectionTrait; +use Countable; use IteratorAggregate; /** * A readable, writable, iterable collection */ -class Collection implements CollectionInterface, WritableCollectionInterface, IteratorAggregate { +class Collection implements Countable, CollectionInterface, WritableCollectionInterface, IteratorAggregate { use CollectionTrait; public function __construct(array $initial = []) { diff --git a/src/Interfaces/Collection.php b/src/Interfaces/Collection.php index 03a8916..53d9ce1 100644 --- a/src/Interfaces/Collection.php +++ b/src/Interfaces/Collection.php @@ -84,6 +84,13 @@ public function values(); * @returns int The size of the collection */ public function size(); + + /** + * Get the size of the collection + * + * @returns int The size of the collection + */ + public function count(); /** * Merges two collections together. If the arrays contain `string` keys, the values diff --git a/src/ReadOnlyCollection.php b/src/ReadOnlyCollection.php index d0c1fd8..fd434f4 100644 --- a/src/ReadOnlyCollection.php +++ b/src/ReadOnlyCollection.php @@ -4,12 +4,13 @@ use BapCat\Collection\Traits\ReadableCollectionTrait; use BapCat\Collection\Traits\IterableCollectionTrait; +use Countable; use IteratorAggregate; /** * A readable, writable, iterable collection */ -class ReadOnlyCollection implements CollectionInterface, IteratorAggregate { +class ReadOnlyCollection implements Countable, CollectionInterface, IteratorAggregate { use ReadableCollectionTrait; use IterableCollectionTrait; diff --git a/src/Traits/ReadableCollectionTrait.php b/src/Traits/ReadableCollectionTrait.php index 15d520d..09afa16 100644 --- a/src/Traits/ReadableCollectionTrait.php +++ b/src/Traits/ReadableCollectionTrait.php @@ -146,6 +146,18 @@ public function values() { public function size() { return count($this->collection); } + + /** + * Get the number of elements in the collection; Required for Countable interface + * + * @see Countable + * @see self::size() + * + * @return int The size of the collection + */ + public function count() { + return $this->size(); + } /** * Merges two collections together. If the arrays contain `string` keys, the values diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 261812d..006066c 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -31,7 +31,13 @@ public function testConstructWithContent() { public function testInstantiator() { $collection = new Collection(); - $this->assertInstanceOf(Collection::class, $collection->__new([])); } + + public function testCountable() { + $collection = new Collection(); + $this->assertSame(0, count($collection)); + $collection = new Collection([1,2,3]); + $this->assertSame(3, count($collection)); + } } diff --git a/tests/ReadOnlyCollectionTest.php b/tests/ReadOnlyCollectionTest.php index 69420c6..de0a750 100644 --- a/tests/ReadOnlyCollectionTest.php +++ b/tests/ReadOnlyCollectionTest.php @@ -34,4 +34,11 @@ public function testInstantiator() { $this->assertInstanceOf(ReadOnlyCollection::class, $collection->__new([])); } + + public function testCountable() { + $collection = new ReadOnlyCollection(); + $this->assertSame(0, count($collection)); + $collection = new ReadOnlyCollection([1,2,3]); + $this->assertSame(3, count($collection)); + } }