Passed
Pull Request — master (#752)
by Sergei
03:20 queued 01:02
created

BinaryColumnSchema::dbTypecast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
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\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 BinaryColumnSchema extends AbstractColumnSchema
18
{
19
    public function __construct(string $name)
20
    {
21
        parent::__construct($name);
22
23
        $this->type(SchemaInterface::TYPE_BINARY);
24
        $this->phpType(SchemaInterface::PHP_TYPE_RESOURCE);
25
    }
26
27
    public function dbTypecast(mixed $value): mixed
28
    {
29
        return match (true) {
30
            is_string($value) => new Param($value, PDO::PARAM_LOB),
31
            $value === null, is_resource($value), $value instanceof ExpressionInterface => $value,
32
            /** ensure type cast always has . as decimal separator in all locales */
33
            is_float($value) => DbStringHelper::normalizeFloat($value),
34
            $value === false => '0',
35
            default => (string) $value,
36
        };
37
    }
38
}
39