1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventSauce\EventSourcing\CodeGeneration; |
4
|
|
|
|
5
|
|
|
abstract class DefinitionWithFields |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var DefinitionGroup |
9
|
|
|
*/ |
10
|
|
|
protected $group; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array[] |
19
|
|
|
*/ |
20
|
|
|
protected $fields = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $fieldsFrom = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array <field, template> |
29
|
|
|
*/ |
30
|
|
|
protected $fieldSerializers = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array <field, template> |
34
|
|
|
*/ |
35
|
|
|
protected $fieldDeserializers = []; |
36
|
|
|
|
37
|
3 |
|
public function __construct(DefinitionGroup $group, string $name) |
38
|
|
|
{ |
39
|
3 |
|
$this->name = $name; |
40
|
3 |
|
$this->group = $group; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $otherType |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
2 |
|
public function withFieldsFrom(string $otherType) |
48
|
|
|
{ |
49
|
2 |
|
$this->fieldsFrom = $otherType; |
50
|
|
|
|
51
|
2 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public function name(): string |
55
|
|
|
{ |
56
|
3 |
|
return $this->name; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
public function fields(): array |
60
|
|
|
{ |
61
|
3 |
|
return $this->fields; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
public function fieldsFrom(): string |
65
|
|
|
{ |
66
|
3 |
|
return $this->fieldsFrom; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public function field(string $name, string $type = null, string $example = null) |
70
|
|
|
{ |
71
|
3 |
|
$type = $this->group->resolveTypeAlias($type); |
72
|
3 |
|
$type = TypeNormalizer::normalize($type ?: $this->group->typeForField($name)); |
73
|
3 |
|
$example = $example ?: $this->group->exampleForField($name); |
74
|
3 |
|
$this->fields[] = compact('name', 'type', 'example'); |
75
|
|
|
|
76
|
3 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function fieldSerializer($field, $template) |
80
|
|
|
{ |
81
|
2 |
|
$this->fieldSerializers[$field] = $template; |
82
|
|
|
|
83
|
2 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
public function serializerForField($field) |
87
|
|
|
{ |
88
|
3 |
|
return $this->fieldSerializers[$field] ?? $this->group->serializerForField($field); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
public function fieldDeserializer($field, $template) |
92
|
|
|
{ |
93
|
2 |
|
$this->fieldDeserializers[$field] = $template; |
94
|
|
|
|
95
|
2 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
public function deserializerForField($field) |
99
|
|
|
{ |
100
|
3 |
|
return $this->fieldDeserializers[$field] ?? $this->group->deserializerForField($field); |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
public function deserializerForType($type) |
104
|
|
|
{ |
105
|
3 |
|
return $this->group->deserializerForType($type); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
public function serializerForType($type) |
109
|
|
|
{ |
110
|
3 |
|
return $this->group->serializerForType($type); |
111
|
|
|
} |
112
|
|
|
} |