Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make \Stripe\Collection implement \Countable #879

Merged
merged 1 commit into from
Feb 14, 2020
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
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