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