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 mappings() |
34
|
|
|
{ |
35
|
|
|
return array(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public static function deserialize(array $attributes) |
42
|
|
|
{ |
43
|
|
|
if ($mappings = static::mappings()) { |
|
|
|
|
44
|
|
|
$attributes = static::mapBaseOnKeys($attributes, array_flip(static::cleanMappingKeys($attributes))); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return static::deserializeData($attributes); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function serialize() |
54
|
|
|
{ |
55
|
|
|
$serialized = $this->serializeData(); |
56
|
|
|
|
57
|
|
|
if ($mappings = static::mappings()) { |
|
|
|
|
58
|
|
|
$serialized = static::mapBaseOnKeys($serialized, static::cleanMappingKeys($serialized)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $serialized; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Mapping data base on mapping keys. |
66
|
|
|
* |
67
|
|
|
* @param array $attributes |
68
|
|
|
* @param array $mappingKeys |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected static function mapBaseOnKeys(array $attributes, array $mappingKeys) |
73
|
|
|
{ |
74
|
|
|
return (array) ParameterMapper::map(new ParameterBag($attributes), $mappingKeys); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $serialized |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
protected static function cleanMappingKeys(array $serialized) |
83
|
|
|
{ |
84
|
|
|
return static::mergeMappingKeys(array_keys($serialized), static::mappings()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $attributesKeys |
89
|
|
|
* @param array $mappingKeys |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
protected static function mergeMappingKeys(array $attributesKeys, array $mappingKeys) |
94
|
|
|
{ |
95
|
|
|
$diffs = array_diff($attributesKeys, static::extraKeys($mappingKeys)); |
96
|
|
|
|
97
|
|
|
return array_merge(array_combine($diffs, $diffs), $mappingKeys); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $attributesKeys |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
protected static function extraKeys(array $attributesKeys) |
106
|
|
|
{ |
107
|
|
|
$extracted = array(); |
108
|
|
|
foreach (array_keys($attributesKeys) as $key) { |
109
|
|
|
$parts = explode('.', $key); |
110
|
|
|
if (!in_array($parts[0], $extracted)) { |
111
|
|
|
$extracted[] = $parts[0]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $extracted; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.