Passed
Pull Request — master (#752)
by Sergei
02:46
created

BigIntColumnSchema::dbTypecast()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema\Column;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
use Yiisoft\Db\Schema\SchemaInterface;
9
10
use function is_int;
11
12
class BigIntColumnSchema extends AbstractColumnSchema
13
{
14
    public function __construct(string $name)
15
    {
16
        parent::__construct($name);
17
18
        $this->type(SchemaInterface::TYPE_BIGINT);
19
        $this->phpType(SchemaInterface::PHP_TYPE_INTEGER);
20
    }
21
22
    public function dbTypecast(mixed $value): int|string|ExpressionInterface|null
23
    {
24
        return match (true) {
25
            is_int($value), $value === null, $value instanceof ExpressionInterface => $value,
26
            $value === '' => null,
27
            $value === false => 0,
28
            PHP_INT_MIN <= $value && $value <= PHP_INT_MAX => (int) $value,
29
            default => (string) $value,
30
        };
31
    }
32
33
    public function phpTypecast(mixed $value): int|string|null
34
    {
35
        /** @psalm-var int|string|null $value */
36
        return match (true) {
37
            $value === null => null,
38
            PHP_INT_MIN <= $value && $value <= PHP_INT_MAX => (int) $value,
39
            default => (string) $value,
40
        };
41
    }
42
}
43