Comparator.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder\Comparator;
  11. /**
  12. * Comparator.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Comparator
  17. {
  18. private $target;
  19. private $operator = '==';
  20. /**
  21. * Gets the target value.
  22. *
  23. * @return string The target value
  24. */
  25. public function getTarget()
  26. {
  27. return $this->target;
  28. }
  29. public function setTarget(string $target)
  30. {
  31. $this->target = $target;
  32. }
  33. /**
  34. * Gets the comparison operator.
  35. *
  36. * @return string The operator
  37. */
  38. public function getOperator()
  39. {
  40. return $this->operator;
  41. }
  42. /**
  43. * Sets the comparison operator.
  44. *
  45. * @throws \InvalidArgumentException
  46. */
  47. public function setOperator(string $operator)
  48. {
  49. if ('' === $operator) {
  50. $operator = '==';
  51. }
  52. if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
  53. throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
  54. }
  55. $this->operator = $operator;
  56. }
  57. /**
  58. * Tests against the target.
  59. *
  60. * @param mixed $test A test value
  61. *
  62. * @return bool
  63. */
  64. public function test($test)
  65. {
  66. switch ($this->operator) {
  67. case '>':
  68. return $test > $this->target;
  69. case '>=':
  70. return $test >= $this->target;
  71. case '<':
  72. return $test < $this->target;
  73. case '<=':
  74. return $test <= $this->target;
  75. case '!=':
  76. return $test != $this->target;
  77. }
  78. return $test == $this->target;
  79. }
  80. }