Skip to content

Commit

Permalink
Add first() and last() functions to Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
baijunyao committed Jun 11, 2020
1 parent 2468717 commit e862605
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 \reset($this->data);
}

/**
* Get the last item from the collection.
*
* @return null|\Stripe\StripeObject
*/
public function last()
{
return \end($this->data);
}

private function extractPathAndUpdateParams($params)
{
$url = \parse_url($this->url);
Expand Down
24 changes: 24 additions & 0 deletions tests/Stripe/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit e862605

Please sign in to comment.