-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClassMetadata.php
More file actions
154 lines (124 loc) · 4.17 KB
/
ClassMetadata.php
File metadata and controls
154 lines (124 loc) · 4.17 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Metadata;
use ReflectionClass;
use ReflectionParameter;
/**
* @phpstan-type serialized array{
* className: class-string,
* properties: array<string, PropertyMetadata>,
* lazy: bool|null,
* extras: array<string, mixed>,
* }
* @template T of object = object
*/
final class ClassMetadata
{
/** @var class-string<T> */
public readonly string $className;
/** @var array<string, PropertyMetadata> */
public array $properties;
/** @var list<PropertyMetadata> */
public array $propertiesWithNormalizer;
/** @var list<PropertyMetadata> */
public array $propertiesWithoutNormalizer;
/** @var array<string, ReflectionParameter>|null */
private array|null $promotedConstructorDefaults = null;
/**
* @param ReflectionClass<T> $reflection
* @param list<PropertyMetadata> $properties
* @param array<string, mixed> $extras
*/
public function __construct(
public readonly ReflectionClass $reflection,
array $properties = [],
public bool|null $lazy = null,
public array $extras = [],
) {
$this->className = $reflection->getName();
$this->updateProperties($properties);
}
/** @param list<PropertyMetadata> $properties */
public function updateProperties(array $properties): void
{
$map = [];
$withNormalizer = [];
$withoutNormalizer = [];
foreach ($properties as $property) {
$map[$property->propertyName] = $property;
if ($property->normalizer !== null) {
$withNormalizer[] = $property;
} else {
$withoutNormalizer[] = $property;
}
}
$this->properties = $map;
$this->propertiesWithNormalizer = $withNormalizer;
$this->propertiesWithoutNormalizer = $withoutNormalizer;
}
public function propertyForField(string $name): PropertyMetadata
{
foreach ($this->properties as $property) {
if ($property->fieldName === $name) {
return $property;
}
}
throw PropertyMetadataNotFound::withName($name);
}
/** @return T */
public function newInstance(): object
{
return $this->reflection->newInstanceWithoutConstructor();
}
/** @return array<string, ReflectionParameter> */
public function promotedConstructorDefaults(): array
{
if ($this->promotedConstructorDefaults !== null) {
return $this->promotedConstructorDefaults;
}
$constructor = $this->reflection->getConstructor();
if (!$constructor) {
return $this->promotedConstructorDefaults = [];
}
$result = [];
foreach ($constructor->getParameters() as $parameter) {
if (!$parameter->isPromoted() || !$parameter->isDefaultValueAvailable()) {
continue;
}
$result[$parameter->getName()] = $parameter;
}
return $this->promotedConstructorDefaults = $result;
}
/** @return serialized */
public function __serialize(): array
{
return [
'className' => $this->className,
'properties' => array_values($this->properties),
'lazy' => $this->lazy,
'extras' => $this->extras,
];
}
/** @param serialized $data */
public function __unserialize(array $data): void
{
$this->reflection = new ReflectionClass($data['className']);
$map = [];
$withNormalizer = [];
$withoutNormalizer = [];
foreach ($data['properties'] as $property) {
$map[$property->propertyName] = $property;
if ($property->normalizer !== null) {
$withNormalizer[] = $property;
} else {
$withoutNormalizer[] = $property;
}
}
$this->className = $data['className'];
$this->properties = $map;
$this->propertiesWithNormalizer = $withNormalizer;
$this->propertiesWithoutNormalizer = $withoutNormalizer;
$this->lazy = $data['lazy'];
$this->extras = $data['extras'];
}
}