Parser.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model\Widget\Grid;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Parser
  12. {
  13. /**
  14. * List of allowed operations
  15. *
  16. * @var string[]
  17. */
  18. protected $_operations = ['-', '+', '/', '*'];
  19. /**
  20. * Parse expression
  21. *
  22. * @param string $expression
  23. * @return array
  24. */
  25. public function parseExpression($expression)
  26. {
  27. $stack = [];
  28. $expression = trim($expression);
  29. foreach ($this->_operations as $operation) {
  30. $splittedExpr = preg_split('/\\' . $operation . '/', $expression, -1, PREG_SPLIT_DELIM_CAPTURE);
  31. $count = count($splittedExpr);
  32. if ($count > 1) {
  33. for ($i = 0; $i < $count; $i++) {
  34. $stack = array_merge($stack, $this->parseExpression($splittedExpr[$i]));
  35. if ($i > 0) {
  36. $stack[] = $operation;
  37. }
  38. }
  39. break;
  40. }
  41. }
  42. return empty($stack) ? [$expression] : $stack;
  43. }
  44. /**
  45. * Check if string is operation
  46. *
  47. * @param string $operation
  48. * @return bool
  49. */
  50. public function isOperation($operation)
  51. {
  52. return in_array($operation, $this->_operations);
  53. }
  54. }