Skip to content

Commit

Permalink
Speeding up conversion of collections to arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 6, 2015
1 parent 6b9920d commit 9ee1eab
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Collection/CollectionTrait.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Collection;

use AppendIterator;
use ArrayIterator;
use ArrayObject;
use Cake\Collection\Collection;
use Cake\Collection\Iterator\BufferedIterator;
Expand Down Expand Up @@ -409,6 +410,16 @@ public function insert($path, $values)
*/
public function toArray($preserveKeys = true)
{
if (get_class($this) === 'Cake\Collection\Collection') {
$inner = $this->getInnerIterator();
if ($inner instanceof ArrayIterator) {
$items = $this->getInnerIterator()->getArrayCopy();
return $preserveKeys ? $items : array_values($items);
}
if ($inner instanceof CollectionInterface) {
return $inner->toArray($preserveKeys);
}
}
return iterator_to_array($this, $preserveKeys);
}

Expand All @@ -418,7 +429,7 @@ public function toArray($preserveKeys = true)
*/
public function toList()
{
return iterator_to_array($this, false);
return $this->toArray(false);
}

/**
Expand Down

0 comments on commit 9ee1eab

Please sign in to comment.