|
4 | 4 |
|
5 | 5 | namespace Doctrine\DBAL\Schema; |
6 | 6 |
|
| 7 | +use Doctrine\DBAL\Platforms\SQLServerPlatform; |
7 | 8 | use Doctrine\DBAL\Schema\Exception\UnknownColumnOption; |
8 | 9 | use Doctrine\DBAL\Schema\Name\Parser\UnqualifiedNameParser; |
9 | 10 | use Doctrine\DBAL\Schema\Name\Parsers; |
|
16 | 17 | /** |
17 | 18 | * Object representation of a database column. |
18 | 19 | * |
| 20 | + * @final |
19 | 21 | * @extends AbstractNamedObject<UnqualifiedName> |
20 | 22 | * @phpstan-type ColumnProperties = array{ |
21 | 23 | * name: UnqualifiedName, |
22 | 24 | * type: Type, |
23 | 25 | * default: mixed, |
24 | 26 | * notnull?: bool, |
25 | 27 | * autoincrement: bool, |
26 | | - * columnDefinition: ?string, |
| 28 | + * columnDefinition: ?non-empty-string, |
27 | 29 | * comment: string, |
28 | | - * charset?: ?string, |
29 | | - * collation?: ?string, |
| 30 | + * charset?: ?non-empty-string, |
| 31 | + * collation?: ?non-empty-string, |
30 | 32 | * } |
31 | 33 | * @phpstan-type PlatformOptions = array{ |
32 | | - * charset?: ?string, |
33 | | - * collation?: ?string, |
34 | | - * default_constraint_name?: string, |
| 34 | + * charset?: ?non-empty-string, |
| 35 | + * collation?: ?non-empty-string, |
| 36 | + * default_constraint_name?: non-empty-string, |
35 | 37 | * } |
36 | 38 | */ |
37 | 39 | class Column extends AbstractNamedObject |
@@ -60,12 +62,14 @@ class Column extends AbstractNamedObject |
60 | 62 | /** @var PlatformOptions */ |
61 | 63 | protected array $_platformOptions = []; |
62 | 64 |
|
| 65 | + /** @var ?non-empty-string */ |
63 | 66 | protected ?string $_columnDefinition = null; |
64 | 67 |
|
65 | 68 | protected string $_comment = ''; |
66 | 69 |
|
67 | 70 | /** |
68 | | - * Creates a new Column. |
| 71 | + * @internal Use {@link Column::editor()} to instantiate an editor and {@link ColumnEditor::create()} to create a |
| 72 | + * column. |
69 | 73 | * |
70 | 74 | * @param array<string, mixed> $options |
71 | 75 | */ |
@@ -170,6 +174,7 @@ public function setPlatformOption(string $name, mixed $value): self |
170 | 174 | return $this; |
171 | 175 | } |
172 | 176 |
|
| 177 | + /** @param ?non-empty-string $value */ |
173 | 178 | public function setColumnDefinition(?string $value): self |
174 | 179 | { |
175 | 180 | $this->_columnDefinition = $value; |
@@ -217,19 +222,82 @@ public function getDefault(): mixed |
217 | 222 | return $this->_default; |
218 | 223 | } |
219 | 224 |
|
220 | | - /** @return PlatformOptions */ |
| 225 | + /** |
| 226 | + * Returns the name of the character set to use with the column. |
| 227 | + * |
| 228 | + * @return ?non-empty-string |
| 229 | + */ |
| 230 | + public function getCharset(): ?string |
| 231 | + { |
| 232 | + return $this->_platformOptions['charset'] ?? null; |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Returns the name of the collation to use with the column. |
| 237 | + * |
| 238 | + * @return ?non-empty-string |
| 239 | + */ |
| 240 | + public function getCollation(): ?string |
| 241 | + { |
| 242 | + return $this->_platformOptions['collation'] ?? null; |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Returns the minimum value to enforce on the column. |
| 247 | + */ |
| 248 | + public function getMinimumValue(): mixed |
| 249 | + { |
| 250 | + return $this->_platformOptions['min'] ?? null; |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Returns the maximum value to enforce on the column. |
| 255 | + */ |
| 256 | + public function getMaximumValue(): mixed |
| 257 | + { |
| 258 | + return $this->_platformOptions['max'] ?? null; |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * @internal Should be used only from within the {@see AbstractSchemaManager} class hierarchy. |
| 263 | + * |
| 264 | + * Returns the name of the DEFAULT constraint that implements the default value for the column on SQL Server. |
| 265 | + * |
| 266 | + * @return ?non-empty-string |
| 267 | + */ |
| 268 | + public function getDefaultConstraintName(): ?string |
| 269 | + { |
| 270 | + return $this->_platformOptions[SQLServerPlatform::OPTION_DEFAULT_CONSTRAINT_NAME] ?? null; |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} |
| 275 | + * instead. |
| 276 | + * |
| 277 | + * @return PlatformOptions |
| 278 | + */ |
221 | 279 | public function getPlatformOptions(): array |
222 | 280 | { |
223 | 281 | return $this->_platformOptions; |
224 | 282 | } |
225 | 283 |
|
226 | | - /** @param key-of<PlatformOptions> $name */ |
| 284 | + /** |
| 285 | + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} |
| 286 | + * instead. |
| 287 | + * |
| 288 | + * @param key-of<PlatformOptions> $name |
| 289 | + */ |
227 | 290 | public function hasPlatformOption(string $name): bool |
228 | 291 | { |
229 | 292 | return isset($this->_platformOptions[$name]); |
230 | 293 | } |
231 | 294 |
|
232 | | - /** @param key-of<PlatformOptions> $name */ |
| 295 | + /** |
| 296 | + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} |
| 297 | + * instead. |
| 298 | + * |
| 299 | + * @param key-of<PlatformOptions> $name |
| 300 | + */ |
233 | 301 | public function getPlatformOption(string $name): mixed |
234 | 302 | { |
235 | 303 | /** @phpstan-ignore offsetAccess.notFound */ |
@@ -302,4 +370,32 @@ public function toArray(): array |
302 | 370 | 'values' => $this->_values, |
303 | 371 | ], $this->_platformOptions); |
304 | 372 | } |
| 373 | + |
| 374 | + public static function editor(): ColumnEditor |
| 375 | + { |
| 376 | + return new ColumnEditor(); |
| 377 | + } |
| 378 | + |
| 379 | + public function edit(): ColumnEditor |
| 380 | + { |
| 381 | + return self::editor() |
| 382 | + ->setName($this->getObjectName()) |
| 383 | + ->setType($this->_type) |
| 384 | + ->setLength($this->_length) |
| 385 | + ->setPrecision($this->_precision) |
| 386 | + ->setScale($this->_scale) |
| 387 | + ->setUnsigned($this->_unsigned) |
| 388 | + ->setFixed($this->_fixed) |
| 389 | + ->setNotNull($this->_notnull) |
| 390 | + ->setDefaultValue($this->_default) |
| 391 | + ->setAutoincrement($this->_autoincrement) |
| 392 | + ->setComment($this->_comment) |
| 393 | + ->setValues($this->_values) |
| 394 | + ->setColumnDefinition($this->_columnDefinition) |
| 395 | + ->setCharset($this->getCharset()) |
| 396 | + ->setCollation($this->getCollation()) |
| 397 | + ->setMinimumValue($this->getMinimumValue()) |
| 398 | + ->setMaximumValue($this->getMaximumValue()) |
| 399 | + ->setDefaultConstraintName($this->getDefaultConstraintName()); |
| 400 | + } |
305 | 401 | } |
0 commit comments