Skip to content

Commit

Permalink
Make \Stripe\Collection implement \Countable (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe authored Feb 14, 2020
1 parent f7c6462 commit cb44b0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @property bool $has_more
* @property \Stripe\StripeObject[] $data
*/
class Collection extends StripeObject implements \IteratorAggregate
class Collection extends StripeObject implements \Countable, \IteratorAggregate
{
const OBJECT_NAME = 'list';

Expand Down Expand Up @@ -104,6 +104,14 @@ public function retrieve($id, $params = null, $opts = null)
return Util\Util::convertToStripeObject($response, $opts);
}

/**
* @return int the number of objects in the current page
*/
public function count()
{
return \count($this->data);
}

/**
* @return \ArrayIterator an iterator that can be used to iterate
* across objects in the current page
Expand Down
13 changes: 13 additions & 0 deletions tests/Stripe/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ public function testCanCreate()
]);
}

public function testCanCount()
{
$collection = Collection::constructFrom([
'data' => [['id' => 1]],
]);
static::assertCount(1, $collection);

$collection = Collection::constructFrom([
'data' => [['id' => 1], ['id' => 2], ['id' => 3]],
]);
static::assertCount(3, $collection);
}

public function testCanIterate()
{
$collection = Collection::constructFrom([
Expand Down

0 comments on commit cb44b0b

Please sign in to comment.