Browse Source

Speeding up conversion of collections to arrays

Jose Lorenzo Rodriguez 11 years ago
parent
commit
9ee1eab782
1 changed files with 12 additions and 1 deletions
  1. 12 1
      src/Collection/CollectionTrait.php

+ 12 - 1
src/Collection/CollectionTrait.php

@@ -15,6 +15,7 @@
 namespace Cake\Collection;
 
 use AppendIterator;
+use ArrayIterator;
 use ArrayObject;
 use Cake\Collection\Collection;
 use Cake\Collection\Iterator\BufferedIterator;
@@ -409,6 +410,16 @@ trait CollectionTrait
      */
     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);
     }
 
@@ -418,7 +429,7 @@ trait CollectionTrait
      */
     public function toList()
     {
-        return iterator_to_array($this, false);
+        return $this->toArray(false);
     }
 
     /**