From a13c95570cbc20daeeb2af4563578c2dc84f7e1d Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Fri, 14 Feb 2020 09:39:06 -0800 Subject: [PATCH] Make `\Stripe\Collection` implement `\Countable` --- lib/Collection.php | 10 +++++++++- tests/Stripe/CollectionTest.php | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/Collection.php b/lib/Collection.php index 96cf6cbc1..199aa9d32 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -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'; @@ -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 diff --git a/tests/Stripe/CollectionTest.php b/tests/Stripe/CollectionTest.php index 38ee133f9..5465ef51e 100644 --- a/tests/Stripe/CollectionTest.php +++ b/tests/Stripe/CollectionTest.php @@ -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([