Completed
Push — master ( 6fb5fb...b26681 )
by David
11s
created

FluidTable::quoteArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
4
namespace TheCodingMachine\FluidSchema;
5
6
use Doctrine\DBAL\Schema\Table;
7
8
class FluidTable
9
{
10
    /**
11
     * @var FluidSchema
12
     */
13
    private $schema;
14
    /**
15
     * @var Table
16
     */
17
    private $table;
18
    /**
19
     * @var FluidColumn[]
20
     */
21
    private $fluidColumns;
22
    /**
23
     * @var NamingStrategyInterface
24
     */
25
    private $namingStrategy;
26
27
    /**
28
     * @param FluidSchema $schema
29
     * @param Table $table
30
     */
31
    public function __construct(FluidSchema $schema, Table $table, NamingStrategyInterface $namingStrategy)
32
    {
33
        $this->schema = $schema;
34
        $this->table = $table;
35
        $this->namingStrategy = $namingStrategy;
36
    }
37
38
    public function column(string $name): FluidColumn
39
    {
40
        $name = $this->namingStrategy->quoteIdentifier($name);
41
42
        if (isset($this->fluidColumns[$name])) {
43
            return $this->fluidColumns[$name];
44
        }
45
46
        if ($this->table->hasColumn($name)) {
47
            $column = $this->table->getColumn($name);
48
        } else {
49
            $column = $this->table->addColumn($name, 'string');
50
        }
51
52
        $this->fluidColumns[$name] = new FluidColumn($this->schema, $this, $this->table, $column, $this->namingStrategy);
53
        return $this->fluidColumns[$name];
54
    }
55
56
    public function index(array $columnNames): FluidTable
57
    {
58
        $this->table->addIndex($this->quoteArray($columnNames));
59
        return $this;
60
    }
61
62
    public function unique(array $columnNames): FluidTable
63
    {
64
        $this->table->addUniqueIndex($this->quoteArray($columnNames));
65
        return $this;
66
    }
67
68
    public function primaryKey(array $columnNames, ?string $indexName = null): FluidTable
69
    {
70
        $newIndexName = $indexName ?: false;
71
72
        $this->table->setPrimaryKey($this->quoteArray($columnNames), $newIndexName);
73
        return $this;
74
    }
75
76
    private function quoteArray(array $columnNames): array
77
    {
78
        return array_map([$this->namingStrategy, 'quoteIdentifier'], $columnNames);
79
    }
80
81
    /**
82
     * Creates a "id" autoincremented primary key column.
83
     *
84
     * @return FluidTable
85
     */
86
    public function id(): FluidTable
87
    {
88
        $this->column('id')->integer()->primaryKey()->autoIncrement();
89
        return $this;
90
    }
91
92
    /**
93
     * Creates a "uuid" primary key column.
94
     *
95
     * @return FluidTable
96
     */
97
    public function uuid(): FluidTable
98
    {
99
        $this->column('uuid')->guid()->primaryKey();
100
        return $this;
101
    }
102
103
    /**
104
     * Creates "created_at" and "updated_at" columns.
105
     *
106
     * @return FluidTable
107
     */
108
    public function timestamps(): FluidTable
109
    {
110
        $this->column('created_at')->datetimeImmutable();
111
        $this->column('updated_at')->datetimeImmutable();
112
        return $this;
113
    }
114
115
    public function extends(string $tableName): FluidTable
116
    {
117
        $tableName = $this->namingStrategy->quoteIdentifier($tableName);
118
119
        $inheritedTable = $this->schema->getDbalSchema()->getTable($tableName);
120
121
        $pks = $inheritedTable->getPrimaryKeyColumns();
122
123
        if (count($pks) > 1) {
124
            throw new FluidSchemaException('You cannot inherit from a table with a primary key on several columns using FluidSchema. Use DBAL Schema methods instead.');
125
        }
126
127
        $pkName = $pks[0];
128
        $pk = $inheritedTable->getColumn($pkName);
129
130
        $this->column($pk->getName())->references($tableName)->primaryKey();
131
        return $this;
132
    }
133
}
134