Manager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace GFG\Mapper\Data;
4
5
class Manager
6
{
7
    /**
8
     * @var MapperInterface
9
     */
10
    private $mapper;
11
12
    /**
13
     * @var MapperFactory
14
     */
15
    private $factory;
16
17
    /**
18
     * @param MapperInterface $mapper
19
     * @param MapperFactory $factory
20
     */
21 1
    public function __construct(
22
        MapperInterface $mapper,
23
        MapperFactory $factory
24
    ) {
25 1
        $this->mapper    = $mapper;
26 1
        $this->factory   = $factory;
27 1
    }
28
29
    /**
30
     * Map a given data based on a given structure
31
     *
32
     * @param array $data Data to be mapped
33
     * @param array $structure How to map
34
     * @param string $basePrefix
35
     * @return void will change $data by reference
36
     */
37 1
    public function map(array &$data, array $structure, $basePrefix = null)
38
    {
39 1
        foreach ($structure as $key => $value) {
40
41 1
            if (!isset($data[$key])) {
42
                continue;
43
            }
44
45 1
            $keyData = &$data[$key];
46
47 1
            if (!isset($value['prefix'])) {
48 1
                $this->map($keyData, $value);
49 1
            } else {
50 1
                foreach ((array) $value['type'] as $type) {
51 1
                    $use   = isset($value['use'])   ? $value['use']   : '';
52 1
                    $inner = isset($value['inner']) ? $value['inner'] : '';
53 1
                    $type  = $this->factory->create(
54 1
                        $this,
55 1
                        $type,
56 1
                        $this->mapper,
57
                        [
58 1
                            'prefix' => $basePrefix . $value['prefix'],
59 1
                            'use'    => $use,
60
                            'inner'  => $inner
61 1
                        ]
62 1
                    );
63 1
                    $type->run($keyData);
64 1
                }
65
            }
66 1
        }
67 1
    }
68
}
69