/** * f\flatten($coll) * * Returns a new collection without nesting combinations. * * f\flatten(array(1, array(2, 3))); * => array(1, 2, 3) * * f\flatten(array(1, 2, 3)); * => array(1, 2, 3) * * f\first(array(1, array(2, array(3)))); * => array(1, 2, 3) */ function flatten($coll) { $result = array(); foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator(f\to_array($coll))) as $v) { $result[] = $v; } return $result; }
/** * f\join($coll) * f\join($separator, $coll) * * Returns a string joining all the elements in coll, with an optional separator. * * f\join(array(1, 2, 3)); * => "123" * * f\join(', ', array(1, 2, 3)); * => "1, 2, 3" */ function join() { $args = func_get_args(); if (count($args) == 1) { $separator = ''; $coll = $args[0]; } else { $separator = $args[0]; $coll = $args[1]; } return implode($separator, f\to_array($coll)); }
function _coll_depth($array, $depth) { $first = f\first($depth); if (f\contains($array, $first)) { $arrayIn = f\to_array(f\get($array, $first)); $inRest = f\rest($depth); if (count($inRest)) { return f\_coll_depth($arrayIn, $inRest); } return $arrayIn; } return false; }
/** * f\normalize_coll($coll, $normalizers) * * Returns a new collection normalizing the elements indicating in normalizers. * It accepts param rules as normalizers. * It's similar to map_indexed. * * f\normalize_coll(array('a' => 1.0), array('a' => 'intval')); * => array('a' => 1) * * // elements without normalizers are returned without modification * f\normalize_coll(array('a' => 1.0, 'b' => 2.0), array('a' => 'intval')); * => array('a' => 1, 'b' => 2.0) * * // with param rules * f\normalize_coll(array('a' => 1.0), array('a' => f\required('normalizer' => 'intval'))); * => array('a' => 1) */ function normalize_coll($collection, $normalizers) { $result = f\to_array($collection); foreach ($normalizers as $name => $normalizer) { if ($normalizer instanceof param_rule) { $normalizer = $normalizer->getNormalizer(); if (f\not($normalizer)) { continue; } } $result[$name] = call_user_func($normalizer, f\get($collection, $name)); } return $result; }
/** * f\assoc_in($coll, $in, $value) * * Returns an array based on coll with value associated in the nested structure of in. * * f\assoc_in(array('a' => 1), array('b', 'b1'), 2); * => array('a' => 1, 'b' => array('b1' => 2)) * * // does nothing without in * f\assoc_in(array('a' => 1), array(), 2); * => array('a' => 1) * * // supports infinite nesting * f\assoc_in(array(), array('a', 'a1', 'a1I', 'a1IA'), 1); * => array('a' => array('a1' => array('a1I' => array('a1IA' => 1)))) */ function assoc_in($coll, $in, $value) { if (empty($in)) { return $coll; } $array = f\to_array($coll); $current =& $array; foreach ($in as $k) { if (f\not(f\get_or($current, $k, null))) { $current = f\assoc($current, $k, array()); } $current =& $current[$k]; } $current = $value; return $array; }
/** * f\dissoc($coll, $key) * * Returns an array based on coll with value associated with key removed. * * f\dissoc(array('a' => 1, 'b' => 2), 'b'); * => array('a' => 1) * * f\dissoc(array('a' => 1, 'b' => 2, 'c' => 3), 'b'); * => array('a' => 1, 'c' => 3) */ function dissoc($collection, $key) { $result = f\to_array($collection); unset($result[$key]); return $result; }
/** * f\partition($n, $coll) * * Returns a new collection from coll parted in chunks of n length. * * f\partition(2, range(1, 6)); * => array(array(1, 2), array(3, 4), array(5, 6) * * f\partition(3, range(1, 6)); * => array(array(1, 2, 3), array(4, 5, 6)) */ function partition($n, $coll) { return array_chunk(f\to_array($coll), $n); }
/** * @dataProvider invalidValueProvider * @expectedException \InvalidArgumentException */ public function testInvalid($value) { f\to_array($value); }
/** * f\last($coll) * * Returns the last value of collection, or null if collection is empty. * * f\last(array(1, 2, 3)); * => 3 * * f\last(array()); * => null */ function last($coll) { $array = f\to_array($coll); $last = end($array); return $last !== false ? $last : (count($array) ? false : null); }
/** * f/reverse($coll) * * Returns a new collection in reversed order. * * f\reverse(array(1, 2, 3)); * => array(3, 2, 1) */ function reverse($coll) { return array_reverse(f\to_array($coll)); }
/** * f/reverse_indexed($coll) * * Same than reverse but keeping the index. * * f\reverse_indexed(array('a' => 1, 'b' => 2, 'c' => 3)); * => array('c' => 3, 'b' => 2, 'a' => 1) */ function reverse_indexed($coll) { return array_reverse(f\to_array($coll), true); }
/** * @dataProvider renameKeysProvider */ public function testRenameKeys($coll) { $this->assertSame(array('one' => 3, 'two' => 9), f\rename_keys($coll, array('foo' => 'one', 'bar' => 'two'))); $this->assertSame(array('one' => 3, 'bar' => 9), f\rename_keys(f\to_array($coll), array('foo' => 'one', 'bar' => 'bar'))); }
/** * @dataProvider renameKeyProvider */ public function testRenameKey($coll) { $this->assertSame(array('foo' => 3, 'ups' => 9), f\rename_key($coll, 'bar', 'ups')); $this->assertSame(f\to_array($coll), f\rename_key($coll, 'bar', 'bar')); }
/** * f\assoc($coll, $key, $value) * * Returns an array based on coll with value associated with key. * * f\assoc(array('a' => 1, 'b' => 2), 'c', 3); * => array('a' => 1, 'b' => 2, 'c' => 3) */ function assoc($collection, $key, $value) { $result = f\to_array($collection); $result[$key] = $value; return $result; }
/** * f\distinct($coll) * * Returns a new coll without duplicates. * * f\distinct(array(1, 2, 3, 2, 4, 5, 3, 1)); * => array(1, 2, 3, 4, 5) */ function distinct($coll) { return array_values(array_unique(f\to_array($coll))); }
/** * f\drop_last($coll) * * Returns an array based on coll with the last element removed. * * f\drop_last(array('a' => 1, 'b' => 2)); * => array('a' => 1) */ function drop_last($coll) { $result = f\to_array($coll); unset($result[f\last(f\keys($result))]); return $result; }
/** * f\conjoin($coll, $value) * * Returns an array based on collection with value added. * * f\conjoin(array(1, 2, 3), 4); * => array(1, 2, 3, 4) */ function conjoin($coll, $value) { $result = f\to_array($coll); $result[] = $value; return $result; }
/** * f\construct($first, $rest) * * Returns an array with first and rest. * * f\construct(1, array(2, 3, 4)); * => array(1, 2, 3, 4) */ function construct($first, $rest) { $array = f\to_array($rest); array_unshift($array, $first); return $array; }