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\DTO\Exception\InvalidScalarParameterException; |
17
|
|
|
use Gears\Immutability\ImmutabilityBehaviour; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Abstract immutable and only scalar values Data Transfer Object. |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractScalarDTO implements DTO, \Serializable |
23
|
|
|
{ |
24
|
|
|
use ImmutabilityBehaviour, ScalarPayloadBehaviour { |
25
|
|
|
ScalarPayloadBehaviour::__call insteadof ImmutabilityBehaviour; |
26
|
|
|
ScalarPayloadBehaviour::checkParameterType as private scalarCheckParameterType; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AbstractSerializableDTO constructor. |
31
|
|
|
* |
32
|
|
|
* @param array<string, mixed> $parameters |
33
|
|
|
*/ |
34
|
|
|
final protected function __construct(array $parameters) |
35
|
|
|
{ |
36
|
|
|
$this->assertImmutable(); |
37
|
|
|
|
38
|
|
|
$this->setPayload($parameters); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array<string, mixed> |
43
|
|
|
*/ |
44
|
|
|
final public function __serialize(): array |
45
|
|
|
{ |
46
|
|
|
return ['payload' => $this->payload]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array<string, mixed> $data |
51
|
|
|
* |
52
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
53
|
|
|
*/ |
54
|
|
|
final public function __unserialize(array $data): void |
55
|
|
|
{ |
56
|
|
|
$this->assertImmutable(); |
57
|
|
|
|
58
|
|
|
$this->setPayload($data['payload']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
final public function serialize(): string |
65
|
|
|
{ |
66
|
|
|
return \serialize($this->payload); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* |
72
|
|
|
* @param mixed $serialized |
73
|
|
|
*/ |
74
|
|
|
final public function unserialize($serialized): void |
75
|
|
|
{ |
76
|
|
|
$this->assertImmutable(); |
77
|
|
|
|
78
|
|
|
$this->payload = \unserialize($serialized, ['allowed_classes' => false]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
* |
84
|
|
|
* @param mixed $value |
85
|
|
|
*/ |
86
|
|
|
final protected function checkParameterType($value): void |
87
|
|
|
{ |
88
|
|
|
if ($value instanceof DTO) { |
89
|
|
|
$value = $value->getPayload(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (\is_array($value)) { |
93
|
|
|
foreach ($value as $val) { |
94
|
|
|
$this->checkParameterType($val); |
95
|
|
|
} |
96
|
|
|
} elseif ($value !== null && !\is_scalar($value)) { |
97
|
|
|
throw new InvalidScalarParameterException(\sprintf( |
98
|
|
|
'Class "%s" can only accept scalar payload parameters, "%s" given', |
99
|
|
|
self::class, |
100
|
|
|
\is_object($value) ? \get_class($value) : \gettype($value) |
101
|
|
|
)); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
* |
108
|
|
|
* @return string[] |
109
|
|
|
*/ |
110
|
|
|
final protected function getAllowedInterfaces(): array |
111
|
|
|
{ |
112
|
|
|
return [DTO::class, \Serializable::class]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|