Passed
Pull Request — master (#40)
by Wilmer
14:42
created

ColumnSchemaBuilder::__toString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 0
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use Yiisoft\Db\Exception\Exception;
8
use Yiisoft\Db\Exception\InvalidConfigException;
9
use Yiisoft\Db\Exception\NotSupportedException;
10
use Yiisoft\Db\Schema\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;
11
12
final class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
13
{
14
    /**
15
     * Builds the unsigned string for column. Defaults to unsupported.
16
     *
17
     * @return string a string containing UNSIGNED keyword.
18
     */
19
    protected function buildUnsignedString(): string
20
    {
21
        return $this->isUnsigned() ? ' UNSIGNED' : '';
22
    }
23
24
    /**
25
     * Builds the after constraint for the column. Defaults to unsupported.
26
     *
27
     * @throws Exception
28
     * @throws InvalidConfigException
29
     * @throws NotSupportedException
30
     *
31
     * @return string a string containing the AFTER constraint.
32
     */
33
    protected function buildAfterString(): string
34
    {
35
        return $this->getAfter() !== null ? ' AFTER ' . $this->getDb()->quoteColumnName($this->getAfter()) : '';
36
    }
37
38
    /**
39
     * Builds the first constraint for the column. Defaults to unsupported.
40
     *
41
     * @return string a string containing the FIRST constraint.
42
     */
43
    protected function buildFirstString(): string
44
    {
45
        return $this->isFirst() ? ' FIRST' : '';
46
    }
47
48
    /**
49
     * Builds the comment specification for the column.
50
     *
51
     * @throws Exception
52
     * @throws InvalidConfigException
53
     * @throws NotSupportedException
54
     *
55
     * @return string a string containing the COMMENT keyword and the comment itself.
56
     */
57
    protected function buildCommentString(): string
58
    {
59
        return $this->getComment() !== null ? ' COMMENT ' . $this->getDb()->quoteValue($this->getComment()) : '';
60
    }
61
62
    public function __toString(): string
63
    {
64
        switch ($this->getTypeCategory()) {
65
            case self::CATEGORY_PK:
66
                $format = '{type}{length}{comment}{check}{append}{pos}';
67
                break;
68
            case self::CATEGORY_NUMERIC:
69
                $format = '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{pos}{check}';
70
                break;
71
            default:
72
                $format = '{type}{length}{notnull}{default}{unique}{comment}{append}{pos}{check}';
73
        }
74
75
        return $this->buildCompleteString($format);
76
    }
77
}
78