Skip to content

Commit

Permalink
Implemented "Countable"
Browse files Browse the repository at this point in the history
  • Loading branch information
nozavroni committed Nov 13, 2016
1 parent 9079304 commit b39d2a0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/CSVelte/Collection/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
*/
namespace CSVelte\Collection;

use ArrayAccess;
use Countable;
use Iterator;
use CSVelte\Contract\Collectable;
use CSVelte\Collection\Collection as BaseCollection;
use function CSVelte\is_traversable;
use Iterator;
use ArrayAccess;

use OutOfBoundsException;

/**
Expand All @@ -38,8 +40,9 @@
* it to return other formats if you want
*/
abstract class AbstractCollection implements
Iterator,
ArrayAccess
ArrayAccess,
Countable,
Iterator
/*Collectable*/
{
/**
Expand Down Expand Up @@ -89,8 +92,6 @@ public function __invoke($val = null, $index = null)
}
}

/** BEGIN Iterator methods */

/** BEGIN ArrayAccess methods */

/**
Expand Down Expand Up @@ -142,6 +143,17 @@ public function offsetUnset($offset)

/** END ArrayAccess methods */

/** BEGIN Countable methods */

public function count()
{
return count($this->data);
}

/** END Countable methods */

/** BEGIN Iterator methods */

/**
* Return the current element.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/CSVelte/Collection/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CSVelteTest\Collection;

use ArrayAccess;
use Countable;
use \Iterator;
use \ArrayIterator;
use CSVelte\Collection\AbstractCollection;
Expand Down Expand Up @@ -466,4 +467,16 @@ public function testOffsetMethodsForCollectionArrayAccess()
$this->assertTrue(isset($coll['foo']));
$this->assertEquals('var', $coll['foo']);
}

public function testCollectionIsCountable()
{
$coll = Collection::factory($exp = [
'mk' => 'lady',
'lorrie' => 'sweet',
'luke' => 'really cool guy',
'terry' => 'what a fool',
]);
$this->assertInstanceOf(Countable::class, $coll);
$this->assertEquals(4, $coll->count());
}
}

0 comments on commit b39d2a0

Please sign in to comment.