-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClassMetadata.php
More file actions
114 lines (92 loc) · 2.94 KB
/
Copy pathClassMetadata.php
File metadata and controls
114 lines (92 loc) · 2.94 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
<?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 readonly array $properties;
/** @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();
$map = [];
foreach ($properties as $property) {
$map[$property->propertyName] = $property;
}
$this->properties = $map;
}
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' => $this->properties,
'lazy' => $this->lazy,
'extras' => $this->extras,
];
}
/** @param serialized $data */
public function __unserialize(array $data): void
{
$this->reflection = new ReflectionClass($data['className']);
$this->properties = $data['properties'];
$this->lazy = $data['lazy'];
$this->extras = $data['extras'];
}
}