Passed
Pull Request — master (#808)
by Sergei
15:21 queued 13:01
created

BigIntColumn::phpTypecast()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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