From ec528f7071c8467d6cab08d61141d3859fb37662 Mon Sep 17 00:00:00 2001 From: baijunyao Date: Thu, 4 Jun 2020 16:58:05 +0800 Subject: [PATCH 1/3] Add `first()` and `last()` functions to `Collection` (#948) --- lib/Collection.php | 20 ++++++++++++++++++++ tests/Stripe/CollectionTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/Collection.php b/lib/Collection.php index 199aa9d32..1981de465 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -241,6 +241,26 @@ public function previousPage($params = null, $opts = null) return $this->all($params, $opts); } + /** + * Get the first item from the collection. + * + * @return null|\Stripe\StripeObject + */ + public function first() + { + return \count($this->data) > 0 ? $this->data[0] : null; + } + + /** + * Get the last item from the collection. + * + * @return null|\Stripe\StripeObject + */ + public function last() + { + return \count($this->data) > 0 ? $this->data[\count($this->data) - 1] : null; + } + private function extractPathAndUpdateParams($params) { $url = \parse_url($this->url); diff --git a/tests/Stripe/CollectionTest.php b/tests/Stripe/CollectionTest.php index 28abc2ca0..df40fdf3c 100644 --- a/tests/Stripe/CollectionTest.php +++ b/tests/Stripe/CollectionTest.php @@ -312,4 +312,28 @@ public function testPreviousPage() $previousPage = $this->fixture->previousPage(); static::assertSame([], $previousPage->data); } + + public function testFirst() + { + $collection = Collection::constructFrom([ + 'data' => [ + ['content' => 'first'], + ['content' => 'middle'], + ['content' => 'last'], + ], + ]); + static::assertSame('first', $collection->first()['content']); + } + + public function testLast() + { + $collection = Collection::constructFrom([ + 'data' => [ + ['content' => 'first'], + ['content' => 'middle'], + ['content' => 'last'], + ], + ]); + static::assertSame('last', $collection->last()['content']); + } } From 20cd9dc6177d6343dffc62a6d18784b0e2f8964d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E4=BF=8A=E9=81=A5?= Date: Tue, 16 Jun 2020 08:44:59 +0800 Subject: [PATCH 2/3] Update lib/Collection.php Co-authored-by: richardm-stripe <52928443+richardm-stripe@users.noreply.github.com> --- lib/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Collection.php b/lib/Collection.php index 1981de465..c0f1fcc40 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -242,7 +242,7 @@ public function previousPage($params = null, $opts = null) } /** - * Get the first item from the collection. + * Gets the first item from the current page. Returns `null` if the current page is empty. * * @return null|\Stripe\StripeObject */ From 409c8ae106722f4bbf593be1621f6b1886b5c67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E4=BF=8A=E9=81=A5?= Date: Tue, 16 Jun 2020 08:45:10 +0800 Subject: [PATCH 3/3] Update lib/Collection.php Co-authored-by: richardm-stripe <52928443+richardm-stripe@users.noreply.github.com> --- lib/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Collection.php b/lib/Collection.php index c0f1fcc40..ec5bebb7a 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -252,7 +252,7 @@ public function first() } /** - * Get the last item from the collection. + * Gets the last item from the current page. Returns `null` if the current page is empty. * * @return null|\Stripe\StripeObject */