packages/ztd-query-core/src/Schema/TableDefinitionRegistry.php

1<?php
2
3declare(strict_types=1);
4
5namespace ZtdQuery\Schema;
6
7/**
8 * Stores TableDefinition instances keyed by table name.
9 */
10final class TableDefinitionRegistry
11{
12    /**
13     * @var array<string, TableDefinition>
14     */
15    private array $definitions = [];
16
17    /** @var array<string, TableDefinition> */
18    private array $removedDefinitions = [];
19
20    /**
21     * Register or replace a table definition.
22     */
23    public function register(string $tableName, TableDefinition $definition): void
24    {
25        $this->definitions[$tableName] = $definition;
26        unset($this->removedDefinitions[$tableName]);
27    }
28
29    /**
30     * Return a table definition when present.
31     */
32    public function get(string $tableName): ?TableDefinition
33    {
34        return $this->definitions[$tableName] ?? null;
35    }
36
37    /**
38     * Check whether a table is registered.
39     */
40    public function has(string $tableName): bool
41    {
42        return isset($this->definitions[$tableName]);
43    }
44
45    /**
46     * Return all registered table definitions.
47     *
48     * @return array<string, TableDefinition>
49     */
50    public function getAll(): array
51    {
52        return $this->definitions;
53    }
54
55    /**
56     * @return array<string, TableDefinition>
57     */
58    public function getAllRemoved(): array
59    {
60        return $this->removedDefinitions;
61    }
62
63    /**
64     * Reports whether removed.
65     *
66     * @param string $tableName
67     * @return bool
68     */
69    public function isRemoved(string $tableName): bool
70    {
71        return isset($this->removedDefinitions[$tableName]);
72    }
73
74    /**
75     * Check whether at least one table is registered.
76     */
77    public function hasAnyTables(): bool
78    {
79        return $this->definitions !== [];
80    }
81
82    /**
83     * Remove all registered tables.
84     */
85    public function clear(): void
86    {
87        $this->definitions = [];
88        $this->removedDefinitions = [];
89    }
90
91    /**
92     * Snapshot.
93     *
94     * @return self
95     */
96    public function snapshot(): self
97    {
98        return clone $this;
99    }
100
101    /**
102     * Restore.
103     *
104     * @param self $snapshot
105     */
106    public function restore(self $snapshot): void
107    {
108        $this->definitions = $snapshot->definitions;
109        $this->removedDefinitions = $snapshot->removedDefinitions;
110    }
111
112    /**
113     * Remove a registered table.
114     */
115    public function unregister(string $tableName): void
116    {
117        unset($this->definitions[$tableName]);
118        unset($this->removedDefinitions[$tableName]);
119    }
120
121    /**
122     * Mark removed.
123     *
124     * @param string $tableName
125     */
126    public function markRemoved(string $tableName): void
127    {
128        $definition = $this->definitions[$tableName] ?? null;
129        if ($definition === null) {
130            return;
131        }
132
133        $this->removedDefinitions[$tableName] = $definition;
134        unset($this->definitions[$tableName]);
135    }
136}
137