Fix "last" helper for HHVM

This commit is contained in:
Dmitriy Simushev 2014-12-25 11:21:59 +00:00
parent 62ce4e8639
commit 3d1afa47bc

View File

@ -49,6 +49,23 @@ class LastHelper implements HelperInterface
throw new \InvalidArgumentException('Wrong type of the argument in the "last" helper.');
}
return end($collection);
if (is_array($collection)) {
return end($collection);
}
// "end" function does not work with \Traversable in HHVM. Thus we
// need to get the element manually.
while ($collection instanceof \IteratorAggregate) {
$collection = $collection->getIterator();
}
$collection->rewind();
$item = false;
while ($collection->valid()) {
$item = $collection->current();
$collection->next();
}
return $item;
}
}