Skip to content

Commit

Permalink
Update count() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 21, 2019
1 parent 04739ad commit baf96eb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use drupol\collection\Operation\Collapse;
use drupol\collection\Operation\Combine;
use drupol\collection\Operation\Contains;
use drupol\collection\Operation\Count;
use drupol\collection\Operation\Filter;
use drupol\collection\Operation\First;
use drupol\collection\Operation\Flatten;
Expand Down Expand Up @@ -112,7 +113,7 @@ public function contains($key): bool
*/
public function count(): int
{
return \iterator_count($this->getIterator());
return $this->run(Count::with());
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/Operation/Count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Count.
*
* Be careful, this will only work with finite collection sets.
*/
final class Count extends Operation
{
/**
* {@inheritdoc}
*/
public function run(CollectionInterface $collection)
{
return \iterator_count($collection->getIterator());
}
}

0 comments on commit baf96eb

Please sign in to comment.