1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace Laminas\Hydrator;
use ReflectionClass;
use ReflectionProperty;
use function array_fill_keys;
use function array_map;
use function get_class;
use function get_object_vars;
class ObjectPropertyHydrator extends AbstractHydrator
{
/** @var (null|array)[] indexed by class name and then property name */
private static $skippedPropertiesCache = [];
/**
* Extracts the accessible non-static properties of the given $object.
*
* {@inheritDoc}
*/
public function extract(object $object): array
{
$data = get_object_vars($object);
$filter = $this->getFilter();
foreach ($data as $name => $value) {
// Filter keys, removing any we don't want
if (! $filter->filter($name)) {
unset($data[$name]);
continue;
}
// Replace name if extracted differ
$extracted = $this->extractName($name, $object);
if ($extracted !== $name) {
unset($data[$name]);
$name = $extracted;
}
$data[$name] = $this->extractValue($name, $value, $object);
}
return $data;
}
/**
* Hydrate an object by populating public properties
*
* Hydrates an object by setting public properties of the object.
*
* {@inheritDoc}
*/
public function hydrate(array $data, object $object)
{
$properties = &self::$skippedPropertiesCache[get_class($object)] ?? null;
if (null === $properties) {
$reflection = new ReflectionClass($object);
$properties = array_fill_keys(
array_map(
function (ReflectionProperty $property) {
return $property->getName();
},
$reflection->getProperties(
ReflectionProperty::IS_PRIVATE
+ ReflectionProperty::IS_PROTECTED
+ ReflectionProperty::IS_STATIC
)
),
true
);
}
foreach ($data as $name => $value) {
$property = $this->hydrateName($name, $data);
if (isset($properties[$property])) {
continue;
}
$object->$property = $this->hydrateValue($property, $value, $data);
}
return $object;
}
}