| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | private string $name, |
| 14 | 14 | private string $charsetCollate, |
| 15 | 15 | private array $columns |
| 16 | 16 | ) { |
| 17 | - if ($this->columns === []) { | |
| 17 | + if (count($this->columns) <= 0) { | |
| 18 | 18 | throw new MissingColumnsException('The table needs at least one column.'); |
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 | |
| @@ -39,16 +39,22 @@ | ||
| 39 | 39 | |
| 40 | 40 | public function __toString(): string |
| 41 | 41 | { |
| 42 | 42 | $columns = implode(', ', $this->columns); |
| 43 | - $primaryKeys = array_map( | |
| 44 | - static fn(Column $column) => "`{$column->getName()}`", | |
| 45 | - array_filter($this->columns, static fn(Column $column) => $column->isKey() === true) | |
| 46 | - ); | |
| 43 | + $primaryKeys = []; | |
| 47 | 44 | |
| 48 | - $primaryKeysQuery = $primaryKeys === [] | |
| 49 | - ? '' | |
| 50 | - : ', PRIMARY KEY (' . implode(', ', $primaryKeys) . ')'; | |
| 45 | + foreach ($this->columns as $column) { | |
| 46 | + if ($column->isKey() === true) { | |
| 47 | + $primaryKeys[] = "`{$column->getName()}`"; | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 51 | + $primaryKeysQuery = ''; | |
| 52 | + | |
| 53 | + if ($primaryKeys !== []) { | |
| 54 | + $primaryKeysQuery = implode(', ', $primaryKeys); | |
| 55 | + $primaryKeysQuery = ", PRIMARY KEY ($primaryKeysQuery)"; | |
| 56 | + } | |
| 51 | 57 | |
| 52 | 58 | return "CREATE TABLE `$this->name` ( |
| 53 | 59 | $columns{$primaryKeysQuery} |
| 54 | 60 | ) $this->charsetCollate;"; |