|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\Annotated\Annotation; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\ORM\Parser\Typecast; |
|
8
|
|
|
use Doctrine\Common\Annotations\Annotation\Target; |
|
9
|
|
|
use JetBrains\PhpStorm\ExpectedValues; |
|
10
|
|
|
use Spiral\Attributes\NamedArgumentConstructor; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @Annotation |
|
14
|
|
|
* @NamedArgumentConstructor |
|
15
|
|
|
* @Target({"PROPERTY", "ANNOTATION", "CLASS"}) |
|
16
|
|
|
*/ |
|
17
|
|
|
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] |
|
18
|
|
|
#[NamedArgumentConstructor] |
|
19
|
|
|
final class Column |
|
20
|
|
|
{ |
|
21
|
|
|
private bool $hasDefault = false; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array<non-empty-string, mixed> Other database specific attributes. |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
|
|
private array $attributes; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param non-empty-string $type Column type. {@see \Cycle\Database\Schema\AbstractColumn::$mapping} |
|
|
|
|
|
|
29
|
|
|
* Column types `smallPrimary`, `timetz`, `timestamptz`, `interval`, `bitVarying`, `int4range`, `int8range`, |
|
30
|
|
|
* `numrange`, `tsrange`, `tstzrange`, `daterange`, `jsonb`, `point`, `line`, `lseg`, `box`, `path`, |
|
31
|
|
|
* `polygon`, `circle`, `cidr`, `inet`, `macaddr`, `macaddr8`, `tsvector`, `tsquery` are related |
|
32
|
|
|
* to the PostgreSQL only {@see \Cycle\Database\Driver\Postgres\Schema\PostgresColumn::$mapping} |
|
33
|
|
|
* Column type `datetime2` is related to the SQL Server only |
|
34
|
|
|
* {@see \Cycle\Database\Driver\SQLServer\Schema\SQLServerColumn::$mapping} |
|
35
|
|
|
* @param non-empty-string|null $name Column name. Defaults to the property name. |
|
36
|
|
|
* @param non-empty-string|null $property Property that belongs to column. For virtual columns. |
|
37
|
|
|
* @param bool $primary Explicitly set column as a primary key. |
|
38
|
|
|
* @param bool $nullable Set column as nullable. |
|
39
|
1032 |
|
* @param mixed|null $default Default column value. |
|
40
|
|
|
* @param callable|non-empty-string|null $typecast Typecast rule name. |
|
41
|
|
|
* Regarding the default Typecast handler {@see Typecast} the value can be `callable` or |
|
42
|
|
|
* one of ("int"|"float"|"bool"|"datetime") based on column type. |
|
43
|
|
|
* If you want to use another rule you should add in the `typecast` argument of the {@see Entity} attribute |
|
44
|
|
|
* a relevant Typecast handler that supports the rule. |
|
45
|
|
|
* @param bool $castDefault |
|
46
|
|
|
* @param bool $readonlySchema Set to true to disable schema synchronization for the assigned column. |
|
47
|
|
|
* @param mixed ...$attributes Other database specific attributes. Use named notation to define them. |
|
48
|
|
|
* For example: #[Column('smallInt', unsigned: true, zerofill: true)] |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
#[ExpectedValues(values: ['primary', 'bigPrimary', 'enum', 'boolean', |
|
52
|
|
|
'integer', 'tinyInteger', 'smallInteger', 'bigInteger', 'string', 'text', 'tinyText', 'longText', 'double', |
|
53
|
1032 |
|
'float', 'decimal', 'datetime', 'date', 'time', 'timestamp', 'binary', 'tinyBinary', 'longBinary', 'json', |
|
54
|
496 |
|
'uuid', 'bit', |
|
55
|
|
|
// PostgreSQL |
|
56
|
1032 |
|
'smallPrimary', 'timetz', 'timestamptz', 'interval', 'bitVarying', 'int4range', 'int8range', 'numrange', |
|
57
|
|
|
'tsrange', 'tstzrange', 'daterange', 'jsonb', 'point', 'line', 'lseg', 'box', 'path', 'polygon', 'circle', |
|
58
|
1032 |
|
'cidr', 'inet', 'macaddr', 'macaddr8', 'tsvector', 'tsquery', |
|
59
|
|
|
// SQL Server |
|
60
|
1032 |
|
'datetime2', |
|
61
|
|
|
])] |
|
62
|
|
|
private string $type, |
|
63
|
1032 |
|
private ?string $name = null, |
|
64
|
|
|
private ?string $property = null, |
|
65
|
1032 |
|
private bool $primary = false, |
|
66
|
|
|
private bool $nullable = false, |
|
67
|
|
|
private mixed $default = null, |
|
68
|
472 |
|
private mixed $typecast = null, |
|
69
|
|
|
private bool $castDefault = false, |
|
70
|
472 |
|
private bool $readonlySchema = false, |
|
71
|
|
|
mixed ...$attributes, |
|
72
|
|
|
) { |
|
73
|
1032 |
|
if ($default !== null) { |
|
74
|
|
|
$this->hasDefault = true; |
|
75
|
1032 |
|
} |
|
76
|
|
|
$this->attributes = $attributes; |
|
77
|
|
|
} |
|
78
|
1032 |
|
|
|
79
|
|
|
public function getType(): ?string |
|
80
|
1032 |
|
{ |
|
81
|
|
|
return $this->type; |
|
82
|
|
|
} |
|
83
|
1032 |
|
|
|
84
|
|
|
public function getColumn(): ?string |
|
85
|
1032 |
|
{ |
|
86
|
|
|
return $this->name; |
|
87
|
|
|
} |
|
88
|
496 |
|
|
|
89
|
|
|
public function getProperty(): ?string |
|
90
|
496 |
|
{ |
|
91
|
|
|
return $this->property; |
|
92
|
|
|
} |
|
93
|
1032 |
|
|
|
94
|
|
|
public function isNullable(): bool |
|
95
|
1032 |
|
{ |
|
96
|
|
|
return $this->nullable; |
|
97
|
|
|
} |
|
98
|
1032 |
|
|
|
99
|
|
|
public function isPrimary(): bool |
|
100
|
1032 |
|
{ |
|
101
|
|
|
return $this->primary; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function hasDefault(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->hasDefault; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getDefault(): mixed |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->default; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function castDefault(): bool |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->castDefault; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getTypecast(): mixed |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->typecast; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function isReadonlySchema(): bool |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->readonlySchema; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return array<non-empty-string, mixed> |
|
|
|
|
|
|
131
|
|
|
*/ |
|
132
|
|
|
public function getAttributes(): array |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->attributes; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|