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

BinaryColumn::dbTypecast()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema\Column;
6
7
use PDO;
8
use Yiisoft\Db\Command\Param;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
use Yiisoft\Db\Schema\SchemaInterface;
11
12
use function gettype;
13
14
class BinaryColumn extends Column
15
{
16
    public function __construct(
17
        string|null $type = SchemaInterface::TYPE_BINARY,
18
        string|null $phpType = SchemaInterface::PHP_TYPE_RESOURCE,
19
    ) {
20
        parent::__construct($type, $phpType);
21
    }
22
23
    public function dbTypecast(mixed $value): mixed
24
    {
25
        if ($value instanceof ExpressionInterface) {
26
            return $value;
27
        }
28
29
        return match (gettype($value)) {
30
            'string' => new Param($value, PDO::PARAM_LOB),
31
            'resource' => $value,
32
            'NULL' => null,
33
            'boolean' => $value ? '1' : '0',
34
            default => (string) $value,
35
        };
36
    }
37
}
38