|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Borobudur-Cqrs package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Borobudur\Cqrs\ReadModel; |
|
12
|
|
|
|
|
13
|
|
|
use Borobudur\Bus\Message\MessageMapperTrait; |
|
14
|
|
|
use Borobudur\Parameterize\Mapper\ParameterMapper; |
|
15
|
|
|
use Borobudur\Parameterize\ParameterBag; |
|
16
|
|
|
use Borobudur\Serialization\Serializer\Mixin\DeserializerTrait; |
|
17
|
|
|
use Borobudur\Serialization\Serializer\Mixin\SerializerTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Iqbal Maulana <[email protected]> |
|
21
|
|
|
* @created 8/18/15 |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractReadModel implements ReadModelInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use SerializerTrait, DeserializerTrait, MessageMapperTrait { |
|
26
|
|
|
SerializerTrait::serialize as serializeData; |
|
27
|
|
|
DeserializerTrait::deserialize as deserializeData; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function relations() |
|
34
|
|
|
{ |
|
35
|
|
|
return array(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function mappings() |
|
42
|
|
|
{ |
|
43
|
|
|
return array(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function deserialize(array $attributes) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($mappings = static::mappings()) { |
|
|
|
|
|
|
52
|
|
|
$record = static::mapBaseOnKeys($attributes, array_flip(static::cleanMappingKeys($attributes))); |
|
53
|
|
|
$attributes = static::fixedDeserializeMapping($record, $attributes); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return static::deserializeData($attributes); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function serialize() |
|
63
|
|
|
{ |
|
64
|
|
|
$serialized = $this->serializeData(); |
|
65
|
|
|
|
|
66
|
|
|
if ($mappings = static::mappings()) { |
|
|
|
|
|
|
67
|
|
|
$serialized = static::mapBaseOnKeys($serialized, static::cleanMappingKeys($serialized)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $serialized; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Mapping data base on mapping keys. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $attributes |
|
77
|
|
|
* @param array $mappingKeys |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
protected static function mapBaseOnKeys(array $attributes, array $mappingKeys) |
|
82
|
|
|
{ |
|
83
|
|
|
return (array) ParameterMapper::map(new ParameterBag($attributes), $mappingKeys); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param array $serialized |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
|
|
protected static function cleanMappingKeys(array $serialized) |
|
92
|
|
|
{ |
|
93
|
|
|
return static::mergeMappingKeys(array_keys($serialized), static::mappings()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $attributesKeys |
|
98
|
|
|
* @param array $mappingKeys |
|
99
|
|
|
* |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
|
|
protected static function mergeMappingKeys(array $attributesKeys, array $mappingKeys) |
|
103
|
|
|
{ |
|
104
|
|
|
$diffs = array_diff($attributesKeys, static::extraKeys($mappingKeys)); |
|
105
|
|
|
|
|
106
|
|
|
return array_merge(array_combine($diffs, $diffs), $mappingKeys); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $attributesKeys |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
protected static function extraKeys(array $attributesKeys) |
|
115
|
|
|
{ |
|
116
|
|
|
$extracted = array(); |
|
117
|
|
|
foreach (array_keys($attributesKeys) as $key) { |
|
118
|
|
|
$parts = explode('.', $key); |
|
119
|
|
|
if (!in_array($parts[0], $extracted)) { |
|
120
|
|
|
$extracted[] = $parts[0]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $extracted; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param array $record |
|
129
|
|
|
* @param array $attributes |
|
130
|
|
|
* |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
protected static function fixedDeserializeMapping(array $record, array $attributes) |
|
134
|
|
|
{ |
|
135
|
|
|
foreach ($record as $index => &$value) { |
|
136
|
|
|
if (isset($attributes[$index]) && is_array($attributes[$index])) { |
|
137
|
|
|
$value = $attributes[$index]; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $record; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.