1 | <?php |
||
21 | abstract class DataTransferObject implements DtoContract |
||
22 | { |
||
23 | /** @var array */ |
||
24 | protected $exceptKeys = []; |
||
25 | |||
26 | /** @var array */ |
||
27 | protected $onlyKeys = []; |
||
28 | |||
29 | /** @var Property[] | array */ |
||
30 | protected $properties = []; |
||
31 | |||
32 | /** @var bool */ |
||
33 | protected $immutable = false; |
||
34 | |||
35 | public function __construct(array $parameters) |
||
39 | |||
40 | /** |
||
41 | * Boot the dto and process all parameters. |
||
42 | * @param array $parameters |
||
43 | * @throws \ReflectionException | DataTransferObjectError |
||
44 | */ |
||
45 | protected function boot(array $parameters): void |
||
73 | |||
74 | protected function resolveImmutable() |
||
79 | |||
80 | /** |
||
81 | * Get all public properties from the current object through reflection. |
||
82 | * @return Property[] |
||
83 | * @throws \ReflectionException |
||
84 | */ |
||
85 | protected function getPublicProperties(): array |
||
96 | |||
97 | /** |
||
98 | * Check if property passes the basic conditions. |
||
99 | * @param PropertyContract $property |
||
100 | * @param array $parameters |
||
101 | */ |
||
102 | protected function validateProperty(PropertyContract $property, array $parameters): void |
||
111 | |||
112 | /** |
||
113 | * Set the value if it's present in the array. |
||
114 | * @param PropertyContract $property |
||
115 | * @param array $parameters |
||
116 | */ |
||
117 | protected function setPropertyValue(PropertyContract $property, array $parameters): void |
||
123 | |||
124 | /** |
||
125 | * Set the value if it's present in the array. |
||
126 | * @param PropertyContract $property |
||
127 | */ |
||
128 | protected function setPropertyDefaultValue(PropertyContract $property): void |
||
132 | |||
133 | /** |
||
134 | * Allows to mutate the property before it gets processed. |
||
135 | * @param PropertyContract $property |
||
136 | * @return PropertyContract |
||
137 | */ |
||
138 | protected function mutateProperty(PropertyContract $property): PropertyContract |
||
142 | |||
143 | /** |
||
144 | * Check if there are additional parameters left. |
||
145 | * Throw error if there are. |
||
146 | * Additional properties are not allowed in a dto. |
||
147 | * @param array $parameters |
||
148 | * @throws UnknownPropertiesDtoException |
||
149 | */ |
||
150 | protected function processRemainingProperties(array $parameters) |
||
156 | |||
157 | /** |
||
158 | * Immutable behavior |
||
159 | * Throw error if a user tries to set a property. |
||
160 | * @param $name |
||
161 | * @param $value |
||
162 | * @throws ImmutableDtoException|ImmutablePropertyDtoException|PropertyNotFoundDtoException |
||
163 | */ |
||
164 | public function __set($name, $value) |
||
178 | |||
179 | /** |
||
180 | * Proxy through to the properties array. |
||
181 | * @param $name |
||
182 | * @return mixed |
||
183 | */ |
||
184 | public function __get($name) |
||
188 | |||
189 | |||
190 | /** |
||
191 | * @return static |
||
192 | */ |
||
193 | public function mutable(): DtoContract |
||
198 | |||
199 | /** |
||
200 | * @return static |
||
201 | */ |
||
202 | public function immutable(): DtoContract |
||
207 | |||
208 | public function all(): array |
||
218 | |||
219 | public function only(string ...$keys): DtoContract |
||
225 | |||
226 | public function except(string ...$keys): DtoContract |
||
236 | |||
237 | public function toArray(): array |
||
253 | |||
254 | protected function parseArray(array $array): array |
||
275 | } |
||
276 |