Skip to content
Permalink
Browse files

Implemented Collection::last()

  • Loading branch information...
lorenzo committed May 16, 2015
1 parent 8c10dee commit a50ff282f915370d80e3fef374fbc1e3b322b04c
@@ -536,6 +536,14 @@ public function firstMatch(array $conditions);
* @return mixed The first value in the collection will be returned.
*/
public function first();
/**
* Returns the last result in this collection
*
* @return mixed The first value in the collection will be returned.
*/
public function last();
/**
* Returns a new collection as the result of concatenating the list of elements
* in this collection with the passed list of elements
@@ -319,6 +319,21 @@ public function first()
}
}
/**
* {@inheritDoc}
*
*/
public function last()
{
$iterator = $this->unwrap();
$count = $iterator instanceof Countable ?
count($iterator) :
iterator_count($iterator);
foreach ($this->take(1, $count - 1) as $last) {
return $last;
}
}
/**
* {@inheritDoc}
*
@@ -1340,4 +1340,20 @@ public function testSkip()
$this->assertEquals([5], $collection->skip(4)->toList());
}
/**
* Tests the last() method
*
* @return void
*/
public function testLast()
{
$collection = new Collection([1, 2, 3]);
$this->assertEquals(3, $collection->last());
$collection = $collection->map(function ($e) {
return $e * 2;
});
$this->assertEquals(6, $collection->last());
}
}

0 comments on commit a50ff28

Please sign in to comment.
You can’t perform that action at this time.