KeyRenameTransformer::exec()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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