api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReflectedDatabase.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Column\Reflection;
  3. use Tqdev\PhpCrudApi\Database\GenericReflection;
  4. class ReflectedDatabase implements \JsonSerializable
  5. {
  6. private $name;
  7. private $tables;
  8. public function __construct(String $name, array $tables)
  9. {
  10. $this->name = $name;
  11. $this->tables = [];
  12. foreach ($tables as $table) {
  13. $this->tables[$table->getName()] = $table;
  14. }
  15. }
  16. public static function fromReflection(GenericReflection $reflection): ReflectedDatabase
  17. {
  18. $name = $reflection->getDatabaseName();
  19. $tables = [];
  20. foreach ($reflection->getTables() as $tableName) {
  21. if (in_array($tableName['TABLE_NAME'], $reflection->getIgnoredTables())) {
  22. continue;
  23. }
  24. $table = ReflectedTable::fromReflection($reflection, $tableName);
  25. $tables[$table->getName()] = $table;
  26. }
  27. return new ReflectedDatabase($name, array_values($tables));
  28. }
  29. public static function fromJson( /* object */$json): ReflectedDatabase
  30. {
  31. $name = $json->name;
  32. $tables = [];
  33. if (isset($json->tables) && is_array($json->tables)) {
  34. foreach ($json->tables as $table) {
  35. $tables[] = ReflectedTable::fromJson($table);
  36. }
  37. }
  38. return new ReflectedDatabase($name, $tables);
  39. }
  40. public function getName(): String
  41. {
  42. return $this->name;
  43. }
  44. public function exists(String $tableName): bool
  45. {
  46. return isset($this->tables[$tableName]);
  47. }
  48. public function get(String $tableName): ReflectedTable
  49. {
  50. return $this->tables[$tableName];
  51. }
  52. public function getTableNames(): array
  53. {
  54. return array_keys($this->tables);
  55. }
  56. public function serialize()
  57. {
  58. return [
  59. 'name' => $this->name,
  60. 'tables' => array_values($this->tables),
  61. ];
  62. }
  63. public function jsonSerialize()
  64. {
  65. return $this->serialize();
  66. }
  67. }