-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPropertyMetadata.php
More file actions
64 lines (55 loc) · 1.72 KB
/
Copy pathPropertyMetadata.php
File metadata and controls
64 lines (55 loc) · 1.72 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Metadata;
use Patchlevel\Hydrator\Normalizer\Normalizer;
use ReflectionProperty;
/**
* @phpstan-type serialized = array{
* className: class-string,
* propertyName: string,
* fieldName: string,
* normalizer: Normalizer|null,
* extras: array<string, mixed>,
* }
*/
final class PropertyMetadata
{
public readonly string $propertyName;
/** @param array<string, mixed> $extras */
public function __construct(
public readonly ReflectionProperty $reflection,
public string $fieldName,
public Normalizer|null $normalizer = null,
public array $extras = [],
) {
$this->propertyName = $reflection->getName();
}
public function setValue(object $object, mixed $value): void
{
$this->reflection->setValue($object, $value);
}
public function getValue(object $object): mixed
{
return $this->reflection->getValue($object);
}
/** @return serialized */
public function __serialize(): array
{
return [
'className' => $this->reflection->getDeclaringClass()->getName(),
'propertyName' => $this->propertyName,
'fieldName' => $this->fieldName,
'normalizer' => $this->normalizer,
'extras' => $this->extras,
];
}
/** @param serialized $data */
public function __unserialize(array $data): void
{
$this->reflection = new ReflectionProperty($data['className'], $data['propertyName']);
$this->propertyName = $data['propertyName'];
$this->fieldName = $data['fieldName'];
$this->normalizer = $data['normalizer'];
$this->extras = $data['extras'];
}
}