Completed
Push — master ( 701c2d...98bf22 )
by Iqbal
02:19
created

AbstractReadModel::deserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::$mappings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::$mappings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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