Passed
Push — 3.x ( 47c35b...0fca64 )
by Aleksei
11:45
created

Column::getDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Annotated\Annotation;
6
7
use Cycle\ORM\Parser\Typecast;
8
use Doctrine\Common\Annotations\Annotation\Target;
9
use JetBrains\PhpStorm\ExpectedValues;
10
use Spiral\Attributes\NamedArgumentConstructor;
11
12
/**
13
 * @Annotation
14
 *
15
 * @NamedArgumentConstructor
16
 *
17
 * @Target({"PROPERTY", "ANNOTATION", "CLASS"})
18
 */
19
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
20
#[NamedArgumentConstructor]
21
final class Column
22
{
23
    private bool $hasDefault = false;
24
    /**
25
     * @var array<non-empty-string, mixed> Other database specific attributes.
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
26
     */
27
    private array $attributes;
28
29
    /**
30
     * @param non-empty-string $type Column type. {@see \Cycle\Database\Schema\AbstractColumn::$mapping}
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
31
     *        Column types `smallPrimary`, `timetz`, `timestamptz`, `interval`, `bitVarying`, `int4range`, `int8range`,
32
     *        `numrange`, `tsrange`, `tstzrange`, `daterange`, `jsonb`, `point`, `line`, `lseg`, `box`, `path`,
33
     *        `polygon`, `circle`, `cidr`, `inet`, `macaddr`, `macaddr8`, `tsvector`, `tsquery` are related
34
     *         to the PostgreSQL only {@see \Cycle\Database\Driver\Postgres\Schema\PostgresColumn::$mapping}
35
     *        Column type `datetime2` is related to the SQL Server only
36
     *        {@see \Cycle\Database\Driver\SQLServer\Schema\SQLServerColumn::$mapping}
37
     * @param non-empty-string|null $name Column name. Defaults to the property name.
38
     * @param non-empty-string|null $property Property that belongs to column. For virtual columns.
39 1032
     * @param bool $primary Explicitly set column as a primary key.
40
     * @param bool $nullable Set column as nullable.
41
     * @param mixed|null $default Default column value.
42
     * @param callable|non-empty-string|null $typecast Typecast rule name.
43
     *        Regarding the default Typecast handler {@see Typecast} the value can be `callable` or
44
     *        one of ("int"|"float"|"bool"|"datetime") based on column type.
45
     *        If you want to use another rule you should add in the `typecast` argument of the {@see Entity} attribute
46
     *        a relevant Typecast handler that supports the rule.
47
     * @param bool $castDefault
48
     * @param bool $readonlySchema Set to true to disable schema synchronization for the assigned column.
49
     * @param mixed ...$attributes Other database specific attributes. Use named notation to define them.
50
     *        For example: #[Column('smallInt', unsigned: true, zerofill: true)]
51
     */
52
    public function __construct(
53 1032
        #[ExpectedValues(values: ['primary', 'bigPrimary', 'enum', 'boolean',
54 496
            'integer', 'tinyInteger', 'smallInteger', 'bigInteger', 'string', 'text', 'tinyText', 'longText', 'double',
55
            'float', 'decimal', 'datetime', 'date', 'time', 'timestamp', 'binary', 'tinyBinary', 'longBinary', 'json',
56 1032
            'uuid', 'bit',
57
            // PostgreSQL
58 1032
            'smallPrimary', 'timetz', 'timestamptz', 'interval', 'bitVarying', 'int4range', 'int8range', 'numrange',
59
            'tsrange', 'tstzrange', 'daterange', 'jsonb', 'point', 'line', 'lseg', 'box', 'path', 'polygon', 'circle',
60 1032
            'cidr', 'inet', 'macaddr', 'macaddr8', 'tsvector', 'tsquery',
61
            // SQL Server
62
            'datetime2',
63 1032
        ])]
64
        private string $type,
65 1032
        private ?string $name = null,
66
        private ?string $property = null,
67
        private bool $primary = false,
68 472
        private bool $nullable = false,
69
        private mixed $default = null,
70 472
        private mixed $typecast = null,
71
        private bool $castDefault = false,
72
        private bool $readonlySchema = false,
73 1032
        mixed ...$attributes,
74
    ) {
75 1032
        if ($default !== null) {
76
            $this->hasDefault = true;
77
        }
78 1032
        $this->attributes = $attributes;
79
    }
80 1032
81
    public function getType(): ?string
82
    {
83 1032
        return $this->type;
84
    }
85 1032
86
    public function getColumn(): ?string
87
    {
88 496
        return $this->name;
89
    }
90 496
91
    public function getProperty(): ?string
92
    {
93 1032
        return $this->property;
94
    }
95 1032
96
    public function isNullable(): bool
97
    {
98 1032
        return $this->nullable;
99
    }
100 1032
101
    public function isPrimary(): bool
102
    {
103
        return $this->primary;
104
    }
105
106
    public function hasDefault(): bool
107
    {
108
        return $this->hasDefault;
109
    }
110
111
    public function getDefault(): mixed
112
    {
113
        return $this->default;
114
    }
115
116
    public function castDefault(): bool
117
    {
118
        return $this->castDefault;
119
    }
120
121
    public function getTypecast(): mixed
122
    {
123
        return $this->typecast;
124
    }
125
126
    public function isReadonlySchema(): bool
127
    {
128
        return $this->readonlySchema;
129
    }
130
131
    /**
132
     * @return array<non-empty-string, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
133
     */
134
    public function getAttributes(): array
135
    {
136
        return $this->attributes;
137
    }
138
}
139