| Total Complexity | 9 |
| Total Lines | 65 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class ArrayDataSource |
||
| 8 | { |
||
| 9 | private $dataSource; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * array( |
||
| 13 | * tableName => ( |
||
| 14 | * array( |
||
| 15 | * columnName => value, anotherColumnName => value |
||
| 16 | * ), |
||
| 17 | * another row |
||
| 18 | * ), |
||
| 19 | * another table |
||
| 20 | * ). |
||
| 21 | * |
||
| 22 | * @param array $dataSource |
||
| 23 | */ |
||
| 24 | public function __construct(array $dataSource) |
||
| 25 | { |
||
| 26 | $this->dataSource = $dataSource; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function getTableNames(): array |
||
| 30 | { |
||
| 31 | $tables = array(); |
||
| 32 | foreach ($this->dataSource as $tableName => $_) { |
||
| 33 | $tables[] = $tableName; |
||
| 34 | } |
||
| 35 | |||
| 36 | return $tables; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function getTableData(string $tableName, bool $sort = false) |
||
| 40 | { |
||
| 41 | if (array_key_exists($tableName, $this->dataSource)) { |
||
| 42 | if ($sort) { |
||
| 43 | return $this->sortData($this->dataSource[$tableName]); |
||
| 44 | } else { |
||
| 45 | return $this->dataSource[$tableName]; |
||
| 46 | } |
||
| 47 | } else { |
||
| 48 | throw new \Exception(sprintf( |
||
| 49 | 'Table "%s" does not exists', |
||
| 50 | $tableName |
||
| 51 | )); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | private function sortData(array $data): array |
||
| 72 | } |
||
| 73 | } |
||
| 74 |