-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathObjectMapNormalizer.php
More file actions
136 lines (110 loc) · 3.57 KB
/
ObjectMapNormalizer.php
File metadata and controls
136 lines (110 loc) · 3.57 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Normalizer;
use Attribute;
use Patchlevel\Hydrator\Hydrator;
use function array_flip;
use function array_key_exists;
use function array_keys;
use function implode;
use function is_array;
use function is_object;
use function is_string;
use function sprintf;
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
final class ObjectMapNormalizer implements Normalizer, HydratorAwareNormalizer
{
private Hydrator|null $hydrator = null;
/** @var array<string, class-string> */
private array $typeToClassMap;
/** @param array<class-string, string> $classToTypeMap */
public function __construct(
private readonly array $classToTypeMap,
private readonly string $typeFieldName = '_type',
) {
$this->typeToClassMap = array_flip($classToTypeMap);
}
public function setHydrator(Hydrator $hydrator): void
{
$this->hydrator = $hydrator;
}
/**
* @param array<string, mixed> $context
*
* @throws InvalidArgument
*/
public function normalize(mixed $value, array $context): mixed
{
if ($value === null) {
return null;
}
if (!$this->hydrator) {
throw new MissingHydrator();
}
if (!is_object($value)) {
throw InvalidArgument::withWrongType(
sprintf('%s|null', implode('|', array_keys($this->classToTypeMap))),
$value,
);
}
if (!array_key_exists($value::class, $this->classToTypeMap)) {
throw InvalidArgument::withWrongType(
sprintf('%s|null', implode('|', array_keys($this->classToTypeMap))),
$value,
);
}
$data = $this->hydrator->extract($value);
if (!is_array($data)) {
throw InvalidArgument::withWrongType('array<string, mixed>', $data);
}
$data[$this->typeFieldName] = $this->classToTypeMap[$value::class];
return $data;
}
/**
* @param array<string, mixed> $context
*
* @throws InvalidArgument
*/
public function denormalize(mixed $value, array $context): mixed
{
if ($value === null) {
return null;
}
if (!$this->hydrator) {
throw new MissingHydrator();
}
if (!is_array($value)) {
throw InvalidArgument::withWrongType('array<string, mixed>|null', $value);
}
if (!array_key_exists($this->typeFieldName, $value)) {
throw new InvalidArgument(sprintf('missing type field "%s"', $this->typeFieldName));
}
$type = $value[$this->typeFieldName];
if (!is_string($type)) {
throw InvalidArgument::withWrongType('string', $type);
}
if (!array_key_exists($type, $this->typeToClassMap)) {
throw new InvalidArgument(sprintf('unknown type "%s"', $type));
}
$className = $this->typeToClassMap[$type];
unset($value[$this->typeFieldName]);
return $this->hydrator->hydrate($className, $value);
}
/**
* @return array{
* typeToClassMap: array<string, class-string>,
* classToTypeMap: array<class-string, string>,
* typeFieldName: string,
* hydrator: null
* }
*/
public function __serialize(): array
{
return [
'typeToClassMap' => $this->typeToClassMap,
'classToTypeMap' => $this->classToTypeMap,
'typeFieldName' => $this->typeFieldName,
'hydrator' => null,
];
}
}