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

BooleanColumnSchema::phpTypecast()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
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
class BooleanColumnSchema extends AbstractColumnSchema
11
{
12
    public function __construct(string $name)
13
    {
14
        parent::__construct($name);
15
16
        $this->type(SchemaInterface::TYPE_BOOLEAN);
17
        $this->phpType(SchemaInterface::PHP_TYPE_BOOLEAN);
18
    }
19
20
    public function dbTypecast(mixed $value): bool|ExpressionInterface|null
21
    {
22
        /** Optimized for performance. Do not merge cases or change order. */
23
        return match (true) {
24
            $value, $value === false, $value === null, $value instanceof ExpressionInterface => $value,
25
            $value === '' => null,
26
            default => (bool) $value,
27
        };
28
    }
29
30
    public function phpTypecast(mixed $value): bool|null
31
    {
32
        if ($value === null) {
33
            return null;
34
        }
35
36
        return $value && $value !== "\0";
37
    }
38
}
39