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.2KB

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