|
@@ -35,14 +35,27 @@ use RecursiveIteratorIterator;
|
|
|
use Traversable;
|
|
|
|
|
|
/**
|
|
|
- * Offers a handful of method to manipulate iterators
|
|
|
+ * Offers a handful of methods to manipulate iterators
|
|
|
*/
|
|
|
trait CollectionTrait
|
|
|
{
|
|
|
-
|
|
|
use ExtractTrait;
|
|
|
|
|
|
/**
|
|
|
+ * Returns a new collection.
|
|
|
+ *
|
|
|
+ * Allows classes which use this trait to determine their own
|
|
|
+ * type of returned collection interface
|
|
|
+ *
|
|
|
+ * @param mixed ...$args Constructor arguments.
|
|
|
+ * @return \Cake\Collection\CollectionInterface
|
|
|
+ */
|
|
|
+ protected function newCollection(...$args)
|
|
|
+ {
|
|
|
+ return new Collection(...$args);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* {@inheritDoc}
|
|
|
*/
|
|
|
public function each(callable $c)
|
|
@@ -258,7 +271,7 @@ trait CollectionTrait
|
|
|
$group[$callback($value)][] = $value;
|
|
|
}
|
|
|
|
|
|
- return new Collection($group);
|
|
|
+ return $this->newCollection($group);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -272,7 +285,7 @@ trait CollectionTrait
|
|
|
$group[$callback($value)] = $value;
|
|
|
}
|
|
|
|
|
|
- return new Collection($group);
|
|
|
+ return $this->newCollection($group);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -292,7 +305,7 @@ trait CollectionTrait
|
|
|
$mr->emit(count($values), $key);
|
|
|
};
|
|
|
|
|
|
- return new Collection(new MapReduce($this->unwrap(), $mapper, $reducer));
|
|
|
+ return $this->newCollection(new MapReduce($this->unwrap(), $mapper, $reducer));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -318,10 +331,10 @@ trait CollectionTrait
|
|
|
*/
|
|
|
public function shuffle()
|
|
|
{
|
|
|
- $elements = $this->toArray();
|
|
|
+ $elements = $this->toList();
|
|
|
shuffle($elements);
|
|
|
|
|
|
- return new Collection($elements);
|
|
|
+ return $this->newCollection($elements);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -329,7 +342,7 @@ trait CollectionTrait
|
|
|
*/
|
|
|
public function sample($size = 10)
|
|
|
{
|
|
|
- return new Collection(new LimitIterator($this->shuffle(), 0, $size));
|
|
|
+ return $this->newCollection(new LimitIterator($this->shuffle(), 0, $size));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -337,7 +350,7 @@ trait CollectionTrait
|
|
|
*/
|
|
|
public function take($size = 1, $from = 0)
|
|
|
{
|
|
|
- return new Collection(new LimitIterator($this, $from, $size));
|
|
|
+ return $this->newCollection(new LimitIterator($this, $from, $size));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -345,7 +358,7 @@ trait CollectionTrait
|
|
|
*/
|
|
|
public function skip($howMany)
|
|
|
{
|
|
|
- return new Collection(new LimitIterator($this, $howMany));
|
|
|
+ return $this->newCollection(new LimitIterator($this, $howMany));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -412,19 +425,19 @@ trait CollectionTrait
|
|
|
|
|
|
$iterator = $this->optimizeUnwrap();
|
|
|
if (is_array($iterator)) {
|
|
|
- return new Collection(array_slice($iterator, $howMany * -1));
|
|
|
+ return $this->newCollection(array_slice($iterator, $howMany * -1));
|
|
|
}
|
|
|
|
|
|
if ($iterator instanceof Countable) {
|
|
|
$count = count($iterator);
|
|
|
|
|
|
if ($count === 0) {
|
|
|
- return new Collection([]);
|
|
|
+ return $this->newCollection([]);
|
|
|
}
|
|
|
|
|
|
$iterator = new LimitIterator($iterator, max(0, $count - $howMany), $howMany);
|
|
|
|
|
|
- return new Collection($iterator);
|
|
|
+ return $this->newCollection($iterator);
|
|
|
}
|
|
|
|
|
|
$generator = function ($iterator, $howMany) {
|
|
@@ -497,7 +510,7 @@ trait CollectionTrait
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- return new Collection($generator($iterator, $howMany));
|
|
|
+ return $this->newCollection($generator($iterator, $howMany));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -507,9 +520,9 @@ trait CollectionTrait
|
|
|
{
|
|
|
$list = new AppendIterator();
|
|
|
$list->append($this->unwrap());
|
|
|
- $list->append((new Collection($items))->unwrap());
|
|
|
+ $list->append($this->newCollection($items)->unwrap());
|
|
|
|
|
|
- return new Collection($list);
|
|
|
+ return $this->newCollection($list);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -531,7 +544,7 @@ trait CollectionTrait
|
|
|
*/
|
|
|
public function prepend($items)
|
|
|
{
|
|
|
- return (new Collection($items))->append($this);
|
|
|
+ return $this->newCollection($items)->append($this);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -556,7 +569,7 @@ trait CollectionTrait
|
|
|
$options = [
|
|
|
'keyPath' => $this->_propertyExtractor($keyPath),
|
|
|
'valuePath' => $this->_propertyExtractor($valuePath),
|
|
|
- 'groupPath' => $groupPath ? $this->_propertyExtractor($groupPath) : null
|
|
|
+ 'groupPath' => $groupPath ? $this->_propertyExtractor($groupPath) : null,
|
|
|
];
|
|
|
|
|
|
$mapper = function ($value, $key, $mapReduce) use ($options) {
|
|
@@ -586,7 +599,7 @@ trait CollectionTrait
|
|
|
$mapReduce->emit($result, $key);
|
|
|
};
|
|
|
|
|
|
- return new Collection(new MapReduce($this->unwrap(), $mapper, $reducer));
|
|
|
+ return $this->newCollection(new MapReduce($this->unwrap(), $mapper, $reducer));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -631,7 +644,7 @@ trait CollectionTrait
|
|
|
$parents[$key][$nestingKey] = $children;
|
|
|
};
|
|
|
|
|
|
- return (new Collection(new MapReduce($this->unwrap(), $mapper, $reducer)))
|
|
|
+ return $this->newCollection(new MapReduce($this->unwrap(), $mapper, $reducer))
|
|
|
->map(function ($value) use (&$isObject) {
|
|
|
/** @var \ArrayIterator $value */
|
|
|
return $isObject ? $value : $value->getArrayCopy();
|
|
@@ -689,7 +702,7 @@ trait CollectionTrait
|
|
|
*/
|
|
|
public function compile($preserveKeys = true)
|
|
|
{
|
|
|
- return new Collection($this->toArray($preserveKeys));
|
|
|
+ return $this->newCollection($this->toArray($preserveKeys));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -703,7 +716,7 @@ trait CollectionTrait
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- return new Collection($generator());
|
|
|
+ return $this->newCollection($generator());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -727,7 +740,7 @@ trait CollectionTrait
|
|
|
$modes = [
|
|
|
'desc' => TreeIterator::SELF_FIRST,
|
|
|
'asc' => TreeIterator::CHILD_FIRST,
|
|
|
- 'leaves' => TreeIterator::LEAVES_ONLY
|
|
|
+ 'leaves' => TreeIterator::LEAVES_ONLY,
|
|
|
];
|
|
|
|
|
|
return new TreeIterator(
|
|
@@ -761,7 +774,7 @@ trait CollectionTrait
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- return new Collection(
|
|
|
+ return $this->newCollection(
|
|
|
new RecursiveIteratorIterator(
|
|
|
new UnfoldIterator($this->unwrap(), $transformer),
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY
|
|
@@ -776,7 +789,7 @@ trait CollectionTrait
|
|
|
{
|
|
|
$result = $handler($this);
|
|
|
|
|
|
- return $result instanceof CollectionInterface ? $result : new Collection($result);
|
|
|
+ return $result instanceof CollectionInterface ? $result : $this->newCollection($result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -900,7 +913,7 @@ trait CollectionTrait
|
|
|
public function cartesianProduct(callable $operation = null, callable $filter = null)
|
|
|
{
|
|
|
if ($this->isEmpty()) {
|
|
|
- return new Collection([]);
|
|
|
+ return $this->newCollection([]);
|
|
|
}
|
|
|
|
|
|
$collectionArrays = [];
|
|
@@ -942,7 +955,7 @@ trait CollectionTrait
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return new Collection($result);
|
|
|
+ return $this->newCollection($result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -966,7 +979,7 @@ trait CollectionTrait
|
|
|
$result[] = array_column($arrayValue, $column);
|
|
|
}
|
|
|
|
|
|
- return new Collection($result);
|
|
|
+ return $this->newCollection($result);
|
|
|
}
|
|
|
|
|
|
/**
|