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

BinaryColumn   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 18
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dbTypecast() 0 9 1
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\Helper\DbStringHelper;
11
use Yiisoft\Db\Schema\SchemaInterface;
12
13
use function is_float;
14
use function is_resource;
15
use function is_string;
16
17
class BinaryColumn extends Column
18
{
19
    public function __construct(
20
        string|null $type = SchemaInterface::TYPE_BINARY,
21
        string|null $phpType = SchemaInterface::PHP_TYPE_RESOURCE,
22
    ) {
23
        parent::__construct($type, $phpType);
24
    }
25
26
    public function dbTypecast(mixed $value): mixed
27
    {
28
        return match (true) {
29
            is_string($value) => new Param($value, PDO::PARAM_LOB),
30
            $value === null, is_resource($value), $value instanceof ExpressionInterface => $value,
31
            /** ensure type cast always has . as decimal separator in all locales */
32
            is_float($value) => DbStringHelper::normalizeFloat($value),
33
            $value === false => '0',
34
            default => (string) $value,
35
        };
36
    }
37
}
38