Comparator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  30. * Sets the target value.
  31. *
  32. * @param string $target The target value
  33. */
  34. public function setTarget($target)
  35. {
  36. $this->target = $target;
  37. }
  38. /**
  39. * Gets the comparison operator.
  40. *
  41. * @return string The operator
  42. */
  43. public function getOperator()
  44. {
  45. return $this->operator;
  46. }
  47. /**
  48. * Sets the comparison operator.
  49. *
  50. * @param string $operator A valid operator
  51. *
  52. * @throws \InvalidArgumentException
  53. */
  54. public function setOperator($operator)
  55. {
  56. if (!$operator) {
  57. $operator = '==';
  58. }
  59. if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
  60. throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
  61. }
  62. $this->operator = $operator;
  63. }
  64. /**
  65. * Tests against the target.
  66. *
  67. * @param mixed $test A test value
  68. *
  69. * @return bool
  70. */
  71. public function test($test)
  72. {
  73. switch ($this->operator) {
  74. case '>':
  75. return $test > $this->target;
  76. case '>=':
  77. return $test >= $this->target;
  78. case '<':
  79. return $test < $this->target;
  80. case '<=':
  81. return $test <= $this->target;
  82. case '!=':
  83. return $test != $this->target;
  84. }
  85. return $test == $this->target;
  86. }
  87. }