1 | <?php |
||
13 | abstract class DataTransferObject implements DtoContract |
||
14 | { |
||
15 | /** @var array */ |
||
16 | protected $exceptKeys = []; |
||
17 | |||
18 | /** @var array */ |
||
19 | protected $onlyKeys = []; |
||
20 | |||
21 | /** @var Property[] | array */ |
||
22 | protected $properties = []; |
||
23 | |||
24 | /** @var bool */ |
||
25 | protected $immutable; |
||
26 | |||
27 | public static function mutable(array $parameters): DtoContract |
||
31 | |||
32 | public static function immutable(array $parameters): DtoContract |
||
36 | |||
37 | public function __construct(array $parameters, bool $immutable = true) |
||
42 | |||
43 | /** |
||
44 | * Boot the dto and process all parameters. |
||
45 | * @param array $parameters |
||
46 | * @throws \ReflectionException | DataTransferObjectError |
||
47 | */ |
||
48 | protected function boot(array $parameters): void |
||
77 | |||
78 | /** |
||
79 | * Get all public properties from the current object through reflection. |
||
80 | * @return Property[] |
||
81 | * @throws \ReflectionException |
||
82 | */ |
||
83 | protected function getPublicProperties(): array |
||
93 | |||
94 | /** |
||
95 | * Check if property passes the basic conditions. |
||
96 | * @param Property $property |
||
97 | * @param array $parameters |
||
98 | */ |
||
99 | protected function validateProperty($property, array $parameters): void |
||
100 | { |
||
101 | if (! array_key_exists($property->getName(), $parameters) |
||
102 | && is_null($property->getDefault()) |
||
103 | && ! $property->isNullable() |
||
104 | ) { |
||
105 | throw DataTransferObjectError::uninitialized($property); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Set the value if it's present in the array. |
||
111 | * @param Property $property |
||
112 | * @param array $parameters |
||
113 | */ |
||
114 | protected function setPropertyValue($property, array $parameters): void |
||
120 | |||
121 | /** |
||
122 | * Set the value if it's present in the array. |
||
123 | * @param Property $property |
||
124 | * @param array $parameters |
||
|
|||
125 | */ |
||
126 | protected function setPropertyDefaultValue($property): void |
||
130 | |||
131 | /** |
||
132 | * Allows to mutate the property before it gets processed. |
||
133 | * @param Property $property |
||
134 | * @param array $parameters |
||
135 | * @return Property |
||
136 | */ |
||
137 | protected function mutateProperty($property) |
||
141 | |||
142 | /** |
||
143 | * Check if there are additional parameters left. |
||
144 | * Throw error if there are. |
||
145 | * Additional properties are not allowed in a dto. |
||
146 | * @throws DataTransferObjectError |
||
147 | */ |
||
148 | protected function processRemainingProperties(array $parameters) |
||
154 | |||
155 | /** |
||
156 | * Immutable behavior |
||
157 | * Throw error if a user tries to set a property. |
||
158 | * @param $name |
||
159 | * @param $value |
||
160 | * @Throws DataTransferObjectError |
||
161 | */ |
||
162 | public function __set($name, $value) |
||
169 | |||
170 | /** |
||
171 | * Proxy through to the properties array. |
||
172 | * @param $name |
||
173 | * @return mixed |
||
174 | */ |
||
175 | public function __get($name) |
||
179 | |||
180 | public function all(): array |
||
190 | |||
191 | public function only(string ...$keys): DtoContract |
||
197 | |||
198 | public function except(string ...$keys): DtoContract |
||
204 | |||
205 | public function toArray(): array |
||
217 | |||
218 | protected function parseArray(array $array): array |
||
239 | } |
||
240 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.