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; |
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: