Completed
Push — master ( beda51...8adbc5 )
by Dmitriy
02:01
created

Mapper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 99
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A toTableRow() 0 14 2
A fromTableRow() 0 8 1
A fromTableRows() 0 9 2
A getIntersectValuesAsKeys() 0 12 3
B serialize() 0 12 5
1
<?php
2
3
namespace T4webInfrastructure;
4
5
use T4webDomainInterface\EntityInterface;
6
7
class Mapper
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $columnsAsAttributesMap;
13
14
    /**
15
     * @param array $columnsAsAttributesMap
16
     * @param array $serialized
17
     */
18
    public function __construct(array $columnsAsAttributesMap, array $serialized = [])
19
    {
20
        $this->columnsAsAttributesMap = $columnsAsAttributesMap;
21
        $this->serialized = $serialized;
0 ignored issues
show
Bug introduced by
The property serialized does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
24
    /**
25
     * @param EntityInterface $entity
26
     * @return array
27
     */
28
    public function toTableRow(EntityInterface $entity)
29
    {
30
        $objectState = $entity->extract(array_values($this->columnsAsAttributesMap));
31
32
        if (array_key_exists('id', $objectState)) {
33
            unset($objectState['id']);
34
        }
35
36
        $row = $this->getIntersectValuesAsKeys($this->columnsAsAttributesMap, $objectState);
37
38
        $row = $this->serialize($row);
39
40
        return $row;
41
    }
42
43
    /**
44
     * @param array $row
45
     * @return array
46
     */
47
    public function fromTableRow(array $row)
48
    {
49
        $row = $this->serialize($row, false);
50
        
51
        $attributesValues = $this->getIntersectValuesAsKeys(array_flip($this->columnsAsAttributesMap), $row);
52
53
        return $attributesValues;
54
    }
55
56
    /**
57
     * @param array $rows
58
     * @return array
59
     */
60
    public function fromTableRows(array $rows)
61
    {
62
        $attributesValues = [];
63
        foreach ($rows as $row) {
64
            $attributesValues[] = $this->fromTableRow($row);
65
        }
66
67
        return $attributesValues;
68
    }
69
70
    /**
71
     * @param array $array1
72
     * @param array $array2
73
     * @return array
74
     */
75
    private function getIntersectValuesAsKeys($array1, $array2)
76
    {
77
        $result = [];
78
79
        foreach ($array1 as $key => $value) {
80
            if (array_key_exists($value, $array2)) {
81
                $result[$key] = $array2[$value];
82
            }
83
        }
84
85
        return $result;
86
    }
87
88
    /**
89
     * @param array $row
90
     * @param bool $serialize
91
     * @return array
92
     */
93
    private function serialize(array $row, $serialize = true)
94
    {
95
        foreach ($this->serialized as $column => $serializer) {
96
            if (isset($row[$column])) {
97
                if ($serializer == 'json') {
98
                    $row[$column] = $serialize ? json_encode($row[$column], true) : json_decode($row[$column], true);
99
                }
100
            }
101
        }
102
103
        return $row;
104
    }
105
}
106