1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Database |
5
|
|
|
* |
6
|
|
|
* Platine Database is the abstraction layer using PDO with support of query and schema builder |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Database |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file BaseColumn.php |
33
|
|
|
* |
34
|
|
|
* The Base column schema class |
35
|
|
|
* |
36
|
|
|
* @package Platine\Database\Schema |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Database\Schema; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @class BaseColumn |
51
|
|
|
* @package Platine\Database\Schema |
52
|
|
|
*/ |
53
|
|
|
class BaseColumn |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* The column properties |
57
|
|
|
* @var array<string, mixed> |
58
|
|
|
*/ |
59
|
|
|
protected array $properties = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Class constructor |
63
|
|
|
* @param string $name The name of the column |
64
|
|
|
* @param string|null $type The type of column |
65
|
|
|
*/ |
66
|
|
|
public function __construct(protected string $name, protected ?string $type = null) |
67
|
|
|
{ |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getName(): string |
75
|
|
|
{ |
76
|
|
|
return $this->name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @return string|null |
82
|
|
|
*/ |
83
|
|
|
public function getType(): ?string |
84
|
|
|
{ |
85
|
|
|
return $this->type; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
* @return array<string, mixed> |
91
|
|
|
*/ |
92
|
|
|
public function getProperties(): array |
93
|
|
|
{ |
94
|
|
|
return $this->properties; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* @param string $type |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setType(string $type): self |
103
|
|
|
{ |
104
|
|
|
$this->type = $type; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the value for the given property name |
111
|
|
|
* @param string $name |
112
|
|
|
* @param mixed $value |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function set(string $name, mixed $value): self |
116
|
|
|
{ |
117
|
|
|
$this->properties[$name] = $value; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Check whether there is the property for |
124
|
|
|
* the given name |
125
|
|
|
* @param string $name |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function has(string $name): bool |
129
|
|
|
{ |
130
|
|
|
return isset($this->properties[$name]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the property value for the given name |
135
|
|
|
* if not exist the value of $default will be returned |
136
|
|
|
* @param string $name |
137
|
|
|
* @param mixed $default |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
public function get(string $name, mixed $default = null): mixed |
141
|
|
|
{ |
142
|
|
|
return isset($this->properties[$name]) |
143
|
|
|
? $this->properties[$name] |
144
|
|
|
: $default; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the size for the column |
149
|
|
|
* @param string $value |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function size(string $value): self |
153
|
|
|
{ |
154
|
|
|
$name = strtolower($value); |
155
|
|
|
if (!in_array($name, ['tiny', 'normal', 'small', 'medium', 'big'])) { |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->set('size', $name); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function notNull(): self |
167
|
|
|
{ |
168
|
|
|
return $this->set('nullable', false); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set comment for the column |
173
|
|
|
* @param string $comment |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function description(string $comment): self |
177
|
|
|
{ |
178
|
|
|
return $this->set('description', $comment); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Set default value for the column |
183
|
|
|
* @param mixed $value |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function defaultValue(mixed $value): self |
187
|
|
|
{ |
188
|
|
|
return $this->set('default', $value); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* |
193
|
|
|
* @param bool $value |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
|
|
public function unsigned(bool $value = true): self |
197
|
|
|
{ |
198
|
|
|
return $this->set('unsigned', $value); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set length for the column |
203
|
|
|
* @param mixed $value |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function length(mixed $value): self |
207
|
|
|
{ |
208
|
|
|
return $this->set('length', $value); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|