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
|
81 |
|
public function __construct(array $data = [], int $flags = NONE) |
36
|
|
|
{ |
37
|
81 |
|
$this->flags = static::getDefaultFlags() | $flags; |
38
|
81 |
|
$this->propertiesMap = $this->mapData($data); |
39
|
81 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Retrieve an instance of DTO |
43
|
|
|
* |
44
|
|
|
* @param array $data |
45
|
|
|
* @param int $flags |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
72 |
|
public static function make(array $data = [], int $flags = NONE): self |
49
|
|
|
{ |
50
|
72 |
|
return new static($data, $flags); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieve a clone of the DTO |
55
|
|
|
* |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
8 |
|
public function clone(): self |
59
|
|
|
{ |
60
|
8 |
|
return clone $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Retrieve the serialized DTO |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
1 |
|
public function serialize(): string |
69
|
|
|
{ |
70
|
1 |
|
return serialize([ |
71
|
1 |
|
$this->toArray(), |
72
|
1 |
|
$this->getFlags(), |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retrieve the unserialized DTO |
78
|
|
|
* |
79
|
|
|
* @param mixed $serialized |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
public function unserialize($serialized): void |
83
|
|
|
{ |
84
|
1 |
|
[$data, $flags] = unserialize($serialized); |
85
|
|
|
|
86
|
1 |
|
$this->__construct($data, $flags); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Determine how to clone the DTO |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
8 |
|
public function __clone() |
95
|
|
|
{ |
96
|
8 |
|
foreach ($this->propertiesMap as &$property) { |
97
|
8 |
|
$property = clone $property; |
98
|
|
|
} |
99
|
8 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retrieve the data to debug |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
1 |
|
public function __debugInfo(): array |
107
|
|
|
{ |
108
|
1 |
|
return $this->toArray(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|