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.

FilterInfo.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Record;
  3. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  4. use Tqdev\PhpCrudApi\Record\Condition\AndCondition;
  5. use Tqdev\PhpCrudApi\Record\Condition\Condition;
  6. use Tqdev\PhpCrudApi\Record\Condition\NoCondition;
  7. use Tqdev\PhpCrudApi\Record\Condition\OrCondition;
  8. class FilterInfo
  9. {
  10. private function addConditionFromFilterPath(PathTree $conditions, array $path, ReflectedTable $table, array $params)
  11. {
  12. $key = 'filter' . implode('', $path);
  13. if (isset($params[$key])) {
  14. foreach ($params[$key] as $filter) {
  15. $condition = Condition::fromString($table, $filter);
  16. if (($condition instanceof NoCondition) == false) {
  17. $conditions->put($path, $condition);
  18. }
  19. }
  20. }
  21. }
  22. private function getConditionsAsPathTree(ReflectedTable $table, array $params): PathTree
  23. {
  24. $conditions = new PathTree();
  25. $this->addConditionFromFilterPath($conditions, [], $table, $params);
  26. for ($n = ord('0'); $n <= ord('9'); $n++) {
  27. $this->addConditionFromFilterPath($conditions, [chr($n)], $table, $params);
  28. for ($l = ord('a'); $l <= ord('f'); $l++) {
  29. $this->addConditionFromFilterPath($conditions, [chr($n), chr($l)], $table, $params);
  30. }
  31. }
  32. return $conditions;
  33. }
  34. private function combinePathTreeOfConditions(PathTree $tree): Condition
  35. {
  36. $andConditions = $tree->getValues();
  37. $and = AndCondition::fromArray($andConditions);
  38. $orConditions = [];
  39. foreach ($tree->getKeys() as $p) {
  40. $orConditions[] = $this->combinePathTreeOfConditions($tree->get($p));
  41. }
  42. $or = OrCondition::fromArray($orConditions);
  43. return $and->_and($or);
  44. }
  45. public function getCombinedConditions(ReflectedTable $table, array $params): Condition
  46. {
  47. return $this->combinePathTreeOfConditions($this->getConditionsAsPathTree($table, $params));
  48. }
  49. }