-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DefinitionGroup.php
230 lines (184 loc) · 5.59 KB
/
DefinitionGroup.php
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
declare(strict_types=1);
namespace EventSauce\EventSourcing\CodeGeneration;
use DateTimeImmutable;
use OutOfBoundsException;
use function array_key_exists;
final class DefinitionGroup
{
private string $namespace;
/**
* @var PayloadDefinition[]
*/
private array $events = [];
private array $defaults = [];
/**
* @var array<string, string>
*/
private array $typeSerializer = [
'string' => '({type}) {param}',
'array' => '({type}) {param}',
'integer' => '({type}) {param}',
'int' => '({type}) {param}',
'bool' => '({type}) {param}',
'float' => '({type}) {param}',
];
/**
* @var array<string, string>
*/
private array $typeDeserializer = [
'string' => '({type}) {param}',
'array' => '({type}) {param}',
'integer' => '({type}) {param}',
'int' => '({type}) {param}',
'bool' => '({type}) {param}',
'float' => '({type}) {param}',
];
/**
* @var array<string, string>
*/
private array $fieldSerializer = [];
/**
* @var array<string, string>
*/
private array $fieldDeserializer = [];
/**
* @var PayloadDefinition[]
*/
private array $commands = [];
/**
* @var array<string, string>
*/
private array $typeAliases = [];
/**
* @var array<string, class-string>
*/
private array $interfaces = [];
/**
* @var array<string, bool>
*/
private $nullable = [];
public function __construct()
{
$this->typeSerializer(DateTimeImmutable::class, '{param}->format(\'Y-m-d H:i:s.uO\')');
$this->typeDeserializer(DateTimeImmutable::class, '{type}::createFromFormat(\'Y-m-d H:i:s.uO\', {param})');
}
public static function create(string $namespace): DefinitionGroup
{
return (new DefinitionGroup())->withNamespace($namespace);
}
public function withNamespace(string $namespace): DefinitionGroup
{
$this->namespace = $namespace;
return $this;
}
public function typeSerializer(string $type, string $template): void
{
/** @var string $type */
$type = $this->resolveTypeAlias($type);
$this->typeSerializer[TypeNormalizer::normalize($type)] = $template;
}
public function serializerForType(string $type): string
{
$type = $this->resolveTypeAlias($type);
return $this->typeSerializer[$type] ?? 'new {type}({param})';
}
public function typeDeserializer(string $type, string $template): void
{
$type = $this->resolveTypeAlias($type);
$this->typeDeserializer[TypeNormalizer::normalize($type)] = $template;
}
public function deserializerForType(string $type): string
{
$type = $this->resolveTypeAlias($type);
return $this->typeDeserializer[$type] ?? 'new {type}({param})';
}
public function fieldSerializer(string $field, string $template): void
{
$this->fieldSerializer[$field] = $template;
}
public function serializerForField(string $field): ?string
{
return $this->fieldSerializer[$field] ?? null;
}
public function fieldDeserializer(string $field, string $template): void
{
$this->fieldDeserializer[$field] = $template;
}
public function deserializerForField(string $field): ?string
{
return $this->fieldDeserializer[$field] ?? null;
}
public function fieldDefault(string $name, string $type, ?string $example = null, ?bool $nullable = null): void
{
$type = $this->resolveTypeAlias($type);
$this->defaults[$name] = compact('type', 'example', 'nullable');
}
public function aliasType(string $alias, string $type): void
{
$this->typeAliases[$alias] = TypeNormalizer::normalize($type);
}
public function resolveTypeAlias(string $alias): string
{
while (isset($this->typeAliases[$alias])) {
$alias = $this->typeAliases[$alias];
}
return $alias;
}
public function event(string $name): PayloadDefinition
{
return $this->events[] = new PayloadDefinition($this, $name);
}
public function command(string $name): PayloadDefinition
{
return $this->commands[] = new PayloadDefinition($this, $name);
}
public function typeForField(string $field): string
{
return $this->defaults[$field]['type'] ?? 'string';
}
public function exampleForField(string $field): mixed
{
return $this->defaults[$field]['example'] ?? null;
}
/**
* @return PayloadDefinition[]
*/
public function events(): array
{
return $this->events;
}
/**
* @return PayloadDefinition[]
*/
public function commands(): array
{
return $this->commands;
}
public function namespace(): string
{
return $this->namespace;
}
/**
* @param class-string $interfaceName
*/
public function defineInterface(string $alias, string $interfaceName): void
{
$this->interfaces[$alias] = $interfaceName;
}
public function resolveInterface(string $alias): string
{
if ( ! array_key_exists($alias, $this->interfaces)) {
throw new OutOfBoundsException("Interface not registered for alias {$alias}.");
}
return $this->interfaces[$alias];
}
public function setTypeNullability(string $type, bool $nullable): void
{
$this->nullable[$type] = $nullable;
}
public function isTypeNullable(string $type): ?bool
{
return $this->nullable[$type] ?? null;
}
}