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.

php_crud_api_transform.php 1.1KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. function php_crud_api_transform(&$tables) {
  3. $get_objects = function (&$tables,$table_name,$where_index=false,$match_value=false) use (&$get_objects) {
  4. $objects = array();
  5. foreach ($tables[$table_name]['records'] as $record) {
  6. if ($where_index===false || $record[$where_index]==$match_value) {
  7. $object = array();
  8. foreach ($tables[$table_name]['columns'] as $index=>$column) {
  9. $object[$column] = $record[$index];
  10. foreach ($tables as $relation=>$reltable) {
  11. if (isset($reltable['relations'])) {
  12. foreach ($reltable['relations'] as $key=>$target) {
  13. if ($target == "$table_name.$column") {
  14. $column_indices = array_flip($reltable['columns']);
  15. $object[$relation] = $get_objects($tables,$relation,$column_indices[$key],$record[$index]);
  16. }
  17. }
  18. }
  19. }
  20. }
  21. $objects[] = $object;
  22. }
  23. }
  24. return $objects;
  25. };
  26. $tree = array();
  27. foreach ($tables as $name=>$table) {
  28. if (!isset($table['relations'])) {
  29. $tree[$name] = $get_objects($tables,$name);
  30. if (isset($table['results'])) {
  31. $tree['_results'] = $table['results'];
  32. }
  33. }
  34. }
  35. return $tree;
  36. }