Comparator.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * @author Fabien Potencier <fabien@symfony.com>
  13. */
  14. class Comparator
  15. {
  16. private $target;
  17. private $operator = '==';
  18. public function __construct(?string $target = null, string $operator = '==')
  19. {
  20. if (null === $target) {
  21. trigger_deprecation('symfony/finder', '5.4', 'Constructing a "%s" without setting "$target" is deprecated.', __CLASS__);
  22. }
  23. $this->target = $target;
  24. $this->doSetOperator($operator);
  25. }
  26. /**
  27. * Gets the target value.
  28. *
  29. * @return string
  30. */
  31. public function getTarget()
  32. {
  33. if (null === $this->target) {
  34. trigger_deprecation('symfony/finder', '5.4', 'Calling "%s" without initializing the target is deprecated.', __METHOD__);
  35. }
  36. return $this->target;
  37. }
  38. /**
  39. * @deprecated set the target via the constructor instead
  40. */
  41. public function setTarget(string $target)
  42. {
  43. trigger_deprecation('symfony/finder', '5.4', '"%s" is deprecated. Set the target via the constructor instead.', __METHOD__);
  44. $this->target = $target;
  45. }
  46. /**
  47. * Gets the comparison operator.
  48. *
  49. * @return string
  50. */
  51. public function getOperator()
  52. {
  53. return $this->operator;
  54. }
  55. /**
  56. * Sets the comparison operator.
  57. *
  58. * @throws \InvalidArgumentException
  59. *
  60. * @deprecated set the operator via the constructor instead
  61. */
  62. public function setOperator(string $operator)
  63. {
  64. trigger_deprecation('symfony/finder', '5.4', '"%s" is deprecated. Set the operator via the constructor instead.', __METHOD__);
  65. $this->doSetOperator('' === $operator ? '==' : $operator);
  66. }
  67. /**
  68. * Tests against the target.
  69. *
  70. * @param mixed $test A test value
  71. *
  72. * @return bool
  73. */
  74. public function test($test)
  75. {
  76. if (null === $this->target) {
  77. trigger_deprecation('symfony/finder', '5.4', 'Calling "%s" without initializing the target is deprecated.', __METHOD__);
  78. }
  79. switch ($this->operator) {
  80. case '>':
  81. return $test > $this->target;
  82. case '>=':
  83. return $test >= $this->target;
  84. case '<':
  85. return $test < $this->target;
  86. case '<=':
  87. return $test <= $this->target;
  88. case '!=':
  89. return $test != $this->target;
  90. }
  91. return $test == $this->target;
  92. }
  93. private function doSetOperator(string $operator): void
  94. {
  95. if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
  96. throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
  97. }
  98. $this->operator = $operator;
  99. }
  100. }