Passed
Branch master (66090f)
by Wilmer
38:34 queued 29:57
created

ColumnSchemaBuilder::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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