-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPropertyMetadata.php
More file actions
132 lines (111 loc) · 3.55 KB
/
PropertyMetadata.php
File metadata and controls
132 lines (111 loc) · 3.55 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Metadata;
use Closure;
use InvalidArgumentException;
use Patchlevel\Hydrator\Normalizer\Normalizer;
use ReflectionProperty;
use Symfony\Component\TypeInfo\Type;
use function str_starts_with;
/**
* @psalm-type serialized = array{
* className: class-string,
* property: string,
* fieldName: string,
* normalizer: Normalizer|null,
* isPersonalData: bool,
* personalDataFallback: mixed,
* extras: array<string, mixed>
* }
*/
final class PropertyMetadata
{
private const ENCRYPTED_PREFIX = '!';
public readonly string $propertyName;
/**
* @param (callable(string, mixed):mixed)|null $personalDataFallbackCallable
* @param array<string, mixed> $extras
*/
public function __construct(
public readonly ReflectionProperty $reflection,
public string $fieldName,
public Normalizer|null $normalizer = null,
public readonly bool $isPersonalData = false,
public readonly mixed $personalDataFallback = null,
public readonly mixed $personalDataFallbackCallable = null,
public array $extras = [],
public readonly Type|null $type = null,
) {
$this->propertyName = $reflection->getName();
if (str_starts_with($fieldName, self::ENCRYPTED_PREFIX)) {
throw new InvalidArgumentException('fieldName must not start with !');
}
}
public function reflection(): ReflectionProperty
{
return $this->reflection;
}
public function propertyName(): string
{
return $this->propertyName;
}
public function fieldName(): string
{
return $this->fieldName;
}
public function encryptedFieldName(): string
{
return self::ENCRYPTED_PREFIX . $this->fieldName;
}
public function normalizer(): Normalizer|null
{
return $this->normalizer;
}
public function setValue(object $object, mixed $value): void
{
$this->reflection->setValue($object, $value);
}
public function getValue(object $object): mixed
{
return $this->reflection->getValue($object);
}
public function isPersonalData(): bool
{
return $this->isPersonalData;
}
public function personalDataFallback(): mixed
{
return $this->personalDataFallback;
}
/** @return (Closure(string, mixed):mixed)|null */
public function personalDataFallbackCallback(): Closure|null
{
if ($this->personalDataFallbackCallable) {
return ($this->personalDataFallbackCallable)(...);
}
return null;
}
/** @return serialized */
public function __serialize(): array
{
return [
'className' => $this->reflection->getDeclaringClass()->getName(),
'property' => $this->propertyName,
'fieldName' => $this->fieldName,
'normalizer' => $this->normalizer,
'isPersonalData' => $this->isPersonalData,
'personalDataFallback' => $this->personalDataFallback,
'extras' => $this->extras,
];
}
/** @param serialized $data */
public function __unserialize(array $data): void
{
$this->reflection = new ReflectionProperty($data['className'], $data['property']);
$this->fieldName = $data['fieldName'];
$this->normalizer = $data['normalizer'];
$this->isPersonalData = $data['isPersonalData'];
$this->personalDataFallback = $data['personalDataFallback'];
$this->extras = $data['extras'];
}
}