-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathColumn.php
More file actions
187 lines (154 loc) · 4.7 KB
/
Column.php
File metadata and controls
187 lines (154 loc) · 4.7 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
<?php
declare(strict_types=1);
namespace Cycle\Schema\Table;
use Cycle\Database\Schema\AbstractColumn;
use Cycle\Schema\Definition\Field;
use Cycle\Schema\Exception\ColumnException;
/**
* Carries information about column definition.
*
* @internal
*/
final class Column
{
// default column value
public const OPT_DEFAULT = 'default';
// column can automatically define default value
public const OPT_CAST_DEFAULT = 'castDefault';
// column can be nullable
public const OPT_NULLABLE = 'nullable';
// provides ability to define complex types using string notation, i.e. string(32)
private const DEFINITION = '/(?P<type>[a-z]+)(?: *\((?P<options>[^\)]+)\))?/i';
/** @var Field */
private $field;
/** @var string */
private $type;
/** @var array */
private $typeOptions = [];
/**
* Parse field definition into table definition.
*
* @throws ColumnException
*
*/
public static function parse(Field $field): self
{
$column = new self();
$column->field = $field;
if (!preg_match(self::DEFINITION, $field->getType(), $type)) {
throw new ColumnException("Invalid column type declaration in `{$field->getType()}`");
}
$column->type = $type['type'];
if (!empty($type['options'])) {
$column->typeOptions = array_map('trim', explode(',', $type['options'] ?? ''));
}
return $column;
}
/**
* Get column name.
*
* @psalm-suppress UnusedMethod
*/
public function getName(): string
{
return $this->field->getColumn();
}
/**
* Get column type.
*
* @psalm-suppress UnusedMethod
*/
public function getType(): string
{
return $this->type;
}
public function isPrimary(): bool
{
return $this->field->isPrimary() || in_array($this->type, ['primary', 'bigPrimary']);
}
public function isNullable(): bool
{
if ($this->hasDefault() && $this->getDefault() === null) {
return true;
}
return $this->hasOption(self::OPT_NULLABLE) && !$this->isPrimary();
}
public function hasDefault(): bool
{
if ($this->isPrimary()) {
return false;
}
return $this->hasOption(self::OPT_DEFAULT);
}
/**
* @return mixed
* @throws ColumnException
*
*/
public function getDefault()
{
if (!$this->hasDefault()) {
throw new ColumnException("No default value on `{$this->field->getColumn()}`");
}
return $this->field->getOptions()->get(self::OPT_DEFAULT);
}
/**
* Render column definition.
*
* @throws ColumnException
*/
public function render(AbstractColumn $column): void
{
$column->nullable($this->isNullable());
try {
// bypassing call to AbstractColumn->type method (or specialized column method)
if (\method_exists($column, $this->type) && $this->typeOptions !== []) {
call_user_func_array([$column, $this->type], $this->typeOptions);
} else {
call_user_func_array([$column, 'type'], \array_merge([$this->type], $this->typeOptions));
}
} catch (\Throwable $e) {
throw new ColumnException(
"Invalid column type definition in '{$column->getTable()}'.'{$column->getName()}'",
(int) $e->getCode(),
$e,
);
}
if ($this->isNullable()) {
$column->defaultValue(null);
}
if ($this->hasDefault() && $this->getDefault() !== null) {
$column->defaultValue($this->getDefault());
} elseif ($this->hasOption(self::OPT_CAST_DEFAULT)) {
$column->defaultValue($this->castDefault($column));
}
$column->setAttributes(\iterator_to_array($this->field->getAttributes()));
}
/**
*
* @return bool|float|int|string
*/
private function castDefault(AbstractColumn $column)
{
if (in_array($column->getAbstractType(), ['timestamp', 'datetime', 'time', 'date'])) {
return 0;
}
if ($column->getAbstractType() === 'enum') {
// we can use first enum value as default
return $column->getEnumValues()[0];
}
switch ($column->getType()) {
case AbstractColumn::INT:
return 0;
case AbstractColumn::FLOAT:
return 0.0;
case AbstractColumn::BOOL:
return false;
}
return '';
}
private function hasOption(string $option): bool
{
return $this->field->getOptions()->has($option);
}
}