1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Entity\Hydrator; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Entity\Hydrator\Exception\InvalidTypeException; |
6
|
|
|
use Bdf\Prime\Entity\ImportableInterface; |
7
|
|
|
use Closure; |
8
|
|
|
use stdClass; |
9
|
|
|
|
10
|
|
|
use TypeError; |
11
|
|
|
use function get_class; |
12
|
|
|
use function is_array; |
13
|
|
|
use function method_exists; |
14
|
|
|
use function property_exists; |
15
|
|
|
use function ucfirst; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base hydrator implementation. |
19
|
|
|
* Works like {@link ArrayHydrator} |
20
|
|
|
*/ |
21
|
|
|
class ArrayHydrator implements HydratorInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Array prefix for protected properties |
25
|
|
|
*/ |
26
|
|
|
const PROTECTED_PREFIX = "\0*\0"; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array<class-string, callable(object, array)> |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
private $hydratorsCache = []; |
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
519 |
|
public function hydrate($object, array $data) |
37
|
|
|
{ |
38
|
519 |
|
return $this->getHydratorForClass(get_class($object))($object, $data); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
13 |
|
public function extract($object, array $attributes = []) |
45
|
|
|
{ |
46
|
13 |
|
$values = []; |
47
|
13 |
|
$attributes = array_flip($attributes); |
48
|
|
|
|
49
|
13 |
|
$privatePrefix = "\0" . get_class($object) . "\0"; |
50
|
13 |
|
$privatePrefixLen = strlen($privatePrefix); |
51
|
|
|
|
52
|
13 |
|
foreach ((array) $object as $name => $property) { |
|
|
|
|
53
|
13 |
|
if (strpos($name, self::PROTECTED_PREFIX) === 0) { |
54
|
2 |
|
$name = substr($name, 3); |
55
|
13 |
|
} elseif (strpos($name, $privatePrefix) === 0) { |
|
|
|
|
56
|
2 |
|
$name = substr($name, $privatePrefixLen); |
57
|
|
|
} |
58
|
|
|
|
59
|
13 |
|
if (!empty($attributes) && !isset($attributes[$name])) { |
60
|
3 |
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
13 |
|
if ($property instanceof ImportableInterface) { |
64
|
6 |
|
$values[$name] = $property->export(); |
65
|
|
|
} else { |
66
|
13 |
|
$values[$name] = $property; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
13 |
|
return $values; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create or retrieve the hydrator callback for the given entity class |
75
|
|
|
* |
76
|
|
|
* @param class-string $entityClass The entity class name |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return callable(object, array) |
79
|
|
|
*/ |
80
|
519 |
|
private function getHydratorForClass(string $entityClass): callable |
|
|
|
|
81
|
|
|
{ |
82
|
519 |
|
if (isset($this->hydratorsCache[$entityClass])) { |
83
|
490 |
|
return $this->hydratorsCache[$entityClass]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$hydrator = static function ($object, array $data) { |
87
|
519 |
|
foreach ($data as $property => $value) { |
88
|
|
|
try { |
89
|
519 |
|
if (isset($object->$property) && $object->$property instanceof ImportableInterface && is_array($value)) { |
90
|
41 |
|
$object->$property->import($value); |
91
|
518 |
|
} elseif (method_exists($object, 'set' . ucfirst($property))) { |
|
|
|
|
92
|
94 |
|
$object->{'set' . ucfirst($property)}($value); |
93
|
511 |
|
} elseif (property_exists($object, $property)) { |
|
|
|
|
94
|
519 |
|
$object->$property = $value; |
95
|
|
|
} |
96
|
|
|
} catch (TypeError $e) { |
97
|
519 |
|
throw new InvalidTypeException($e); |
98
|
|
|
} |
99
|
|
|
} |
100
|
519 |
|
}; |
101
|
|
|
|
102
|
86 |
|
if ($entityClass !== stdClass::class) { |
103
|
|
|
// Bind to access private properties |
104
|
|
|
// Note: ignore stdClass because all its fields are public |
105
|
85 |
|
$hydrator = Closure::bind($hydrator, null, $entityClass); |
106
|
|
|
} |
107
|
|
|
|
108
|
86 |
|
return $this->hydratorsCache[$entityClass] = $hydrator; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|