Passed
Push — master ( a7cb98...edba21 )
by Fabrice
03:13
created

ArrayMapRecursiveTransformer::arrayMapRecursive()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
/*
4
 * This file is part of YaEtl
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Transformers\Arrays;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\YaEtl\Transformers\TransformerAbstract;
14
15
/**
16
 * Class ArrayMapRecursiveTransformer
17
 */
18
class ArrayMapRecursiveTransformer extends TransformerAbstract
19
{
20
    /**
21
     * Any callable with one argument which returns something
22
     *
23
     * @var callable
24
     */
25
    protected $mapper;
26
27
    /**
28
     * @param callable $mapper
29
     *
30
     * @throws NodalFlowException
31
     */
32
    public function __construct(callable $mapper)
33
    {
34
        $this->mapper = $mapper;
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the arrayMapRecursive call
40
     *
41
     * @param array $record
42
     *
43
     * @return array
44
     */
45
    public function exec($record = null)
46
    {
47
        return $this->arrayMapRecursive($record);
0 ignored issues
show
Bug introduced by
It seems like $record can also be of type null; however, parameter $record of fab2s\YaEtl\Transformers...er::arrayMapRecursive() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        return $this->arrayMapRecursive(/** @scrutinizer ignore-type */ $record);
Loading history...
48
    }
49
50
    protected function arrayMapRecursive(array $record): array
51
    {
52
        $out = [];
53
        foreach ($record as $key => $value) {
54
            $out[$key] = is_array($value) ? $this->arrayMapRecursive($value) : call_user_func($this->mapper, $value);
55
        }
56
57
        return $out;
58
    }
59
}
60