Passed
Pull Request — master (#75)
by Wilmer
09:05 queued 07:19
created

ColumnSchemaBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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