Passed
Push — master ( 73f94a...8a7a3b )
by Def
39:03 queued 26:13
created

ColumnSchemaBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 46
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setQuoter() 0 4 1
A buildUnsignedString() 0 3 2
A asString() 0 9 1
A buildCommentString() 0 8 3
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
    private QuoterInterface|null $quoter = null;
18
19
    /**
20
     * Builds the unsigned string for column. Defaults to unsupported.
21
     *
22
     * @return string a string containing UNSIGNED keyword.
23
     */
24 11
    protected function buildUnsignedString(): string
25
    {
26 11
        return $this->isUnsigned() ? ' UNSIGNED' : '';
27
    }
28
29
    /**
30
     * Builds the comment specification for the column.
31
     *
32
     * @throws Exception|InvalidConfigException
33
     *
34
     * @return string a string containing the COMMENT keyword and the comment itself.
35
     */
36 11
    protected function buildCommentString(): string
37
    {
38 11
        if ($this->quoter === null) {
39 1
            throw new InvalidConfigException('Quoter not setted.');
40
        }
41
42 10
        return $this->getComment() !== null ? ' COMMENT '
43 10
            . (string) $this->quoter->quoteValue($this->getComment()) : '';
44
    }
45
46 11
    public function asString(): string
47
    {
48 11
        $format = match ($this->getTypeCategory()) {
49 11
            self::CATEGORY_PK => '{type}{length}{comment}{check}{append}',
50 11
            self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{check}',
51 11
            default => '{type}{length}{notnull}{default}{unique}{comment}{append}{check}',
52 11
        };
53
54 11
        return $this->buildCompleteString($format);
55
    }
56
57 148
    public function setQuoter(QuoterInterface $quoter): self
58
    {
59 148
        $this->quoter = $quoter;
60 148
        return $this;
61
    }
62
}
63