Passed
Push — master ( 9d7ef7...af08db )
by Andrea Marco
01:42 queued 11s
created

Dto::mapData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\Dto;
4
5
use ArrayAccess;
6
use Cerbero\Dto\Traits\HasFlags;
7
use Cerbero\Dto\Traits\HasProperties;
8
use Cerbero\Dto\Traits\HasValues;
9
use Cerbero\Dto\Traits\ManipulatesData;
10
use Cerbero\Dto\Traits\TurnsIntoArray;
11
use Cerbero\Dto\Traits\TurnsIntoString;
12
use IteratorAggregate;
13
use JsonSerializable;
14
use Serializable;
15
16
/**
17
 * The data transfer object.
18
 *
19
 */
20
abstract class Dto implements IteratorAggregate, ArrayAccess, Serializable, JsonSerializable
21
{
22
    use HasProperties;
23
    use HasValues;
24
    use HasFlags;
25
    use ManipulatesData;
26
    use TurnsIntoArray;
27
    use TurnsIntoString;
28
29
    /**
30
     * Instantiate the class.
31
     *
32
     * @param array $data
33
     * @param int $flags
34
     */
35 240
    public function __construct(array $data = [], int $flags = NONE)
36
    {
37 240
        $this->flags = $this->mergeFlags(static::getDefaultFlags(), $flags);
38 240
        $this->propertiesMap = $this->mapData($data);
39 240
    }
40
41
    /**
42
     * Retrieve an instance of DTO
43
     *
44
     * @param array $data
45
     * @param int $flags
46
     * @return self
47
     */
48 210
    public static function make(array $data = [], int $flags = NONE): self
49
    {
50 210
        return new static($data, $flags);
51
    }
52
53
    /**
54
     * Retrieve a clone of the DTO
55
     *
56
     * @return self
57
     */
58 24
    public function clone(): self
59
    {
60 24
        return clone $this;
61
    }
62
63
    /**
64
     * Retrieve the serialized DTO
65
     *
66
     * @return string
67
     */
68 3
    public function serialize(): string
69
    {
70 3
        return serialize([
71 3
            $this->toArray(),
72 3
            $this->getFlags(),
73
        ]);
74
    }
75
76
    /**
77
     * Retrieve the unserialized DTO
78
     *
79
     * @param mixed $serialized
80
     * @return string
81
     */
82 3
    public function unserialize($serialized): void
83
    {
84 3
        [$data, $flags] = unserialize($serialized);
85
86 3
        $this->__construct($data, $flags);
87 3
    }
88
89
    /**
90
     * Determine how to clone the DTO
91
     *
92
     * @return void
93
     */
94 24
    public function __clone()
95
    {
96 24
        foreach ($this->propertiesMap as &$property) {
97 24
            $property = clone $property;
98
        }
99 24
    }
100
}
101