Passed
Pull Request — master (#808)
by Sergei
02:31
created

BigIntColumn::dbTypecast()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 9.9666
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 const PHP_INT_MAX;
11
use const PHP_INT_MIN;
12
13
class BigIntColumn extends Column
14
{
15
    public function __construct(
16
        string|null $type = SchemaInterface::TYPE_BIGINT,
17
        string|null $phpType = SchemaInterface::PHP_TYPE_INTEGER,
18
    ) {
19
        parent::__construct($type, $phpType);
20
    }
21
22
    public function dbTypecast(mixed $value): int|string|ExpressionInterface|null
23
    {
24
        if ($value instanceof ExpressionInterface) {
25
            return $value;
26
        }
27
28
        return match ($value) {
29
            null, '' => null,
30
            false => 0,
31
            true => 1,
32
            default => match (true) {
33
                PHP_INT_MIN <= $value && $value <= PHP_INT_MAX => (int) $value,
34
                default => (string) $value,
35
            },
36
        };
37
    }
38
39
    public function phpTypecast(mixed $value): int|string|null
40
    {
41
        return match (true) {
42
            $value === null => null,
43
            PHP_INT_MIN <= $value && $value <= PHP_INT_MAX => (int) $value,
44
            default => (string) $value,
45
        };
46
    }
47
}
48