lithium\util\Collection::invoke()
Handles dispatching of methods against all items in the collection.
Parameters
-
string
$method
The name of the method to call on each instance in the collection.
-
array
$params
The parameters to pass on each method call.
-
array
$options
Specifies options for how to run the given method against the object collection. The available options are:
'collect'
: Iftrue
, the results of this method call will be returned wrapped in a newCollection
object or subclass.'merge'
: Used primarily if the method being invoked returns an array. If set totrue
, merges all results arrays into one.
Returns
mixedReturns either an array of the return values of the methods, or the return
values wrapped in a Collection
instance.
Source
public function invoke($method, array $params = [], array $options = []) {
$class = get_class($this);
$defaults = ['merge' => false, 'collect' => false];
$options += $defaults;
$data = [];
foreach ($this as $object) {
$value = call_user_func_array([&$object, $method], $params);
($options['merge']) ? $data = array_merge($data, $value) : $data[$this->key()] = $value;
}
return ($options['collect']) ? new $class(compact('data')) : $data;
}