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
|
|
|
protected $payload = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set payload. |
31
|
|
|
* |
32
|
|
|
* @param array<string, mixed> $parameters |
33
|
|
|
*/ |
34
|
|
|
final protected 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
|
|
|
final protected 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
|
|
|
$payload = []; |
103
|
|
|
|
104
|
|
|
foreach (\array_keys($this->payload) as $parameter) { |
105
|
|
|
$payload[$parameter] = $this->get($parameter); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $payload; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get parameter transformer getter method name. |
113
|
|
|
* |
114
|
|
|
* @param string $parameter |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function getParameterTransformerMethod(string $parameter): string |
119
|
|
|
{ |
120
|
|
|
return 'output' . \ucfirst($parameter); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Magic getters call. |
125
|
|
|
* |
126
|
|
|
* @param string $methodName |
127
|
|
|
* @param mixed[] $arguments |
128
|
|
|
* |
129
|
|
|
* @throws InvalidMethodCallException |
130
|
|
|
* @throws InvalidParameterException |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
final public function __call(string $methodName, array $arguments) |
135
|
|
|
{ |
136
|
|
|
if (!\preg_match('/^(has|get)([A-Z][a-zA-Z0-9-_]*)$/', $methodName, $matches)) { |
137
|
|
|
throw new InvalidMethodCallException(\sprintf('Method %s::%s does not exist', static::class, $methodName)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (\count($arguments) !== 0) { |
141
|
|
|
throw new InvalidMethodCallException(\sprintf( |
142
|
|
|
'%s::%s method should be called with no parameters', |
143
|
|
|
static::class, |
144
|
|
|
$methodName |
145
|
|
|
)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$method = $matches[1]; |
149
|
|
|
$parameter = $matches[2]; |
150
|
|
|
|
151
|
|
|
if ($this->has($parameter)) { |
152
|
|
|
return $method === 'has' ? true : $this->get($parameter); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $method === 'has' ? $this->has(\lcfirst($parameter)) : $this->get(\lcfirst($parameter)); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.