KeyRenameTransformer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A exec() 0 10 3
A __construct() 0 4 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\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