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

RecursiveMap   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 25
ccs 7
cts 7
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 3 1
A process() 0 7 2
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