| @@ -1,30 +1,75 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Table.php | |
| 4 | + * | |
| 5 | + * The Table class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Setup\Database; |
| 6 | 19 | |
| 20 | +/** | |
| 21 | + * Class Table | |
| 22 | + * | |
| 23 | + * @package UserAccessManager\Setup\Database | |
| 24 | + */ | |
| 7 | 25 | class Table |
| 8 | 26 | { |
| 9 | 27 | /** |
| 28 | + * @var string | |
| 29 | + */ | |
| 30 | + private $name; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * @var string | |
| 34 | + */ | |
| 35 | + private $charsetCollate; | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @var Column[] | |
| 39 | + */ | |
| 40 | + private $columns; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Table constructor. | |
| 44 | + * @param string $name | |
| 45 | + * @param string $charsetCollate | |
| 46 | + * @param array $columns | |
| 10 | 47 | * @throws MissingColumnsException |
| 11 | 48 | */ |
| 12 | - public function __construct( | |
| 13 | - private string $name, | |
| 14 | - private string $charsetCollate, | |
| 15 | - private array $columns | |
| 16 | - ) { | |
| 17 | - if ($this->columns === []) { | |
| 49 | + public function __construct(string $name, string $charsetCollate, array $columns) | |
| 50 | + { | |
| 51 | + $this->name = $name; | |
| 52 | + $this->charsetCollate = $charsetCollate; | |
| 53 | + | |
| 54 | + if ($columns === []) { | |
| 18 | 55 | throw new MissingColumnsException('The table needs at least one column.'); |
| 19 | 56 | } |
| 57 | + | |
| 58 | + $this->columns = $columns; | |
| 20 | 59 | } |
| 21 | 60 | |
| 61 | + /** | |
| 62 | + * @return string | |
| 63 | + */ | |
| 22 | 64 | public function getName(): string |
| 23 | 65 | { |
| 24 | 66 | return $this->name; |
| 25 | 67 | } |
| 26 | 68 | |
| 69 | + /** | |
| 70 | + * @return string | |
| 71 | + */ | |
| 27 | 72 | public function getCharsetCollate(): string |
| 28 | 73 | { |
| 29 | 74 | return $this->charsetCollate; |
| 30 | 75 | } |
| @@ -36,21 +81,31 @@ | ||
| 36 | 81 | { |
| 37 | 82 | return $this->columns; |
| 38 | 83 | } |
| 39 | 84 | |
| 85 | + /** | |
| 86 | + * Returns the table in sql format. | |
| 87 | + * @return string | |
| 88 | + */ | |
| 40 | 89 | public function __toString(): string |
| 41 | 90 | { |
| 42 | 91 | $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 | - ); | |
| 92 | + $primaryKeys = []; | |
| 47 | 93 | |
| 48 | - $primaryKeysQuery = $primaryKeys === [] | |
| 49 | - ? '' | |
| 50 | - : ', PRIMARY KEY (' . implode(', ', $primaryKeys) . ')'; | |
| 94 | + foreach ($this->columns as $column) { | |
| 95 | + if ($column->isKey() === true) { | |
| 96 | + $primaryKeys[] = "`{$column->getName()}`"; | |
| 97 | + } | |
| 98 | + } | |
| 51 | 99 | |
| 52 | - return "CREATE TABLE `$this->name` ( | |
| 53 | - $columns{$primaryKeysQuery} | |
| 54 | - ) $this->charsetCollate;"; | |
| 100 | + $primaryKeysQuery = ''; | |
| 101 | + | |
| 102 | + if ($primaryKeys !== []) { | |
| 103 | + $primaryKeysQuery = implode(', ', $primaryKeys); | |
| 104 | + $primaryKeysQuery = ", PRIMARY KEY ({$primaryKeysQuery})"; | |
| 105 | + } | |
| 106 | + | |
| 107 | + return "CREATE TABLE `{$this->name}` ( | |
| 108 | + {$columns}{$primaryKeysQuery} | |
| 109 | + ) {$this->charsetCollate};"; | |
| 55 | 110 | } |
| 56 | 111 | } |