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

BigIntColumn   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dbTypecast() 0 13 3
A __construct() 0 5 1
A phpTypecast() 0 6 2
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