1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EventSauce\EventSourcing\CodeGeneration; |
6
|
|
|
|
7
|
|
|
use EventSauce\EventSourcing\PointInTime; |
8
|
|
|
|
9
|
|
|
final class DefinitionGroup |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $namespace; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var EventDefinition[] |
18
|
|
|
*/ |
19
|
|
|
private $events = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $defaults = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $typeSerializer = [ |
30
|
|
|
'string' => '({type}) {param}', |
31
|
|
|
'array' => '({type}) {param}', |
32
|
|
|
'integer' => '({type}) {param}', |
33
|
|
|
'int' => '({type}) {param}', |
34
|
|
|
'bool' => '({type}) {param}', |
35
|
|
|
'float' => '({type}) {param}', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $typeDeserializer = [ |
42
|
|
|
'string' => '({type}) {param}', |
43
|
|
|
'array' => '({type}) {param}', |
44
|
|
|
'integer' => '({type}) {param}', |
45
|
|
|
'int' => '({type}) {param}', |
46
|
|
|
'bool' => '({type}) {param}', |
47
|
|
|
'float' => '({type}) {param}', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $fieldSerializer = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $fieldDeserializer = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var CommandDefinition[] |
62
|
|
|
*/ |
63
|
|
|
private $commands = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string[] |
67
|
|
|
*/ |
68
|
|
|
private $typeAliases = []; |
69
|
|
|
|
70
|
4 |
|
public function __construct() |
71
|
|
|
{ |
72
|
4 |
|
$this->typeSerializer(PointInTime::class, '{param}->toString()'); |
73
|
4 |
|
$this->typeDeserializer(PointInTime::class, '{type}::fromString({param})'); |
74
|
4 |
|
} |
75
|
|
|
|
76
|
|
|
public static function create(string $namespace): DefinitionGroup |
77
|
|
|
{ |
78
|
|
|
return (new DefinitionGroup())->withNamespace($namespace); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
public function withNamespace(string $namespace): DefinitionGroup |
82
|
|
|
{ |
83
|
3 |
|
$this->namespace = $namespace; |
84
|
|
|
|
85
|
3 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
public function typeSerializer(string $type, string $template) |
89
|
|
|
{ |
90
|
4 |
|
$type = $this->resolveTypeAlias($type); |
91
|
|
|
|
92
|
4 |
|
$this->typeSerializer[TypeNormalizer::normalize($type)] = $template; |
93
|
4 |
|
} |
94
|
|
|
|
95
|
7 |
|
public function serializerForType($type) |
96
|
|
|
{ |
97
|
7 |
|
$type = $this->resolveTypeAlias($type); |
98
|
|
|
|
99
|
7 |
|
return $this->typeSerializer[$type] ?? 'new {type}({param})'; |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
public function typeDeserializer(string $type, string $template) |
103
|
|
|
{ |
104
|
4 |
|
$type = $this->resolveTypeAlias($type); |
105
|
|
|
|
106
|
4 |
|
$this->typeDeserializer[TypeNormalizer::normalize($type)] = $template; |
107
|
4 |
|
} |
108
|
|
|
|
109
|
7 |
|
public function deserializerForType($type) |
110
|
|
|
{ |
111
|
7 |
|
$type = $this->resolveTypeAlias($type); |
112
|
|
|
|
113
|
7 |
|
return $this->typeDeserializer[$type] ?? 'new {type}({param})'; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function fieldSerializer(string $field, string $template) |
117
|
|
|
{ |
118
|
1 |
|
$this->fieldSerializer[$field] = $template; |
119
|
1 |
|
} |
120
|
|
|
|
121
|
8 |
|
public function serializerForField($field) |
122
|
|
|
{ |
123
|
8 |
|
return $this->fieldSerializer[$field] ?? null; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
public function fieldDeserializer(string $field, string $template) |
127
|
|
|
{ |
128
|
1 |
|
$this->fieldDeserializer[$field] = $template; |
129
|
1 |
|
} |
130
|
|
|
|
131
|
8 |
|
public function deserializerForField($field) |
132
|
|
|
{ |
133
|
8 |
|
return $this->fieldDeserializer[$field] ?? null; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function fieldDefault(string $name, string $type, string $example = null) |
137
|
|
|
{ |
138
|
1 |
|
$type = $this->resolveTypeAlias($type); |
139
|
1 |
|
$this->defaults[$name] = compact('type', 'example'); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
1 |
|
public function aliasType(string $alias, string $type) |
143
|
|
|
{ |
144
|
1 |
|
$this->typeAliases[$alias] = TypeNormalizer::normalize($type); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
11 |
|
public function resolveTypeAlias(string $alias = null) |
148
|
|
|
{ |
149
|
11 |
|
while (isset($this->typeAliases[$alias])) { |
150
|
1 |
|
$alias = $this->typeAliases[$alias]; |
151
|
|
|
} |
152
|
|
|
|
153
|
11 |
|
return $alias; |
154
|
|
|
} |
155
|
|
|
|
156
|
3 |
|
public function event(string $name): EventDefinition |
157
|
|
|
{ |
158
|
3 |
|
return $this->events[] = new EventDefinition($this, $name); |
159
|
|
|
} |
160
|
|
|
|
161
|
4 |
|
public function command(string $name) |
162
|
|
|
{ |
163
|
4 |
|
return $this->commands[] = new CommandDefinition($this, $name); |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
public function typeForField(string $field): string |
167
|
|
|
{ |
168
|
1 |
|
return $this->defaults[$field]['type'] ?? 'string'; |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
public function exampleForField(string $field) |
172
|
|
|
{ |
173
|
1 |
|
return $this->defaults[$field]['example'] ?? null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return EventDefinition[] |
178
|
|
|
*/ |
179
|
12 |
|
public function events(): array |
180
|
|
|
{ |
181
|
12 |
|
return $this->events; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return CommandDefinition[] |
186
|
|
|
*/ |
187
|
12 |
|
public function commands(): array |
188
|
|
|
{ |
189
|
12 |
|
return $this->commands; |
190
|
|
|
} |
191
|
|
|
|
192
|
11 |
|
public function namespace(): string |
193
|
|
|
{ |
194
|
11 |
|
return $this->namespace; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|