コード例 #1
0
ファイル: flatten.php プロジェクト: pablodip/felpado
/**
 * 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;
}
コード例 #2
0
ファイル: join.php プロジェクト: pablodip/felpado
/**
 * 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));
}
コード例 #3
0
ファイル: felpado.php プロジェクト: pablodip/felpado
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;
}
コード例 #4
0
ファイル: normalize_coll.php プロジェクト: pablodip/felpado
/**
 * 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;
}
コード例 #5
0
ファイル: assoc_in.php プロジェクト: pablodip/felpado
/**
 * 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;
}
コード例 #6
0
ファイル: dissoc.php プロジェクト: pablodip/felpado
/**
 * 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;
}
コード例 #7
0
ファイル: partition.php プロジェクト: pablodip/felpado
/**
 * 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);
}
コード例 #8
0
ファイル: to_arrayTest.php プロジェクト: pablodip/felpado
 /**
  * @dataProvider invalidValueProvider
  * @expectedException \InvalidArgumentException
  */
 public function testInvalid($value)
 {
     f\to_array($value);
 }
コード例 #9
0
ファイル: last.php プロジェクト: pablodip/felpado
/**
 * 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);
}
コード例 #10
0
ファイル: reverse.php プロジェクト: pablodip/felpado
/**
 * 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));
}
コード例 #11
0
ファイル: reverse_indexed.php プロジェクト: pablodip/felpado
/**
 * 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);
}
コード例 #12
0
ファイル: rename_keysTest.php プロジェクト: pablodip/felpado
 /**
  * @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')));
 }
コード例 #13
0
ファイル: rename_keyTest.php プロジェクト: pablodip/felpado
 /**
  * @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'));
 }
コード例 #14
0
ファイル: assoc.php プロジェクト: pablodip/felpado
/**
 * 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;
}
コード例 #15
0
ファイル: distinct.php プロジェクト: pablodip/felpado
/**
 * 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)));
}
コード例 #16
0
ファイル: drop_last.php プロジェクト: pablodip/felpado
/**
 * 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;
}
コード例 #17
0
ファイル: conjoin.php プロジェクト: pablodip/felpado
/**
 * 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;
}
コード例 #18
0
ファイル: construct.php プロジェクト: pablodip/felpado
/**
 * 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;
}