|
10 | 10 | final class Hydrator implements HydratorInterface |
11 | 11 | { |
12 | 12 |
|
| 13 | + private function invoke(callable $callback, object $entity, ...$args) |
| 14 | + { |
| 15 | + if ($callback instanceof \Closure) { |
| 16 | + $callback = $callback->bindTo($entity, $entity); |
| 17 | + } |
| 18 | + |
| 19 | + return $callback(...$args); |
| 20 | + } |
| 21 | + |
13 | 22 | public function hydrate(HydratorConfigInterface $config, object $entity, array $data): void |
14 | 23 | { |
15 | 24 | foreach ($config->getHydratorProperties() as $propertyName => $set) { |
16 | | - if ($set instanceof \Closure) { |
17 | | - $set = $set->bindTo($entity, $entity); |
18 | | - } |
19 | | - |
20 | | - $set($data[$propertyName] ?? null, $propertyName); |
| 25 | + $this->invoke($set, $entity, $data[$propertyName] ?? null, $propertyName); |
21 | 26 | } |
22 | 27 | } |
23 | 28 |
|
24 | 29 | public function extract(ExtractorConfigInterface $config, object $entity): array |
25 | 30 | { |
26 | 31 | $data = []; |
27 | 32 |
|
28 | | - /** |
29 | | - * @var string $propertyName |
30 | | - * @var \Closure $get |
31 | | - */ |
32 | 33 | foreach ($config->getExtractorProperties() as $propertyName => $get) { |
33 | | - if (!$get instanceof \Closure) { |
34 | | - throw new \RuntimeException('Must be closure'); |
35 | | - } |
36 | | - |
37 | | - $get = $get->bindTo($entity, $entity); |
38 | | - $data[$propertyName] = $get($propertyName); |
| 34 | + $data[$propertyName] = $this->invoke($get, $entity, $propertyName); |
39 | 35 | } |
40 | 36 |
|
41 | 37 | return $data; |
|
0 commit comments