當前位置: 首頁>>代碼示例>>PHP>>正文


PHP InvalidArgumentException::assertCallback方法代碼示例

本文整理匯總了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];
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:40,代碼來源:Memoize.php

示例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);
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:15,代碼來源:Head.php

示例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);
    }
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:16,代碼來源:Each.php

示例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;
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:15,代碼來源:ReduceLeft.php

示例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;
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:18,代碼來源:Map.php

示例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;
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:19,代碼來源:Partition.php

示例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;
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:19,代碼來源:Every.php

示例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);
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:18,代碼來源:With.php

示例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);
}
開發者ID:lstrojny,項目名稱:functional-php,代碼行數:20,代碼來源:With.php

示例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;
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:19,代碼來源:ReduceRight.php

示例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;
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:22,代碼來源:Last.php

示例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;
}
開發者ID:tdomarkas,項目名稱:functional-php,代碼行數:21,代碼來源:DropLast.php

示例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;
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:22,代碼來源:Group.php

示例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;
        }
    }
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:24,代碼來源:First.php

示例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;
}
開發者ID:RightThisMinute,項目名稱:responsive-images-php,代碼行數:24,代碼來源:Sort.php


注:本文中的Functional\Exceptions\InvalidArgumentException::assertCallback方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。