Passed
Push — master ( c999e9...be06f7 )
by Rustam
02:25
created

TableSchema::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Schema;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
9
/**
10
 * TableSchema represents the metadata of a database table.
11
 *
12
 * @property array $columnNames List of column names. This property is read-only.
13
 */
14
abstract class TableSchema
15
{
16
    private ?string $schemaName = null;
17
    private ?string $name = null;
18
    private ?string $fullName = null;
19
    private ?string $sequenceName = null;
20
    private array $primaryKey = [];
21
    private array $columns = [];
22
23
    /**
24
     * Gets the named column metadata.
25
     *
26
     * This is a convenient method for retrieving a named column even if it does not exist.
27
     *
28
     * @param string $name column name
29
     *
30
     * @return ColumnSchema metadata of the named column. Null if the named column does not exist.
31
     */
32 5
    public function getColumn(?string $name): ColumnSchema
33
    {
34 5
        return $this->columns[$name] ?? null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->columns[$name] ?? null could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\ColumnSchema. Consider adding an additional type-check to rule them out.
Loading history...
35
    }
36
37
    /**
38
     * Returns the names of all columns in this table.
39
     *
40
     * @return array list of column names
41
     */
42 1
    public function getColumnNames()
43
    {
44 1
        return \array_keys($this->columns);
45
    }
46
47
    /**
48
     * Manually specifies the primary key for this table.
49
     *
50
     * @param string|array $keys the primary key (can be composite)
51
     *
52
     * @throws InvalidArgumentException if the specified key cannot be found in the table.
53
     */
54
    public function fixPrimaryKey($keys): void
55
    {
56
        $keys = (array) $keys;
57
        $this->primaryKey = $keys;
58
59
        foreach ($this->columns as $column) {
60
            $column->primaryKey(false);
61
        }
62
63
        foreach ($keys as $key) {
64
            if (isset($this->columns[$key])) {
65
                $this->columns[$key]->primaryKey(true);
66
            } else {
67
                throw new InvalidArgumentException("Primary key '$key' cannot be found in table '{$this->name}'.");
68
            }
69
        }
70
    }
71
72
    /**
73
     * @return string|null the name of the schema that this table belongs to.
74
     */
75 147
    public function getSchemaName(): ?string
76
    {
77 147
        return $this->schemaName;
78
    }
79
80
    /**
81
     * @return string|null the name of this table. The schema name is not included. Use {@see fullName} to get the name
82
     * with schema name prefix.
83
     */
84 147
    public function getName(): ?string
85
    {
86 147
        return $this->name;
87
    }
88
89
    /**
90
     * @return string|null the full name of this table, which includes the schema name prefix, if any. Note that if the
91
     * schema name is the same as the {@see Schema::defaultSchema|default schema name}, the schema name will not be
92
     * included.
93
     */
94 5
    public function getFullName(): ?string
95
    {
96 5
        return $this->fullName;
97
    }
98
99
    /**
100
     * @return string|null sequence name for the primary key. Null if no sequence.
101
     */
102 64
    public function getSequenceName(): ?string
103
    {
104 64
        return $this->sequenceName;
105
    }
106
107
    /**
108
     * @return array primary keys of this table.
109
     */
110 2
    public function getPrimaryKey(): array
111
    {
112 2
        return $this->primaryKey;
113
    }
114
115
    /**
116
     * @return ColumnSchema[] column metadata of this table. Each array element is a {@see ColumnSchema} object, indexed
117
     * by column names.
118
     */
119 65
    public function getColumns(): array
120
    {
121 65
        return $this->columns;
122
    }
123
124 147
    public function schemaName(?string $value): void
125
    {
126 147
        $this->schemaName = $value;
127 147
    }
128
129 147
    public function name(?string $value): void
130
    {
131 147
        $this->name = $value;
132 147
    }
133
134 147
    public function fullName(?string $value): void
135
    {
136 147
        $this->fullName = $value;
137 147
    }
138
139 64
    public function sequenceName(?string $value): void
140
    {
141 64
        $this->sequenceName = $value;
142 64
    }
143
144 64
    public function primaryKey(string $value): void
145
    {
146 64
        $this->primaryKey[] = $value;
147 64
    }
148
149 93
    public function columns(string $index, ColumnSchema $value): void
150
    {
151 93
        $this->columns[$index] = $value;
152 93
    }
153
}
154