Skip to content

Commit

Permalink
Change ColumnFactory to AbstractColumnFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Aug 27, 2024
1 parent 7d6fa97 commit d17464d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};
}
}
16 changes: 16 additions & 0 deletions tests/Support/Stub/ColumnFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests\Support\Stub;

use Yiisoft\Db\Schema\Column\AbstractColumnFactory;

class ColumnFactory extends AbstractColumnFactory
{

protected function getType(string $dbType, array $info = []): string
{
return $dbType;
}
}
1 change: 0 additions & 1 deletion tests/Support/Stub/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\AbstractSchema;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnFactory;
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;

Expand Down

0 comments on commit d17464d

Please sign in to comment.