Passed
Push — master ( 7bba5d...c1c3fd )
by Fabrice
02:35
created

KeyRenameTransformer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\YaEtl\Transformers\TransformerAbstract;
13
14
/**
15
 * Class KeyRenameTransformer
16
 */
17
class KeyRenameTransformer extends TransformerAbstract
18
{
19
    /**
20
     * array[inputKeyName] = 'outputKeyName'
21
     *
22
     * @var array
23
     */
24
    protected $aliases;
25
26
    /**
27
     * @param array $aliases
28
     */
29
    public function __construct(array $aliases)
30
    {
31
        $this->aliases = $aliases;
32
    }
33
34
    /**
35
     * Replace keys in array
36
     * This method does not preserve incoming order
37
     *
38
     * @param mixed $record
39
     *
40
     * @return mixed
41
     */
42
    public function exec($record)
43
    {
44
        foreach ($this->aliases as $inputKeyName => $outputKeyName) {
45
            if (\array_key_exists($inputKeyName, $record)) {
46
                $record[$outputKeyName] = $record[$inputKeyName];
47
                unset($record[$inputKeyName]);
48
            }
49
        }
50
51
        return $record;
52
    }
53
}
54