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
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $mappings = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public static function deserialize(array $attributes) |
39
|
|
|
{ |
40
|
|
|
if (static::$mappings) { |
|
|
|
|
41
|
|
|
$attributes = static::map($attributes, array_flip(static::$mappings)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return static::deserializeData($attributes); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function serialize() |
51
|
|
|
{ |
52
|
|
|
$serialized = $this->serializeData(); |
53
|
|
|
|
54
|
|
|
if (static::$mappings) { |
|
|
|
|
55
|
|
|
$serialized = static::map($serialized, static::$mappings); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $serialized; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Mapping data base on mapping keys. |
63
|
|
|
* |
64
|
|
|
* @param array $attributes |
65
|
|
|
* @param array $mappingKeys |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected static function map(array $attributes, array $mappingKeys) |
70
|
|
|
{ |
71
|
|
|
return (array) ParameterMapper::map(new ParameterBag($attributes), $mappingKeys); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.