Skip to content

Commit a204941

Browse files
l-xusox
authored andcommitted
Allow all callables for properties
1 parent 95818c6 commit a204941

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/Scn/Hydrator/Hydrator.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,28 @@
1010
final class Hydrator implements HydratorInterface
1111
{
1212

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+
1322
public function hydrate(HydratorConfigInterface $config, object $entity, array $data): void
1423
{
1524
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);
2126
}
2227
}
2328

2429
public function extract(ExtractorConfigInterface $config, object $entity): array
2530
{
2631
$data = [];
2732

28-
/**
29-
* @var string $propertyName
30-
* @var \Closure $get
31-
*/
3233
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);
3935
}
4036

4137
return $data;

0 commit comments

Comments
 (0)