diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Schema/UniqueConstraint.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Schema/UniqueConstraint.php | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Schema/UniqueConstraint.php b/vendor/doctrine/dbal/src/Schema/UniqueConstraint.php new file mode 100644 index 0000000..a33d446 --- /dev/null +++ b/vendor/doctrine/dbal/src/Schema/UniqueConstraint.php | |||
@@ -0,0 +1,152 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Schema; | ||
6 | |||
7 | use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
8 | |||
9 | use function array_keys; | ||
10 | use function array_map; | ||
11 | use function strtolower; | ||
12 | |||
13 | /** | ||
14 | * Class for a unique constraint. | ||
15 | */ | ||
16 | class UniqueConstraint extends AbstractAsset | ||
17 | { | ||
18 | /** | ||
19 | * Asset identifier instances of the column names the unique constraint is associated with. | ||
20 | * | ||
21 | * @var array<string, Identifier> | ||
22 | */ | ||
23 | protected array $columns = []; | ||
24 | |||
25 | /** | ||
26 | * Platform specific flags | ||
27 | * | ||
28 | * @var array<string, true> | ||
29 | */ | ||
30 | protected array $flags = []; | ||
31 | |||
32 | /** | ||
33 | * @param array<string> $columns | ||
34 | * @param array<string> $flags | ||
35 | * @param array<string, mixed> $options | ||
36 | */ | ||
37 | public function __construct( | ||
38 | string $name, | ||
39 | array $columns, | ||
40 | array $flags = [], | ||
41 | private readonly array $options = [], | ||
42 | ) { | ||
43 | $this->_setName($name); | ||
44 | |||
45 | foreach ($columns as $column) { | ||
46 | $this->addColumn($column); | ||
47 | } | ||
48 | |||
49 | foreach ($flags as $flag) { | ||
50 | $this->addFlag($flag); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | /** | ||
55 | * Returns the names of the referencing table columns the constraint is associated with. | ||
56 | * | ||
57 | * @return list<string> | ||
58 | */ | ||
59 | public function getColumns(): array | ||
60 | { | ||
61 | return array_keys($this->columns); | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * Returns the quoted representation of the column names the constraint is associated with. | ||
66 | * | ||
67 | * But only if they were defined with one or a column name | ||
68 | * is a keyword reserved by the platform. | ||
69 | * Otherwise, the plain unquoted value as inserted is returned. | ||
70 | * | ||
71 | * @param AbstractPlatform $platform The platform to use for quotation. | ||
72 | * | ||
73 | * @return list<string> | ||
74 | */ | ||
75 | public function getQuotedColumns(AbstractPlatform $platform): array | ||
76 | { | ||
77 | $columns = []; | ||
78 | |||
79 | foreach ($this->columns as $column) { | ||
80 | $columns[] = $column->getQuotedName($platform); | ||
81 | } | ||
82 | |||
83 | return $columns; | ||
84 | } | ||
85 | |||
86 | /** @return array<int, string> */ | ||
87 | public function getUnquotedColumns(): array | ||
88 | { | ||
89 | return array_map($this->trimQuotes(...), $this->getColumns()); | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Returns platform specific flags for unique constraint. | ||
94 | * | ||
95 | * @return array<int, string> | ||
96 | */ | ||
97 | public function getFlags(): array | ||
98 | { | ||
99 | return array_keys($this->flags); | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * Adds flag for a unique constraint that translates to platform specific handling. | ||
104 | * | ||
105 | * @return $this | ||
106 | * | ||
107 | * @example $uniqueConstraint->addFlag('CLUSTERED') | ||
108 | */ | ||
109 | public function addFlag(string $flag): self | ||
110 | { | ||
111 | $this->flags[strtolower($flag)] = true; | ||
112 | |||
113 | return $this; | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * Does this unique constraint have a specific flag? | ||
118 | */ | ||
119 | public function hasFlag(string $flag): bool | ||
120 | { | ||
121 | return isset($this->flags[strtolower($flag)]); | ||
122 | } | ||
123 | |||
124 | /** | ||
125 | * Removes a flag. | ||
126 | */ | ||
127 | public function removeFlag(string $flag): void | ||
128 | { | ||
129 | unset($this->flags[strtolower($flag)]); | ||
130 | } | ||
131 | |||
132 | public function hasOption(string $name): bool | ||
133 | { | ||
134 | return isset($this->options[strtolower($name)]); | ||
135 | } | ||
136 | |||
137 | public function getOption(string $name): mixed | ||
138 | { | ||
139 | return $this->options[strtolower($name)]; | ||
140 | } | ||
141 | |||
142 | /** @return array<string, mixed> */ | ||
143 | public function getOptions(): array | ||
144 | { | ||
145 | return $this->options; | ||
146 | } | ||
147 | |||
148 | protected function addColumn(string $column): void | ||
149 | { | ||
150 | $this->columns[$column] = new Identifier($column); | ||
151 | } | ||
152 | } | ||