Skip to content

Commit

Permalink
Add bit abstract type (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Aug 8, 2024
1 parent 439ed62 commit bedc095
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Chg #847: Remove `SchemaInterface::getRawTableName()` and `AbstractSchema::getRawTableName()` methods (@Tigrov)
- Enh #852: Add method chaining for column classes (@Tigrov)
- Enh #855: Add array and JSON overlaps conditions (@Tigrov)
- Enh #860 Add `bit` abstract type (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
6 changes: 6 additions & 0 deletions src/Schema/AbstractSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\Column\BinaryColumnSchema;
use Yiisoft\Db\Schema\Column\BitColumnSchema;
use Yiisoft\Db\Schema\Column\BooleanColumnSchema;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\Column\DoubleColumnSchema;
Expand Down Expand Up @@ -413,6 +414,7 @@ protected function getColumnPhpType(string $type, bool $isUnsigned = false): str
SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE !== 8 || $isUnsigned
? SchemaInterface::PHP_TYPE_STRING
: SchemaInterface::PHP_TYPE_INTEGER,
SchemaInterface::TYPE_BIT => SchemaInterface::PHP_TYPE_INTEGER,
SchemaInterface::TYPE_BOOLEAN => SchemaInterface::PHP_TYPE_BOOLEAN,
SchemaInterface::TYPE_DECIMAL => SchemaInterface::PHP_TYPE_DOUBLE,
SchemaInterface::TYPE_FLOAT => SchemaInterface::PHP_TYPE_DOUBLE,
Expand All @@ -425,6 +427,10 @@ protected function getColumnPhpType(string $type, bool $isUnsigned = false): str

protected function createColumnSchemaFromPhpType(string $phpType, string $type): ColumnSchemaInterface
{
if ($type === SchemaInterface::TYPE_BIT) {
return new BitColumnSchema($type, $phpType);
}

return match ($phpType) {
SchemaInterface::PHP_TYPE_STRING => match ($type) {
SchemaInterface::TYPE_INTEGER => new BigIntColumnSchema($type, $phpType),
Expand Down
39 changes: 39 additions & 0 deletions src/Schema/Column/BitColumnSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Schema\Column;

use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Schema\SchemaInterface;

class BitColumnSchema extends AbstractColumnSchema
{
public function __construct(
string $type = SchemaInterface::TYPE_BIT,
string|null $phpType = SchemaInterface::PHP_TYPE_INTEGER,
) {
parent::__construct($type, $phpType);
}

public function dbTypecast(mixed $value): int|string|ExpressionInterface|null
{
if (is_int($value)) {
return $value;
}

return match ($value) {
null, '' => null,
default => $value instanceof ExpressionInterface ? $value : (int) $value,
};
}

public function phpTypecast(mixed $value): int|null
{
if ($value === null) {
return null;
}

return (int) $value;
}
}
4 changes: 4 additions & 0 deletions src/Schema/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ interface SchemaInterface extends ConstraintSchemaInterface
* Define the abstract column type as `boolean`.
*/
public const TYPE_BOOLEAN = 'boolean';
/**
* Define the abstract column type as `bit`.
*/
public const TYPE_BIT = 'bit';
/**
* Define the abstract column type as `money`.
*/
Expand Down
27 changes: 27 additions & 0 deletions tests/Provider/ColumnSchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Schema\Column\BigIntColumnSchema;
use Yiisoft\Db\Schema\Column\BinaryColumnSchema;
use Yiisoft\Db\Schema\Column\BitColumnSchema;
use Yiisoft\Db\Schema\Column\BooleanColumnSchema;
use Yiisoft\Db\Schema\Column\DoubleColumnSchema;
use Yiisoft\Db\Schema\Column\IntegerColumnSchema;
Expand All @@ -31,6 +32,7 @@ public static function predefinedTypes(): array
'double' => [DoubleColumnSchema::class, SchemaInterface::TYPE_DOUBLE, SchemaInterface::PHP_TYPE_DOUBLE],
'string' => [StringColumnSchema::class, SchemaInterface::TYPE_STRING, SchemaInterface::PHP_TYPE_STRING],
'binary' => [BinaryColumnSchema::class, SchemaInterface::TYPE_BINARY, SchemaInterface::PHP_TYPE_RESOURCE],
'bit' => [BitColumnSchema::class, SchemaInterface::TYPE_BIT, SchemaInterface::PHP_TYPE_INTEGER],
'boolean' => [BooleanColumnSchema::class, SchemaInterface::TYPE_BOOLEAN, SchemaInterface::PHP_TYPE_BOOLEAN],
'json' => [JsonColumnSchema::class, SchemaInterface::TYPE_JSON, SchemaInterface::PHP_TYPE_ARRAY],
];
Expand Down Expand Up @@ -105,6 +107,21 @@ public static function dbTypecastColumns(): array
[$expression = new Expression('expression'), $expression],
],
],
'bit' => [
BitColumnSchema::class,
[
[null, null],
[null, ''],
[1, 1],
[1, 1.0],
[1, '1'],
[10, 10],
[10, '10'],
[1, true],
[0, false],
[$expression = new Expression('expression'), $expression],
],
],
'boolean' => [
BooleanColumnSchema::class,
[
Expand Down Expand Up @@ -187,6 +204,16 @@ public static function phpTypecastColumns(): array
[$resource = fopen('php://memory', 'rb'), $resource],
],
],
'bit' => [
BitColumnSchema::class,
[
[null, null],
[1, 1],
[1, '1'],
[10, 10],
[10, '10'],
],
],
'boolean' => [
BooleanColumnSchema::class,
[
Expand Down

0 comments on commit bedc095

Please sign in to comment.