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