• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thecodingmachine / dbal-fluid-schema-builder / 7686160655

28 Jan 2024 01:42PM UTC coverage: 100.0% (+0.6%) from 99.415%
7686160655

push

github

web-flow
Merge pull request #13 from aszenz/master

ci: Improve tests + fix ci deps

171 of 171 relevant lines covered (100.0%)

14.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/FluidTable.php
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;
60✔
34
        $this->table = $table;
60✔
35
        $this->namingStrategy = $namingStrategy;
60✔
36
    }
37

38
    public function column(string $name): FluidColumn
39
    {
40
        $name = $this->namingStrategy->quoteIdentifier($name);
54✔
41

42
        if (isset($this->fluidColumns[$name])) {
54✔
43
            return $this->fluidColumns[$name];
3✔
44
        }
45

46
        if ($this->table->hasColumn($name)) {
54✔
47
            $column = $this->table->getColumn($name);
3✔
48
        } else {
49
            $column = $this->table->addColumn($name, 'string');
51✔
50
        }
51

52
        $this->fluidColumns[$name] = new FluidColumn($this->schema, $this, $this->table, $column, $this->namingStrategy);
54✔
53
        return $this->fluidColumns[$name];
54✔
54
    }
55

56
    public function index(array $columnNames, ?string $indexName = null): FluidTable
57
    {
58
        $this->table->addIndex($this->quoteArray($columnNames), $indexName);
6✔
59
        return $this;
6✔
60
    }
61

62
    public function unique(array $columnNames, ?string $indexName = null): FluidTable
63
    {
64
        $this->table->addUniqueIndex($this->quoteArray($columnNames), $indexName);
3✔
65
        return $this;
3✔
66
    }
67

68
    public function primaryKey(array $columnNames, ?string $indexName = null): FluidTable
69
    {
70
        $newIndexName = $indexName ?: false;
27✔
71

72
        $this->table->setPrimaryKey($this->quoteArray($columnNames), $newIndexName);
27✔
73
        return $this;
27✔
74
    }
75

76
    private function quoteArray(array $columnNames): array
77
    {
78
        return array_map([$this->namingStrategy, 'quoteIdentifier'], $columnNames);
36✔
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();
12✔
89
        return $this;
12✔
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();
3✔
100
        return $this;
3✔
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();
3✔
111
        $this->column('updated_at')->datetimeImmutable();
3✔
112
        return $this;
3✔
113
    }
114

115
    public function extends(string $tableName): FluidTable
116
    {
117
        $tableName = $this->namingStrategy->quoteIdentifier($tableName);
6✔
118

119
        $inheritedTable = $this->schema->getDbalSchema()->getTable($tableName);
6✔
120

121
        $pks = $inheritedTable->getPrimaryKey()->getColumns();
6✔
122

123
        if (count($pks) > 1) {
6✔
124
            throw new FluidSchemaException('You cannot inherit from a table with a primary key on several columns using FluidSchema. Use DBAL Schema methods instead.');
3✔
125
        }
126

127
        $pkName = $pks[0];
3✔
128
        $pk = $inheritedTable->getColumn($pkName);
3✔
129

130
        $this->column($pk->getName())->references($tableName)->primaryKey();
3✔
131
        return $this;
3✔
132
    }
133

134
    /**
135
     * Returns the underlying DBAL table.
136
     * @return Table
137
     */
138
    public function getDbalTable(): Table
139
    {
140
        return $this->table;
3✔
141
    }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc