Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []) {
Expand Down
7 changes: 7 additions & 0 deletions src/Interfaces/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/ReadOnlyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions src/Traits/ReadableCollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
7 changes: 7 additions & 0 deletions tests/ReadOnlyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}