Passed
Push — 1.0.x ( 3a2c37...ccae28 )
by Julien
14:07 queued 06:59
created

RecursiveMap::process()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Support\Helper\Arr;
13
14
/**
15
 * Class RecursiveMap
16
 *
17
 * This class provides a way to recursively process the elements of an array using a callback function.
18
 */
19
class RecursiveMap
20
{
21 1
    public function __invoke(array $collection = [], callable $callback = null): array
22
    {
23 1
        return self::process($collection, $callback);
24
    }
25
    
26
    /**
27
     * Applies a callback function to each element of the given array recursively and returns a new array.
28
     *
29
     * @param array $collection The array to be processed.
30
     * 
31
     * @param ?callable $callback The callback function to be applied to each array element.
32
     *                           The callback function should accept one argument, which is the current array element,
33
     *                           and can return a modified value for that element.
34
     *
35
     * @return array The processed array with the callback function applied to each element.
36
     */
37 1
    public static function process(array $collection = [], callable $callback = null): array
38
    {
39 1
        $func = function ($item) use (&$func, &$callback) {
40 1
            return is_array($item) ? array_map($func, $item) : call_user_func($callback, $item);
41 1
        };
42
        
43 1
        return array_map($func, $collection);
44
    }
45
}
46