Option::run()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 9.2
cc 4
eloc 13
nc 4
nop 2
crap 4
1
<?php
2
3
namespace GFG\Mapper\Data\Type\Assoc;
4
5
use GFG\Mapper\Data\MapperInterface;
6
use GFG\Mapper\Data\Type;
7
8
/**
9
 * For assoc option structures like:
10
 *
11
 * $data = [
12
 *     'myObject' => [
13
 *         4 => [5, 6, 7]
14
 *     ]
15
 * ];
16
 * $structure = [
17
 *     'myObject' => [
18
 *         'type' => 'assoc-option',
19
 *         'prefix' => 'my_object',
20
 *         'use' => 'raw'   // if raw, uses non mapped keys to fetch values
21
 *     ]
22
 * ];
23
 */
24
class Option extends Type\Option
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function run(&$data, $key = null)
30
    {
31 2
        $new = [];
32 2
        foreach ($data as $key => $value) {
33 2
            if (is_array($value)) { // check for option to be sure!
34 2
                $oldKey = $key;
35
36 2
                $key = $this->get($key);
37 2
                if ($this->options['use'] == 'raw') {
38 1
                    parent::run($value, $oldKey);
39 1
                } else {
40 1
                    parent::run($value, $key);
41
                }
42
43 2
                unset($data[$oldKey]);
44 2
                $new[$key] = $value;
45 2
            }
46 2
        }
47 2
        $data += $new; // yep, union operator here!
48 2
    }
49
}
50