• 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/FluidSchema.php
1
<?php
2
namespace TheCodingMachine\FluidSchema;
3

4
use Doctrine\DBAL\Schema\Schema;
5

6
/**
7
 * This class wraps a DBAL schema and offers a fluid syntax on top of it.
8
 */
9
class FluidSchema
10
{
11
    /**
12
     * @var Schema
13
     */
14
    private $schema;
15

16
    /**
17
     * @var FluidTable[]
18
     */
19
    private $fluidTables;
20
    /**
21
     * @var NamingStrategyInterface
22
     */
23
    private $namingStrategy;
24

25
    /**
26
     * @param Schema $schema
27
     * @param null|NamingStrategyInterface $namingStrategy
28
     */
29
    public function __construct(Schema $schema, ?NamingStrategyInterface $namingStrategy = null)
30
    {
31
        $this->schema = $schema;
63✔
32
        $this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy();
63✔
33
    }
34

35
    public function table(string $name): FluidTable
36
    {
37
        $name = $this->namingStrategy->quoteIdentifier($name);
60✔
38

39
        if (isset($this->fluidTables[$name])) {
60✔
40
            return $this->fluidTables[$name];
3✔
41
        }
42

43
        if ($this->schema->hasTable($name)) {
60✔
44
            $table = $this->schema->getTable($name);
6✔
45
        } else {
46
            $table = $this->schema->createTable($name);
54✔
47
        }
48

49
        $this->fluidTables[$name] = new FluidTable($this, $table, $this->namingStrategy);
60✔
50
        return $this->fluidTables[$name];
60✔
51
    }
52

53
    /**
54
     * Creates a table joining 2 other tables through a foreign key.
55
     *
56
     * @param string $table1
57
     * @param string $table2
58
     * @return FluidSchema
59
     */
60
    public function junctionTable(string $table1, string $table2): FluidSchema
61
    {
62
        $tableName = $this->namingStrategy->getJointureTableName($table1, $table2);
3✔
63
        $column1 = $this->namingStrategy->getForeignKeyColumnName($table1);
3✔
64
        $column2 = $this->namingStrategy->getForeignKeyColumnName($table2);
3✔
65

66
        $this->table($tableName)
3✔
67
            ->column($column1)->references($table1)->then()
3✔
68
            ->column($column2)->references($table2)->then()
3✔
69
            ->primaryKey([$column1, $column2]);
3✔
70

71
        return $this;
3✔
72
    }
73
    
74
    /**
75
     * Returns the underlying schema.
76
     * @return Schema
77
     */
78
    public function getDbalSchema(): Schema
79
    {
80
        return $this->schema;
18✔
81
    }
82
}
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