| @@ -1,31 +1,75 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -declare(strict_types=1); | |
| 4 | - | |
| 2 | +/** | |
| 3 | + * Table.php | |
| 4 | + * | |
| 5 | + * The Table class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <[email protected]> | |
| 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 | + */ | |
| 5 | 15 | namespace UserAccessManager\Setup\Database; |
| 6 | 16 | |
| 17 | +/** | |
| 18 | + * Class Table | |
| 19 | + * | |
| 20 | + * @package UserAccessManager\Setup\Database | |
| 21 | + */ | |
| 7 | 22 | class Table |
| 8 | 23 | { |
| 9 | 24 | /** |
| 25 | + * @var string | |
| 26 | + */ | |
| 27 | + private $name; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @var string | |
| 31 | + */ | |
| 32 | + private $charsetCollate; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * @var Column[] | |
| 36 | + */ | |
| 37 | + private $columns; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Table constructor. | |
| 41 | + * | |
| 42 | + * @param string $name | |
| 43 | + * @param string $charsetCollate | |
| 44 | + * @param array $columns | |
| 45 | + * | |
| 10 | 46 | * @throws MissingColumnsException |
| 11 | 47 | */ |
| 12 | - public function __construct( | |
| 13 | - private string $name, | |
| 14 | - private string $charsetCollate, | |
| 15 | - private array $columns | |
| 16 | - ) { | |
| 17 | - if (count($this->columns) <= 0) { | |
| 48 | + public function __construct($name, $charsetCollate, array $columns) | |
| 49 | + { | |
| 50 | + $this->name = $name; | |
| 51 | + $this->charsetCollate = $charsetCollate; | |
| 52 | + | |
| 53 | + if ($columns === []) { | |
| 18 | 54 | throw new MissingColumnsException('The table needs at least one column.'); |
| 19 | 55 | } |
| 56 | + | |
| 57 | + $this->columns = $columns; | |
| 20 | 58 | } |
| 21 | 59 | |
| 22 | - public function getName(): string | |
| 60 | + /** | |
| 61 | + * @return string | |
| 62 | + */ | |
| 63 | + public function getName() | |
| 23 | 64 | { |
| 24 | 65 | return $this->name; |
| 25 | 66 | } |
| 26 | 67 | |
| 27 | - public function getCharsetCollate(): string | |
| 68 | + /** | |
| 69 | + * @return string | |
| 70 | + */ | |
| 71 | + public function getCharsetCollate() | |
| 28 | 72 | { |
| 29 | 73 | return $this->charsetCollate; |
| 30 | 74 | } |
| 31 | 75 | |
| @@ -31,14 +75,19 @@ | ||
| 31 | 75 | |
| 32 | 76 | /** |
| 33 | 77 | * @return Column[] |
| 34 | 78 | */ |
| 35 | - public function getColumns(): array | |
| 79 | + public function getColumns() | |
| 36 | 80 | { |
| 37 | 81 | return $this->columns; |
| 38 | 82 | } |
| 39 | 83 | |
| 40 | - public function __toString(): string | |
| 84 | + /** | |
| 85 | + * Returns the table in sql format. | |
| 86 | + * | |
| 87 | + * @return string | |
| 88 | + */ | |
| 89 | + public function __toString() | |
| 41 | 90 | { |
| 42 | 91 | $columns = implode(', ', $this->columns); |
| 43 | 92 | $primaryKeys = []; |
| 44 | 93 | |
| @@ -51,12 +100,14 @@ | ||
| 51 | 100 | $primaryKeysQuery = ''; |
| 52 | 101 | |
| 53 | 102 | if ($primaryKeys !== []) { |
| 54 | 103 | $primaryKeysQuery = implode(', ', $primaryKeys); |
| 55 | - $primaryKeysQuery = ", PRIMARY KEY ($primaryKeysQuery)"; | |
| 104 | + $primaryKeysQuery = ", PRIMARY KEY ({$primaryKeysQuery})"; | |
| 56 | 105 | } |
| 57 | 106 | |
| 58 | - return "CREATE TABLE `$this->name` ( | |
| 59 | - $columns{$primaryKeysQuery} | |
| 60 | - ) $this->charsetCollate;"; | |
| 107 | + $table = "CREATE TABLE `{$this->name}` ( | |
| 108 | + {$columns}{$primaryKeysQuery} | |
| 109 | + ) {$this->charsetCollate};"; | |
| 110 | + | |
| 111 | + return $table; | |
| 61 | 112 | } |
| 62 | 113 | } |