Licitator 1.0
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.

98 lines
3.7 KiB

5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Service;
  3. use Symfony\Component\ExpressionLanguage\ExpressionFunction;
  4. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  5. /**
  6. * Provides functions for comparing array values in expressions.
  7. */
  8. class ArrayExpressionLanguageProvider implements ExpressionFunctionProviderInterface
  9. {
  10. /**
  11. * @return ExpressionFunction[]
  12. */
  13. public function getFunctions()
  14. {
  15. return [
  16. new ExpressionFunction(
  17. 'disjoint',
  18. function () {
  19. return 'empty(array_intersect($parameter, explode(\',\', $value)))';
  20. },
  21. function ($variables, $parameter, $value) {
  22. return empty(array_intersect($parameter, explode(',', $value)));
  23. }
  24. ),
  25. new ExpressionFunction(
  26. 'intersecting',
  27. function () {
  28. return '!empty(array_intersect($parameter, explode(\',\', $value)))';
  29. },
  30. function ($variables, $parameter, $value) {
  31. return !empty(array_intersect($parameter, explode(',', $value)));
  32. }
  33. ),
  34. new ExpressionFunction(
  35. 'included',
  36. function () {
  37. return 'empty(array_diff($parameter, explode(\',\', $value)))';
  38. },
  39. function ($variables, $parameter, $value) {
  40. return empty(array_diff($parameter, explode(',', $value)));
  41. }
  42. ),
  43. new ExpressionFunction(
  44. 'completed',
  45. function () {
  46. return 'empty(array_diff(explode(\',\', $value),array_intersect($parameter, explode(\',\', $value)))';
  47. },
  48. function ($variables, $parameter, $value) {
  49. return empty(array_diff( explode(',', $value),array_intersect($parameter, explode(',', $value))));
  50. }
  51. ),
  52. new ExpressionFunction(
  53. 'nocompleted',
  54. function () {
  55. return 'empty(array_intersect($parameter, explode(\',\', $value)))';
  56. },
  57. function ($variables, $parameter, $value) {
  58. return empty(array_intersect($parameter, explode(',', $value)));
  59. }
  60. ),
  61. new ExpressionFunction(
  62. 'identical',
  63. function () {
  64. return '$values = is_array($value) ? $value : explode(\',\', $value);
  65. $values=array_unique($values);
  66. return empty(array_diff($parameter, $values)) && empty(array_diff($values, $parameter));';
  67. },
  68. function ($variables, $parameter, $value) {
  69. $values = is_array($value) ? $value : explode(',', $value);
  70. return empty(array_diff($parameter, $values)) && empty(array_diff($values, $parameter));
  71. }
  72. ),
  73. new ExpressionFunction(
  74. 'includes',
  75. function () {
  76. return 'empty(array_diff(explode(\',\', $value), $parameter))';
  77. },
  78. function ($variables, $parameter, $value) {
  79. return empty(array_diff(explode(',', $value), $parameter));
  80. }
  81. ),
  82. new ExpressionFunction(
  83. 'uniqount',
  84. function () {
  85. return 'count(array_unique($value))';
  86. },
  87. function ($variables, $value) {
  88. return count(array_unique($value));
  89. }
  90. ),
  91. ];
  92. }
  93. }