From d17464d102aec688a0edd6771b9013730e7d8945 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 27 Aug 2024 18:56:21 +0700 Subject: [PATCH] Change `ColumnFactory` to `AbstractColumnFactory` --- CHANGELOG.md | 1 + UPGRADE.md | 6 +- ...nFactory.php => AbstractColumnFactory.php} | 55 +++++-------------- tests/Support/Stub/ColumnFactory.php | 16 ++++++ tests/Support/Stub/Schema.php | 1 - 5 files changed, 34 insertions(+), 45 deletions(-) rename src/Schema/Column/{ColumnFactory.php => AbstractColumnFactory.php} (75%) create mode 100644 tests/Support/Stub/ColumnFactory.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 550d21e56..be112319f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ - Enh #862: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov) - Enh #865: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov, @vjik) - Enh #798: Allow `QueryInterface::one()` and `QueryInterface::all()` to return objects (@darkdef, @Tigrov) +- Enh #864: Realize column factory (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/UPGRADE.md b/UPGRADE.md index bbce1e55a..5592abbf3 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -67,6 +67,7 @@ and the following changes were made: - `getName()` method can return `string` or `null`; - `getPhpType()` method must return `string` PHP type of the column which used for generating related model properties; - `name(string|null $name)` method is added; +- `load(array $info)` method is added; - constructor of `AbstractColumnSchema` class is changed to `__construct(string $type, string|null $phpType = null)`; - added method chaining. @@ -88,9 +89,10 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace - `BinaryColumnSchema` for columns with binary type; - `JsonColumnSchema` for columns with json type. -### New methods in `QuoterInterface` +### New methods -- `QuoterInterface::getRawTableName()` - returns the raw table name without quotes. +- `QuoterInterface::getRawTableName()` - returns the raw table name without quotes; +- `SchemaInterface::getColumnFactory()` - returns the column factory. ### Remove methods diff --git a/src/Schema/Column/ColumnFactory.php b/src/Schema/Column/AbstractColumnFactory.php similarity index 75% rename from src/Schema/Column/ColumnFactory.php rename to src/Schema/Column/AbstractColumnFactory.php index b6e7ff3be..53bb68745 100644 --- a/src/Schema/Column/ColumnFactory.php +++ b/src/Schema/Column/AbstractColumnFactory.php @@ -23,8 +23,20 @@ * @psalm-import-type ColumnInfo from ColumnSchemaInterface * @psalm-suppress MixedArgumentTypeCoercion */ -class ColumnFactory implements ColumnFactoryInterface +abstract class AbstractColumnFactory implements ColumnFactoryInterface { + /** + * Get the abstract database type for a database column type. + * + * @param string $dbType The database column type. + * @param array $info The column information. + * + * @return string The abstract database type. + * + * @psalm-param ColumnInfo $info + */ + abstract protected function getType(string $dbType, array $info = []): string; + public function fromDbType(string $dbType, array $info = []): ColumnSchemaInterface { $info['db_type'] = $dbType; @@ -93,45 +105,4 @@ public function fromType(string $type, array $info = []): ColumnSchemaInterface return $column->load($info); } - - /** - * Get the abstract database type for a database column type. - * - * @param string $dbType The database column type. - * @param array $info The column information. - * - * @return string The abstract database type. - * - * @psalm-param ColumnInfo $info - */ - protected function getType(string $dbType, array $info = []): string - { - return $this->isType($dbType) ? $dbType : SchemaInterface::TYPE_STRING; - } - - protected function isType(string $dbType): bool - { - return match ($dbType) { - SchemaInterface::TYPE_UUID, - SchemaInterface::TYPE_CHAR, - SchemaInterface::TYPE_STRING, - SchemaInterface::TYPE_TEXT, - SchemaInterface::TYPE_BINARY, - SchemaInterface::TYPE_BOOLEAN, - SchemaInterface::TYPE_TINYINT, - SchemaInterface::TYPE_SMALLINT, - SchemaInterface::TYPE_INTEGER, - SchemaInterface::TYPE_BIGINT, - SchemaInterface::TYPE_FLOAT, - SchemaInterface::TYPE_DOUBLE, - SchemaInterface::TYPE_DECIMAL, - SchemaInterface::TYPE_MONEY, - SchemaInterface::TYPE_DATETIME, - SchemaInterface::TYPE_TIMESTAMP, - SchemaInterface::TYPE_TIME, - SchemaInterface::TYPE_DATE, - SchemaInterface::TYPE_JSON => true, - default => false, - }; - } } diff --git a/tests/Support/Stub/ColumnFactory.php b/tests/Support/Stub/ColumnFactory.php new file mode 100644 index 000000000..df5636c26 --- /dev/null +++ b/tests/Support/Stub/ColumnFactory.php @@ -0,0 +1,16 @@ +