Passed
Push — master ( 05c121...11a82d )
by Def
09:00 queued 05:15
created

Column::asString()   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\Schema\Builder\AbstractColumn;
9
use Yiisoft\Db\Schema\QuoterInterface;
10
11
/**
12
 * It's a utility that provides a convenient way to create column schema for use with {@see `\Yiisoft\Db\Mysql\Schema`}
13
 * for MySQL, MariaDb Server.
14
 *
15
 * It provides methods for specifying the properties of a column, such as its type, size, default value, and whether it
16
 * is nullable or not. It also provides a method for creating a column schema based on the specified properties.
17
 *
18
 * For example, the following code creates a column schema for an integer column:
19
 *
20
 * ```php
21
 * $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0);
22
 * ```
23
 *
24
 * Provides a fluent interface, which means that the methods can be chained together to create a column schema with
25
 * many properties in a single line of code.
26
 */
27
final class Column extends AbstractColumn
28
{
29
    private QuoterInterface|null $quoter = null;
0 ignored issues
show
introduced by
The private property $quoter is not used, and could be removed.
Loading history...
30
31
    /**
32
     * Builds the unsigned string for column. Defaults to unsupported.
33
     *
34
     * @return string a string containing UNSIGNED keyword.
35
     */
36 9
    protected function buildUnsignedString(): string
37
    {
38 9
        return $this->isUnsigned() ? ' UNSIGNED' : '';
39
    }
40
41
    /**
42
     * Builds the comment specification for the column.
43
     *
44
     * @throws Exception
45
     *
46
     * @return string a string containing the COMMENT keyword and the comment itself.
47
     */
48 9
    protected function buildCommentString(): string
49
    {
50 9
        if ($this->getComment() === null) {
51 7
            return '';
52
        }
53
54 2
        return ' COMMENT ' . (string) (new Quoter('`', '`'))->quoteValue($this->getComment());
55
    }
56
57 9
    public function asString(): string
58
    {
59 9
        $format = match ($this->getTypeCategory()) {
60 9
            self::CATEGORY_PK => '{type}{length}{comment}{check}{append}',
61 9
            self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{check}',
62 9
            default => '{type}{length}{notnull}{default}{unique}{comment}{append}{check}',
63 9
        };
64
65 9
        return $this->buildCompleteString($format);
66
    }
67
}
68