1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* dto (https://github.com/phpgears/dto). |
5
|
|
|
* General purpose immutable Data Transfer Objects for PHP. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/dto |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\DTO; |
15
|
|
|
|
16
|
|
|
use Gears\Immutability\ImmutabilityBehaviour; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Abstract immutable Data Transfer Object. |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractDTO implements DTO |
22
|
|
|
{ |
23
|
|
|
use ImmutabilityBehaviour, PayloadBehaviour { |
24
|
|
|
PayloadBehaviour::__call insteadof ImmutabilityBehaviour; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* AbstractDTO constructor. |
29
|
|
|
* |
30
|
|
|
* @param array<string, mixed> $parameters |
31
|
|
|
*/ |
32
|
|
|
final protected function __construct(array $parameters) |
33
|
|
|
{ |
34
|
|
|
$this->assertImmutable(); |
35
|
|
|
|
36
|
|
|
$this->setPayload($parameters); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return mixed[] |
41
|
|
|
*/ |
42
|
|
|
final public function __sleep(): array |
43
|
|
|
{ |
44
|
|
|
return ['payload']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
final public function __wakeup(): void |
48
|
|
|
{ |
49
|
|
|
$this->assertImmutable(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array<string, mixed> |
54
|
|
|
*/ |
55
|
|
|
final public function __serialize(): array |
56
|
|
|
{ |
57
|
|
|
return ['payload' => $this->payload]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array<string, mixed> $data |
62
|
|
|
* |
63
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
64
|
|
|
*/ |
65
|
|
|
final public function __unserialize(array $data): void |
66
|
|
|
{ |
67
|
|
|
$this->assertImmutable(); |
68
|
|
|
|
69
|
|
|
$this->setPayload($data['payload']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
* |
75
|
|
|
* @return string[] |
76
|
|
|
*/ |
77
|
|
|
final protected function getAllowedInterfaces(): array |
78
|
|
|
{ |
79
|
|
|
return [DTO::class, \Serializable::class]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|