-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRegistryModifier.php
More file actions
352 lines (290 loc) · 12.1 KB
/
RegistryModifier.php
File metadata and controls
352 lines (290 loc) · 12.1 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
declare(strict_types=1);
namespace Cycle\ORM\Entity\Behavior\Schema;
use Cycle\Database\Schema\AbstractColumn;
use Cycle\Database\Schema\AbstractTable;
use Cycle\ORM\Entity\Behavior\Exception\BehaviorCompilationException;
use Cycle\ORM\Parser\Typecast;
use Cycle\ORM\Parser\TypecastInterface;
use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Defaults;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\Definition\Field;
use Cycle\Schema\Definition\Map\FieldMap;
use Cycle\Schema\Registry;
/**
* @internal
*/
class RegistryModifier
{
protected const DEFINITION = '/(?P<type>[a-z]+)(?: *\((?P<options>[^\)]+)\))?/i';
protected const INTEGER_TYPES = [
'int',
'smallint',
'tinyint',
'bigint',
'integer',
'tinyInteger',
'smallInteger',
'bigInteger',
];
protected const BIG_INTEGER_TYPES = [
'bigint',
'bigInteger',
];
protected const DATETIME_TYPES = ['datetime', 'datetime2'];
protected const INT_COLUMN = AbstractColumn::INT;
protected const STRING_COLUMN = AbstractColumn::STRING;
protected const BIG_INTEGER_COLUMN = 'bigInteger';
protected const DATETIME_COLUMN = 'datetime';
protected const SNOWFLAKE_COLUMN = 'snowflake';
protected const ULID_COLUMN = 'ulid';
protected const UUID_COLUMN = 'uuid';
protected FieldMap $fields;
protected AbstractTable $table;
protected Entity $entity;
protected Defaults $defaults;
public function __construct(Registry $registry, string $role)
{
$this->entity = $registry->getEntity($role);
$this->fields = $this->entity->getFields();
$this->table = $registry->getTableSchema($this->entity);
$this->defaults = $registry->getDefaults();
}
public static function isIntegerType(string $type): bool
{
\preg_match(self::DEFINITION, $type, $matches);
return \in_array($matches['type'], self::INTEGER_TYPES, true);
}
public static function isBigIntegerType(string $type): bool
{
\preg_match(self::DEFINITION, $type, $matches);
return \in_array($matches['type'], self::BIG_INTEGER_TYPES, true);
}
public static function isDatetimeType(string $type): bool
{
\preg_match(self::DEFINITION, $type, $matches);
return \in_array($matches['type'], self::DATETIME_TYPES, true);
}
public static function isStringType(string $type): bool
{
\preg_match(self::DEFINITION, $type, $matches);
return $matches['type'] === 'string';
}
public static function isSnowflakeType(string $type): bool
{
\preg_match(self::DEFINITION, $type, $matches);
return $matches['type'] === self::SNOWFLAKE_COLUMN;
}
public static function isUlidType(string $type): bool
{
\preg_match(self::DEFINITION, $type, $matches);
return $matches['type'] === self::ULID_COLUMN;
}
public static function isUuidType(string $type): bool
{
\preg_match(self::DEFINITION, $type, $matches);
return $matches['type'] === self::UUID_COLUMN;
}
public function addDatetimeColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn
{
if ($this->fields->has($fieldName)) {
if (!static::isDatetimeType($this->fields->get($fieldName)->getType())) {
throw new BehaviorCompilationException(\sprintf('Field %s must be of type datetime.', $fieldName));
}
$this->validateColumnName($fieldName, $columnName);
$this->fields->get($fieldName)->setGenerated($generated);
return $this->table->column($columnName);
}
$field = (new Field())
->setColumn($columnName)
->setType('datetime')
->setTypecast('datetime')
->setGenerated($generated);
$this->fields->set($fieldName, $field);
return $this->table->column($columnName)->type(self::DATETIME_COLUMN);
}
public function addIntegerColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn
{
if ($this->fields->has($fieldName)) {
if (!static::isIntegerType($this->fields->get($fieldName)->getType())) {
throw new BehaviorCompilationException(\sprintf('Field %s must be of type integer.', $fieldName));
}
$this->validateColumnName($fieldName, $columnName);
$this->fields->get($fieldName)->setGenerated($generated);
return $this->table->column($columnName);
}
$field = (new Field())
->setColumn($columnName)
->setType('integer')
->setTypecast('int')
->setGenerated($generated);
$this->fields->set($fieldName, $field);
return $this->table->column($columnName)->type(self::INT_COLUMN);
}
public function addBigIntegerColumn(
string $columnName,
string $fieldName,
int|null $generated = null,
): AbstractColumn {
if ($this->fields->has($fieldName)) {
if (! static::isBigIntegerType($this->fields->get($fieldName)->getType())) {
throw new BehaviorCompilationException(\sprintf('Field %s must be of type big integer.', $fieldName));
}
$this->validateColumnName($fieldName, $columnName);
$this->fields->get($fieldName)->setGenerated($generated);
return $this->table->column($columnName);
}
$field = (new Field())
->setColumn($columnName)
->setType(self::BIG_INTEGER_COLUMN)
->setTypecast('int')
->setGenerated($generated);
$this->fields->set($fieldName, $field);
return $this->table->column($columnName)->type(self::BIG_INTEGER_COLUMN);
}
public function addStringColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn
{
if ($this->fields->has($fieldName)) {
if (!static::isStringType($this->fields->get($fieldName)->getType())) {
throw new BehaviorCompilationException(\sprintf('Field %s must be of type string.', $fieldName));
}
$this->validateColumnName($fieldName, $columnName);
$this->fields->get($fieldName)->setGenerated($generated);
return $this->table->column($columnName);
}
$field = (new Field())->setColumn($columnName)->setType('string')->setGenerated($generated);
$this->fields->set($fieldName, $field);
return $this->table->column($columnName)->type(self::STRING_COLUMN);
}
/**
* @param non-empty-string $columnName
* @throws BehaviorCompilationException
*/
public function addSnowflakeColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn
{
if ($this->fields->has($fieldName)) {
if (!static::isSnowflakeType($this->fields->get($fieldName)->getType())) {
throw new BehaviorCompilationException(
\sprintf('Field %s must be of type %s.', $fieldName, self::SNOWFLAKE_COLUMN),
);
}
$this->validateColumnName($fieldName, $columnName);
$this->fields->get($fieldName)->setGenerated($generated);
return $this->table->column($columnName);
}
$field = (new Field())->setColumn($columnName)->setType(self::SNOWFLAKE_COLUMN)->setGenerated($generated);
$this->fields->set($fieldName, $field);
return $this->table->column($columnName)->type(self::SNOWFLAKE_COLUMN);
}
/**
* @param non-empty-string $columnName
* @throws BehaviorCompilationException
*/
public function addUlidColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn
{
if ($this->fields->has($fieldName)) {
if (!static::isUlidType($this->fields->get($fieldName)->getType())) {
throw new BehaviorCompilationException(
\sprintf('Field %s must be of type %s.', $fieldName, self::ULID_COLUMN),
);
}
$this->validateColumnName($fieldName, $columnName);
$this->fields->get($fieldName)->setGenerated($generated);
return $this->table->column($columnName);
}
$field = (new Field())->setColumn($columnName)->setType(self::ULID_COLUMN)->setGenerated($generated);
$this->fields->set($fieldName, $field);
return $this->table->column($columnName)->type(self::ULID_COLUMN);
}
/**
* @param non-empty-string $columnName
* @throws BehaviorCompilationException
*/
public function addUuidColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn
{
if ($this->fields->has($fieldName)) {
if (!static::isUuidType($this->fields->get($fieldName)->getType())) {
throw new BehaviorCompilationException(
\sprintf('Field %s must be of type %s.', $fieldName, self::UUID_COLUMN),
);
}
$this->validateColumnName($fieldName, $columnName);
$this->fields->get($fieldName)->setGenerated($generated);
return $this->table->column($columnName);
}
$field = (new Field())->setColumn($columnName)->setType(self::UUID_COLUMN)->setGenerated($generated);
$this->fields->set($fieldName, $field);
return $this->table->column($columnName)->type(self::UUID_COLUMN);
}
public function findColumnName(string $fieldName, ?string $columnName): ?string
{
if ($columnName !== null) {
return $columnName;
}
return $this->fields->has($fieldName) ? $this->fields->get($fieldName)->getColumn() : null;
}
/**
* @param class-string<TypecastInterface> $handler
*/
public function setTypecast(Field $field, array|string|null $rule, string $handler = Typecast::class): Field
{
if ($field->getTypecast() === null) {
$field->setTypecast($rule);
}
$defaultHandlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? [];
if (!\is_array($defaultHandlers)) {
$defaultHandlers = [$defaultHandlers];
}
$handlers = $this->entity->getTypecast() ?? [];
if (!\is_array($handlers)) {
$handlers = [$handlers];
}
if (!\in_array($handler, $handlers, true) && !\in_array($handler, $defaultHandlers, true)) {
$this->entity->setTypecast(\array_merge($handlers, [$handler]));
}
return $field;
}
/**
* @throws BehaviorCompilationException
*/
protected function validateColumnName(string $fieldName, string $columnName): void
{
$field = $this->fields->get($fieldName);
if ($field->getColumn() !== $columnName) {
throw new BehaviorCompilationException(
\sprintf(
'Ambiguous column name definition. '
. 'The `%s` field already linked with the `%s` column but the behavior expects `%s`.',
$fieldName,
$field->getColumn(),
$columnName,
),
);
}
}
/**
* @deprecated since v1.2
*
* @param non-empty-string $type
* @param non-empty-string $fieldName
* @param non-empty-string $columnName
*/
protected function isType(string $type, string $fieldName, string $columnName): bool
{
if ($type === self::DATETIME_COLUMN) {
return
$this->table->column($columnName)->getInternalType() === self::DATETIME_COLUMN ||
$this->fields->get($fieldName)->getType() === self::DATETIME_COLUMN;
}
if ($type === self::INT_COLUMN) {
return $this->table->column($columnName)->getType() === self::INT_COLUMN;
}
if ($type === self::UUID_COLUMN) {
return
$this->table->column($columnName)->getInternalType() === self::UUID_COLUMN ||
$this->fields->get($fieldName)->getType() === self::UUID_COLUMN;
}
return $this->table->column($columnName)->getType() === $type;
}
}