| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 78.57% | 11 / 14 |  | 40.00% | 2 / 5 | CRAP |  | 0.00% | 0 / 1 | 
| SchemaMapper |  | 78.57% | 11 / 14 |  | 40.00% | 2 / 5 | 8.63 |  | 0.00% | 0 / 1 | 
| __construct |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| getTables |  | 87.50% | 7 / 8 |  | 0.00% | 0 / 1 | 3.02 | |||
| getTable |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 1 | 2 | |||
| getFields |  | 100.00% | 3 / 3 |  | 100.00% | 1 / 1 | 2 | |||
| getField |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 1 | 2 | |||
| 1 | <?php | 
| 2 | /** | 
| 3 | * Jingga | 
| 4 | * | 
| 5 | * PHP Version 8.1 | 
| 6 | * | 
| 7 | * @package phpOMS\DataStorage\Database | 
| 8 | * @copyright Dennis Eichhorn | 
| 9 | * @license OMS License 2.0 | 
| 10 | * @version 1.0.0 | 
| 11 | * @link https://jingga.app | 
| 12 | */ | 
| 13 | declare(strict_types=1); | 
| 14 | |
| 15 | namespace phpOMS\DataStorage\Database; | 
| 16 | |
| 17 | use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; | 
| 18 | use phpOMS\DataStorage\Database\Schema\Builder; | 
| 19 | use phpOMS\DataStorage\Database\Schema\Field; | 
| 20 | use phpOMS\DataStorage\Database\Schema\Table; | 
| 21 | |
| 22 | /** | 
| 23 | * Database schema mapper. | 
| 24 | * | 
| 25 | * @package phpOMS\DataStorage\Database | 
| 26 | * @license OMS License 2.0 | 
| 27 | * @link https://jingga.app | 
| 28 | * @since 1.0.0 | 
| 29 | */ | 
| 30 | class SchemaMapper | 
| 31 | { | 
| 32 | /** | 
| 33 | * Database connection. | 
| 34 | * | 
| 35 | * @var ConnectionAbstract | 
| 36 | * @since 1.0.0 | 
| 37 | */ | 
| 38 | protected ConnectionAbstract $db; | 
| 39 | |
| 40 | /** | 
| 41 | * Constructor. | 
| 42 | * | 
| 43 | * @param ConnectionAbstract $db Database connection | 
| 44 | * | 
| 45 | * @since 1.0.0 | 
| 46 | */ | 
| 47 | public function __construct(ConnectionAbstract $db) | 
| 48 | { | 
| 49 | $this->db = $db; | 
| 50 | } | 
| 51 | |
| 52 | /** | 
| 53 | * Get all tables of database | 
| 54 | * | 
| 55 | * @return array | 
| 56 | * | 
| 57 | * @since 1.0.0 | 
| 58 | */ | 
| 59 | public function getTables() : array | 
| 60 | { | 
| 61 | $tables = []; | 
| 62 | $builder = new Builder($this->db); | 
| 63 | |
| 64 | /** @var array<int, string[]> $tNames */ | 
| 65 | $tNames = $builder->selectTables()->execute()?->fetchAll(\PDO::FETCH_ASSOC); | 
| 66 | |
| 67 | if ($tNames === null) { | 
| 68 | return $tables; | 
| 69 | } | 
| 70 | |
| 71 | foreach ($tNames as $name) { | 
| 72 | $tables[] = \array_values($name)[0]; | 
| 73 | } | 
| 74 | |
| 75 | return $tables; | 
| 76 | } | 
| 77 | |
| 78 | /** | 
| 79 | * Get table by name | 
| 80 | * | 
| 81 | * @param string $name Name of the table | 
| 82 | * | 
| 83 | * @return Table | 
| 84 | * | 
| 85 | * @since 1.0.0 | 
| 86 | */ | 
| 87 | public function getTable(string $name) : Table | 
| 88 | { | 
| 89 | return new Table(); | 
| 90 | } | 
| 91 | |
| 92 | /** | 
| 93 | * Get fields of table | 
| 94 | * | 
| 95 | * @param string $table Name of the table | 
| 96 | * | 
| 97 | * @return array | 
| 98 | * | 
| 99 | * @since 1.0.0 | 
| 100 | */ | 
| 101 | public function getFields(string $table) : array | 
| 102 | { | 
| 103 | $builder = new Builder($this->db); | 
| 104 | $fields = $builder->selectFields($table)->execute()?->fetchAll(\PDO::FETCH_ASSOC); | 
| 105 | |
| 106 | return $fields === null ? [] : $fields; | 
| 107 | } | 
| 108 | |
| 109 | /** | 
| 110 | * Get field of table | 
| 111 | * | 
| 112 | * @param string $table Name of the table | 
| 113 | * @param string $name Name of the field | 
| 114 | * | 
| 115 | * @return Field | 
| 116 | * | 
| 117 | * @since 1.0.0 | 
| 118 | */ | 
| 119 | public function getField(string $table, string $name) : Field | 
| 120 | { | 
| 121 | return new Field(); | 
| 122 | } | 
| 123 | } |