Test Failed
Pull Request — master (#205)
by Alexander
07:13 queued 03:37
created

ColumnSchemaBuilder::isJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use Exception;
8
use Yiisoft\Db\Exception\InvalidConfigException;
9
use Yiisoft\Db\Schema\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;
10
use Yiisoft\Db\Schema\QuoterInterface;
11
12
/**
13
 * The class ColumnSchemaBuilder for Mysql database.
14
 */
15
final class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder implements \Stringable
16
{
17
    /**
18
     *  @psalm-param string[]|int[]|int|string|null $length column size or precision definition.
19
     */
20 10
    public function __construct(string $type, array|int|string|null $length, private QuoterInterface $quoter)
21
    {
22 10
        parent::__construct($type, $length);
23
24
        if ($this->isJson()) {
25
            $this->check('[[{name}]] is null or json_valid([[{name}]])');
26
        }
27
    }
28
29
    /**
30 8
     * Builds the unsigned string for column. Defaults to unsupported.
31
     *
32 8
     * @return string a string containing UNSIGNED keyword.
33
     */
34
    protected function buildUnsignedString(): string
35
    {
36
        return $this->isUnsigned() ? ' UNSIGNED' : '';
37
    }
38
39
    /**
40
     * Builds the comment specification for the column.
41
     *
42 8
     * @throws Exception|InvalidConfigException
43
     *
44 8
     * @return string a string containing the COMMENT keyword and the comment itself.
45 8
     */
46
    protected function buildCommentString(): string
47
    {
48 8
        return $this->getComment() !== null ? ' COMMENT '
49
            . (string) $this->quoter->quoteValue($this->getComment()) : '';
50 8
    }
51 8
52 8
    public function __toString(): string
53 8
    {
54 8
        $format = match ($this->getTypeCategory()) {
55
            self::CATEGORY_PK => '{type}{length}{comment}{check}{append}',
56 8
            self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{check}',
57
            default => '{type}{length}{notnull}{default}{unique}{comment}{append}{check}',
58
        };
59
60
        return $this->buildCompleteString($format);
61
    }
62
63
    public function isJson(): bool
64
    {
65
        return $this->getType() === Schema::TYPE_JSON;
66
    }
67
}
68