|
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\InvalidMethodCallException; |
|
17
|
|
|
use Gears\DTO\Exception\InvalidParameterException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Payload behaviour. |
|
21
|
|
|
*/ |
|
22
|
|
|
trait PayloadBehaviour |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var mixed[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $payload = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set payload. |
|
31
|
|
|
* |
|
32
|
|
|
* @param array<string, mixed> $parameters |
|
33
|
|
|
*/ |
|
34
|
|
|
private function setPayload(array $parameters): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->payload = []; |
|
37
|
|
|
|
|
38
|
|
|
foreach ($parameters as $parameter => $value) { |
|
39
|
|
|
$this->setPayloadParameter($parameter, $value); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Set payload attribute. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $parameter |
|
47
|
|
|
* @param mixed $value |
|
48
|
|
|
*/ |
|
49
|
|
|
private function setPayloadParameter(string $parameter, $value): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->payload[$parameter] = $value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Check parameter existence. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $parameter |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
final public function has(string $parameter): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return \array_key_exists($parameter, $this->payload); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get parameter. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $parameter |
|
70
|
|
|
* |
|
71
|
|
|
* @throws InvalidParameterException |
|
72
|
|
|
* |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
*/ |
|
75
|
|
|
final public function get(string $parameter) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!\array_key_exists($parameter, $this->payload)) { |
|
78
|
|
|
throw new InvalidParameterException(\sprintf( |
|
79
|
|
|
'Payload parameter "%s" on "%s" does not exist', |
|
80
|
|
|
$parameter, |
|
81
|
|
|
static::class |
|
82
|
|
|
)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$value = $this->payload[$parameter]; |
|
86
|
|
|
|
|
87
|
|
|
$transformer = $this->getParameterTransformerMethod($parameter); |
|
88
|
|
|
if (\method_exists($this, $transformer)) { |
|
89
|
|
|
$value = $this->$transformer($value); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $value; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Export payload. |
|
97
|
|
|
* |
|
98
|
|
|
* @return array<string, mixed> |
|
99
|
|
|
*/ |
|
100
|
|
|
final public function getPayload(): array |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->payload; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get parameter transformer getter method name. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $parameter |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getParameterTransformerMethod(string $parameter): string |
|
113
|
|
|
{ |
|
114
|
|
|
return 'output' . \ucfirst($parameter); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Magic getters call. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $methodName |
|
121
|
|
|
* @param mixed[] $arguments |
|
122
|
|
|
* |
|
123
|
|
|
* @throws InvalidMethodCallException |
|
124
|
|
|
* @throws InvalidParameterException |
|
125
|
|
|
* |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
final public function __call(string $methodName, array $arguments) |
|
129
|
|
|
{ |
|
130
|
|
|
if (\preg_match('/^(has|get)([A-Z][a-zA-Z0-9-_]*)$/', $methodName, $matches) !== 1) { |
|
131
|
|
|
throw new InvalidMethodCallException( |
|
132
|
|
|
\sprintf('Method "%s::%s" does not exist', static::class, $methodName) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (\count($arguments) !== 0) { |
|
137
|
|
|
throw new InvalidMethodCallException(\sprintf( |
|
138
|
|
|
'Method "%s::%s" should be called with no parameters', |
|
139
|
|
|
static::class, |
|
140
|
|
|
$methodName |
|
141
|
|
|
)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
[$method, $parameter] = \array_slice($matches, 1); |
|
145
|
|
|
|
|
146
|
|
|
if ($this->has($parameter)) { |
|
147
|
|
|
return $method === 'has' ? true : $this->get($parameter); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $method === 'has' ? $this->has(\lcfirst($parameter)) : $this->get(\lcfirst($parameter)); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|