diff --git a/lib/Collection.php b/lib/Collection.php index 199aa9d32..ec5bebb7a 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); } + /** + * Gets the first item from the current page. Returns `null` if the current page is empty. + * + * @return null|\Stripe\StripeObject + */ + public function first() + { + return \count($this->data) > 0 ? $this->data[0] : null; + } + + /** + * Gets the last item from the current page. Returns `null` if the current page is empty. + * + * @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']); + } }