本文整理汇总了PHP中Functional\Exceptions\InvalidArgumentException::assertCallback方法的典型用法代码示例。如果您正苦于以下问题:PHP InvalidArgumentException::assertCallback方法的具体用法?PHP InvalidArgumentException::assertCallback怎么用?PHP InvalidArgumentException::assertCallback使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Functional\Exceptions\InvalidArgumentException
的用法示例。
在下文中一共展示了InvalidArgumentException::assertCallback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: memoize
/**
* Memoizes callbacks and returns their value instead of calling them
*
* @param callable $callback Callable closure or function
* @param array $arguments Arguments
* @param array|string $key Optional memoize key to override the auto calculated hash
* @return mixed
*/
function memoize($callback, array $arguments = array(), $key = null)
{
static $storage = array();
if ($callback === null) {
$storage = array();
return null;
}
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 1);
static $keyGenerator = null;
if (!$keyGenerator) {
$keyGenerator = function ($value) use(&$keyGenerator) {
$type = gettype($value);
if ($type === 'array') {
$key = join(':', map($value, $keyGenerator));
} elseif ($type === 'object') {
$key = get_class($value) . ':' . spl_object_hash($value);
} else {
$key = (string) $value;
}
return $key;
};
}
if ($key === null) {
$key = $keyGenerator(array_merge(array($callback), $arguments));
} else {
$key = $keyGenerator($key);
}
if (!isset($storage[$key]) && !array_key_exists($key, $storage)) {
$storage[$key] = call_user_func_array($callback, $arguments);
}
return $storage[$key];
}
示例2: head
/**
* Alias for Functional\first
*
* @param Traversable|array $collection
* @param callable $callback
* @return mixed
*/
function head($collection, $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
if ($callback !== null) {
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
}
return first($collection, $callback);
}
示例3: each
/**
* Iterates over a collection of elements, yielding each in turn to a callback function. Each invocation of $callback
* is called with three arguments: (element, index, collection)
*
* @param Traversable|array $collection
* @param callable $callback
* @return null
*/
function each($collection, $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
foreach ($collection as $index => $element) {
call_user_func($callback, $element, $index, $collection);
}
}
示例4: reduce_left
/**
* @param Traversable|array $collection
* @param callable $callback
* @param mixed $initial
* @return array
*/
function reduce_left($collection, $callback, $initial = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
foreach ($collection as $index => $value) {
$initial = call_user_func($callback, $value, $index, $collection, $initial);
}
return $initial;
}
示例5: map
/**
* Produces a new array of elements by mapping each element in collection through a transformation function (callback).
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
*/
function map($collection, $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
$aggregation = array();
foreach ($collection as $index => $element) {
$aggregation[$index] = call_user_func($callback, $element, $index, $collection);
}
return $aggregation;
}
示例6: partition
/**
* Partitions a collection by callback result. The truthy partition is the first one
* (array index "0"), the falsy the second one (array index "1")
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
*/
function partition($collection, $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
$partitions = array(0 => array(), 1 => array());
foreach ($collection as $index => $element) {
$partitionKey = call_user_func($callback, $element, $index, $collection) ? 0 : 1;
$partitions[$partitionKey][$index] = $element;
}
return $partitions;
}
示例7: every
/**
* Returns true if every value in the collection passes the callback truthy test. Opposite of Functional\none().
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return bool
*/
function every($collection, $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
foreach ($collection as $index => $element) {
if (!call_user_func($callback, $element, $index, $collection)) {
return false;
}
}
return true;
}
示例8: with
/**
* Invoke a callback on a value if the value is not null
*
* @param mixed $value
* @param callable $callback
* @return mixed
*/
function with($value, $callback)
{
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
if ($value === null) {
return null;
}
if (is_callable($value)) {
$value = $value();
}
return $callback($value);
}
示例9: with
/**
* Invoke a callback on a value if the value is not null
*
* @param mixed $value
* @param callable $callback
* @param bool $invokeValue Set to false to not invoke $value if it is a callable. Will be removed in 2.0
* @return mixed
*/
function with($value, callable $callback, $invokeValue = true)
{
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
if ($value === null) {
return null;
}
if ($invokeValue && is_callable($value)) {
trigger_error('Invoking the value is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
$value = $value();
}
return $callback($value);
}
示例10: reduce_right
/**
* @param Traversable|array $collection
* @param callable $callback
* @param mixed $initial
* @return array
*/
function reduce_right($collection, $callback, $initial = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
$data = array();
foreach ($collection as $index => $value) {
$data[] = array($index, $value);
}
while (list($index, $value) = array_pop($data)) {
$initial = call_user_func($callback, $value, $index, $collection, $initial);
}
return $initial;
}
示例11: last
/**
* Looks through each element in the collection, returning the last one that passes a truthy test (callback).
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return mixed
*/
function last($collection, $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
if ($callback !== null) {
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
}
$match = null;
foreach ($collection as $index => $element) {
if ($callback === null || call_user_func($callback, $element, $index, $collection)) {
$match = $element;
}
}
return $match;
}
示例12: drop_last
/**
* Drop all elements from a collection after callback returns true
*
* @param Traversable|array $collection
* @param callable|integer $callback
* @return array
*/
function drop_last($collection, callable $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
$result = [];
$drop = false;
foreach ($collection as $index => $element) {
if (!$drop && !$callback($element, $index, $collection)) {
break;
}
$result[$index] = $element;
}
return $result;
}
示例13: group
/**
* Groups a collection by index returned by callback.
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
*/
function group($collection, $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
$groups = array();
foreach ($collection as $index => $element) {
$groupKey = call_user_func($callback, $element, $index, $collection);
InvalidArgumentException::assertValidArrayKey($groupKey, __FUNCTION__);
if (!isset($groups[$groupKey])) {
$groups[$groupKey] = array();
}
$groups[$groupKey][$index] = $element;
}
return $groups;
}
示例14: first
/**
* Looks through each element in the collection, returning the first one that passes a truthy test (callback). The
* function returns as soon as it finds an acceptable element, and doesn't traverse the entire collection. Callback
* arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return mixed
*/
function first($collection, $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
if ($callback !== null) {
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
}
foreach ($collection as $index => $element) {
if ($callback === null) {
return $element;
}
if (call_user_func($callback, $element, $index, $collection)) {
return $element;
}
}
}
示例15: sort
/**
* Sorts a collection with a user-defined function, optionally preserving array keys
*
* @param Traversable|array $collection
* @param callable $callback
* @param bool $preserveKeys
* @return array
*/
function sort($collection, $callback, $preserveKeys = false)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);
InvalidArgumentException::assertBoolean($preserveKeys, __FUNCTION__, 3);
if ($collection instanceof \Traversable) {
$array = iterator_to_array($collection);
} else {
$array = $collection;
}
$fn = $preserveKeys ? 'uasort' : 'usort';
$fn($array, function ($left, $right) use($callback, $collection) {
return call_user_func($callback, $left, $right, $collection);
});
return $array;
}