Comparator.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Symfony\Component\Finder\Comparator;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Comparator.
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com> PHP port
  15. */
  16. class Comparator
  17. {
  18. protected $target;
  19. protected $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. public function setOperator($operator)
  53. {
  54. if (!$operator) {
  55. $operator = '==';
  56. }
  57. if (!in_array($operator, array('>', '<', '>=', '<=', '=='))) {
  58. throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
  59. }
  60. $this->operator = $operator;
  61. }
  62. /**
  63. * Tests against the target.
  64. *
  65. * @param mixed $test A test value
  66. */
  67. public function test($test)
  68. {
  69. switch ($this->operator) {
  70. case '>':
  71. return $test > $this->target;
  72. case '>=':
  73. return $test >= $this->target;
  74. case '<':
  75. return $test < $this->target;
  76. case '<=':
  77. return $test <= $this->target;
  78. }
  79. return $test == $this->target;
  80. }
  81. }