本文整理汇总了PHP中Functional\Exceptions\InvalidArgumentException::assertCollection方法的典型用法代码示例。如果您正苦于以下问题:PHP InvalidArgumentException::assertCollection方法的具体用法?PHP InvalidArgumentException::assertCollection怎么用?PHP InvalidArgumentException::assertCollection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Functional\Exceptions\InvalidArgumentException
的用法示例。
在下文中一共展示了InvalidArgumentException::assertCollection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: zip_all
/**
* Recombines arrays by index (column) and applies a callback optionally
*
* When the input collections are different lengths the resulting collections
* will all have the length which is required to fit all the keys
*
* @param $args array|Traversable $collection One or more callbacks
* @return array
*/
function zip_all(...$args)
{
/** @var callable|null $callback */
$callback = null;
if (is_callable(end($args))) {
$callback = array_pop($args);
}
foreach ($args as $position => $arg) {
InvalidArgumentException::assertCollection($arg, __FUNCTION__, $position + 1);
}
$resultKeys = [];
foreach ($args as $arg) {
foreach ($arg as $index => $value) {
$resultKeys[] = $index;
}
}
$resultKeys = array_unique($resultKeys);
$result = [];
foreach ($resultKeys as $key) {
$zipped = [];
foreach ($args as $arg) {
$zipped[] = isset($arg[$key]) ? $arg[$key] : null;
}
$result[$key] = $zipped;
}
if ($callback !== null) {
foreach ($result as $key => $column) {
$result[$key] = $callback(...$column);
}
}
return $result;
}
示例2: 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, callable $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
foreach ($collection as $index => $element) {
$callback($element, $index, $collection);
}
}
示例3: zip
/**
* Recombines arrays by index and applies a callback optionally
*
* @param $args array|Traversable $collection One or more callbacks
* @return array
*/
function zip($arg)
{
$args = func_get_args();
$callback = null;
if (is_callable(end($args))) {
$callback = array_pop($args);
}
foreach ($args as $position => $arg) {
InvalidArgumentException::assertCollection($arg, __FUNCTION__, $position + 1);
}
$result = [];
foreach ((array) reset($args) as $index => $value) {
$zipped = [];
foreach ($args as $arg) {
$zipped[] = isset($arg[$index]) ? $arg[$index] : null;
}
if ($callback !== null) {
/** @var callable $callback */
// $zipped = $callback(...$zipped);
$zipped = call_user_func_array($callback, $zipped);
}
$result[] = $zipped;
}
return $result;
}
示例4: 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);
}
}
示例5: testHashIterator
public function testHashIterator()
{
$flat = flat_map(new ArrayIterator(['ka' => 'a', 'kb' => ['b'], 'kc' => ['C' => 'c'], 'kd' => [['d']], 'ke' => null, null]), function ($v, $k, $collection) {
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 3);
return $v;
});
$this->assertSame(['a', 'b', 'c', ['d']], $flat);
}
示例6: 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);
}
示例7: testDuplicateKeys
public function testDuplicateKeys()
{
$fn = function ($v, $k, $collection) {
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 3);
return $k[0];
};
$this->assertSame(['k' => 'val2'], reindex($this->hash, $fn));
}
示例8: reduce_left
/**
* @param Traversable|array $collection
* @param callable $callback
* @param mixed $initial
* @return array
*/
function reduce_left($collection, callable $callback, $initial = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
foreach ($collection as $index => $value) {
$initial = $callback($value, $index, $collection, $initial);
}
return $initial;
}
示例9: 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, callable $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
$aggregation = [];
foreach ($collection as $index => $element) {
$aggregation[$index] = $callback($element, $index, $collection);
}
return $aggregation;
}
示例10: flatten
/**
* Takes a nested combination of collections and returns their contents as a single, flat array.
* Does not preserve indexs.
*
* @param Traversable|array $collection
* @return array
*/
function flatten($collection)
{
Exceptions\InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($collection));
$result = array();
foreach ($it as $val) {
$result[] = $val;
}
return $result;
}
示例11: none
/**
* Returns true if all of the elements in the collection pass the callback falsy test. Opposite of Functional\all().
* Callback arguments will be element, index, collection.
*
* @param Traversable|array $collection
* @param callable $callback
* @return bool
*/
function none($collection, callable $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
foreach ($collection as $index => $element) {
if ($callback($element, $index, $collection)) {
return false;
}
}
return true;
}
示例12: contains
/**
* Returns true if the collection contains the given value. If the third parameter is
* true values will be compared in strict mode
*
* @param Traversable|array $collection
* @param mixed $value
* @param bool $strict
* @return bool
*/
function contains($collection, $value, $strict = true)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
foreach ($collection as $element) {
if ($value === $element || !$strict && $value == $element) {
return true;
}
}
return false;
}
示例13: 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;
}
示例14: 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, callable $callback)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
$partitions = [0 => [], 1 => []];
foreach ($collection as $index => $element) {
$partitionKey = $callback($element, $index, $collection) ? 0 : 1;
$partitions[$partitionKey][$index] = $element;
}
return $partitions;
}
示例15: true
/**
* Returns true if all elements of the collection are strictly true
*
* @param Traversable|array $collection
* @return bool
*/
function true($collection)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
foreach ($collection as $value) {
if ($value !== true) {
return false;
}
}
return true;
}