/** * Returns a function that invokes method `$method` with arguments `$methodArguments` on the object * * @param string $methodName * @param array $arguments * @return callable */ function invoker($methodName, array $arguments = []) { InvalidArgumentException::assertMethodName($methodName, __FUNCTION__, 1); return static function ($object) use($methodName, $arguments) { return $object->{$methodName}(...$arguments); }; }
/** * Returns a function that expects an object as the first param and tries to invoke the given method on it * * @param string $methodName * @param array $arguments * @param mixed $defaultValue * @return callable */ function partial_method($methodName, array $arguments = [], $defaultValue = null) { InvalidArgumentException::assertMethodName($methodName, __FUNCTION__, 1); return function ($object) use($methodName, $arguments, $defaultValue) { if (!is_callable([$object, $methodName])) { return $defaultValue; } return $object->{$methodName}(...$arguments); }; }
/** * Calls the method named by $methodName on first value in the collection. Any extra arguments passed to invoke will be * forwarded on to the method invocation. * * @param Traversable|array $collection * @param string $methodName * @param array $arguments * @return array */ function invoke_first($collection, $methodName, array $arguments = []) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); InvalidArgumentException::assertMethodName($methodName, __FUNCTION__, 2); foreach ($collection as $element) { $callback = [$element, $methodName]; if (is_callable($callback)) { return $callback(...$arguments); } } return null; }
/** * Calls the method named by $methodName on each value in the collection. Any extra arguments passed to invoke will be * forwarded on to the method invocation. * * @param Traversable|array $collection * @param string $methodName * @param array $arguments * @return array */ function invoke($collection, $methodName, array $arguments = []) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); InvalidArgumentException::assertMethodName($methodName, __FUNCTION__, 2); $aggregation = []; foreach ($collection as $index => $element) { $value = null; $callback = [$element, $methodName]; if (is_callable($callback)) { $value = $callback(...$arguments); } $aggregation[$index] = $value; } return $aggregation; }
/** * Calls the method named by $methodName on last value in the collection. Any extra arguments passed to invoke will be * forwarded on to the method invocation. * * @param Traversable|array $collection * @param string $methodName * @param array $arguments * @return array */ function invoke_last($collection, $methodName, array $arguments = array()) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); InvalidArgumentException::assertMethodName($methodName, __FUNCTION__, 2); $lastCallback = null; foreach ($collection as $element) { $callback = array($element, $methodName); if (is_callable($callback)) { $lastCallback = $callback; } } if (!$lastCallback) { return null; } return call_user_func_array($lastCallback, $arguments); }