1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\FluidSchema; |
5
|
|
|
|
6
|
|
|
use function addslashes; |
7
|
|
|
use Doctrine\DBAL\Schema\Table; |
8
|
|
|
use function in_array; |
9
|
|
|
|
10
|
|
|
class TdbmFluidTable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var FluidSchema|TdbmFluidSchema |
14
|
|
|
*/ |
15
|
|
|
private $schema; |
16
|
|
|
/** |
17
|
|
|
* @var FluidTable |
18
|
|
|
*/ |
19
|
|
|
private $fluidTable; |
20
|
|
|
/** |
21
|
|
|
* @var NamingStrategyInterface |
22
|
|
|
*/ |
23
|
|
|
private $namingStrategy; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array<string, TdbmFluidColumn> |
27
|
|
|
*/ |
28
|
|
|
private $tdbmFluidColumns = []; |
29
|
|
|
|
30
|
|
|
public function __construct(TdbmFluidSchema $schema, FluidTable $fluidTable, NamingStrategyInterface $namingStrategy) |
31
|
|
|
{ |
32
|
|
|
$this->schema = $schema; |
33
|
|
|
$this->fluidTable = $fluidTable; |
34
|
|
|
$this->namingStrategy = $namingStrategy; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function column(string $name): TdbmFluidColumn |
38
|
|
|
{ |
39
|
|
|
if (!isset($this->tdbmFluidColumns[$name])) { |
40
|
|
|
$this->tdbmFluidColumns[$name] = new TdbmFluidColumn($this, $this->fluidTable->column($name), $this->namingStrategy); |
41
|
|
|
} |
42
|
|
|
return $this->tdbmFluidColumns[$name]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function index(array $columnNames): TdbmFluidTable |
46
|
|
|
{ |
47
|
|
|
$this->fluidTable->index($columnNames); |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function unique(array $columnNames): TdbmFluidTable |
52
|
|
|
{ |
53
|
|
|
$this->fluidTable->unique($columnNames); |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function primaryKey(array $columnNames, ?string $indexName = null): TdbmFluidTable |
58
|
|
|
{ |
59
|
|
|
$this->fluidTable->primaryKey($columnNames, $indexName); |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function id(): TdbmFluidColumnOptions |
64
|
|
|
{ |
65
|
|
|
$this->fluidTable->id(); |
66
|
|
|
return $this->column('id')->integer(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function uuid(string $version = 'v4'): TdbmFluidColumnOptions |
70
|
|
|
{ |
71
|
|
|
if ($version !== 'v1' && $version !== 'v4') { |
72
|
|
|
throw new FluidSchemaException('UUID version must be one of "v1" or "v4"'); |
73
|
|
|
} |
74
|
|
|
$this->fluidTable->uuid(); |
75
|
|
|
|
76
|
|
|
$this->column('uuid')->guid()->addAnnotation('UUID', $version); |
77
|
|
|
return $this->column('uuid')->guid(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function timestamps(): self |
81
|
|
|
{ |
82
|
|
|
$this->fluidTable->timestamps(); |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @throws FluidSchemaException |
88
|
|
|
*/ |
89
|
|
|
public function extends(string $tableName): self |
90
|
|
|
{ |
91
|
|
|
$this->fluidTable->extends($tableName); |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Adds a "Bean" annotation to the table. |
97
|
|
|
*/ |
98
|
|
|
public function customBeanName(string $beanName): self |
99
|
|
|
{ |
100
|
|
|
$this->addAnnotation('Bean', ['name'=>$beanName]); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adds a "Type" annotation. |
106
|
|
|
*/ |
107
|
|
|
public function graphqlType(): self |
108
|
|
|
{ |
109
|
|
|
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Type'); |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Makes the generated bean implement the interface $interfaceName |
115
|
|
|
* |
116
|
|
|
* @param string $interfaceName The fully qualified name of the PHP interface to implement. |
117
|
|
|
* @return TdbmFluidTable |
118
|
|
|
*/ |
119
|
|
|
public function implementsInterface(string $interfaceName): self |
120
|
|
|
{ |
121
|
|
|
$this->addAnnotation('AddInterface', ['name' => $interfaceName], false); |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Makes the generated DAO implement the interface $interfaceName |
127
|
|
|
* |
128
|
|
|
* @param string $interfaceName The fully qualified name of the PHP interface to implement. |
129
|
|
|
* @return TdbmFluidTable |
130
|
|
|
*/ |
131
|
|
|
public function implementsInterfaceOnDao(string $interfaceName): self |
132
|
|
|
{ |
133
|
|
|
$this->addAnnotation('AddInterfaceOnDao', ['name' => $interfaceName], false); |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Makes the generated bean implement the trait $traitName |
139
|
|
|
* |
140
|
|
|
* @param string $traitName |
141
|
|
|
* @param string[] $modifiers An array of modifiers. For instance: ['A::foo insteadof B'] |
142
|
|
|
* @return TdbmFluidTable |
143
|
|
|
*/ |
144
|
|
|
public function useTrait(string $traitName, array $modifiers = []): self |
145
|
|
|
{ |
146
|
|
|
$this->addAnnotation('AddTrait', ['name' => $traitName, 'modifiers' => $modifiers], false); |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Makes the generated DAO implement the trait $traitName |
152
|
|
|
* |
153
|
|
|
* @param string $traitName |
154
|
|
|
* @param string[] $modifiers An array of modifiers. For instance: ['A::foo insteadof B'] |
155
|
|
|
* @return TdbmFluidTable |
156
|
|
|
*/ |
157
|
|
|
public function useTraitOnDao(string $traitName, array $modifiers = []): self |
158
|
|
|
{ |
159
|
|
|
$this->addAnnotation('AddTraitOnDao', ['name' => $traitName, 'modifiers' => $modifiers], false); |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function getComment(): Comment |
164
|
|
|
{ |
165
|
|
|
$options = $this->fluidTable->getDbalTable()->getOptions(); |
166
|
|
|
$comment = $options['comment'] ?? ''; |
167
|
|
|
|
168
|
|
|
return new Comment($comment); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function saveComment(Comment $comment): self |
172
|
|
|
{ |
173
|
|
|
$this->fluidTable->getDbalTable()->addOption('comment', $comment->getComment()); |
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $annotation |
179
|
|
|
* @param mixed $content |
180
|
|
|
* @param bool $replaceExisting |
181
|
|
|
* @return TdbmFluidTable |
182
|
|
|
*/ |
183
|
|
|
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self |
184
|
|
|
{ |
185
|
|
|
$comment = $this->getComment()->addAnnotation($annotation, $content, $replaceExisting, $explicitNull); |
186
|
|
|
$this->saveComment($comment); |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function removeAnnotation(string $annotation): self |
191
|
|
|
{ |
192
|
|
|
$comment = $this->getComment()->removeAnnotation($annotation); |
193
|
|
|
$this->saveComment($comment); |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getDbalTable(): Table |
198
|
|
|
{ |
199
|
|
|
return $this->fluidTable->getDbalTable(); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|