Between.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /*
  3. * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. declare(strict_types=1);
  7. namespace Respect\Validation\Rules;
  8. use Respect\Validation\Exceptions\ComponentException;
  9. use Respect\Validation\Helpers\CanCompareValues;
  10. /**
  11. * Validates whether the input is between two other values.
  12. *
  13. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  14. * @author Henrique Moody <henriquemoody@gmail.com>
  15. */
  16. final class Between extends AbstractEnvelope
  17. {
  18. use CanCompareValues;
  19. /**
  20. * Initializes the rule.
  21. *
  22. * @param mixed $minValue
  23. * @param mixed $maxValue
  24. *
  25. * @throws ComponentException
  26. */
  27. public function __construct($minValue, $maxValue)
  28. {
  29. if ($this->toComparable($minValue) >= $this->toComparable($maxValue)) {
  30. throw new ComponentException('Minimum cannot be less than or equals to maximum');
  31. }
  32. parent::__construct(
  33. new AllOf(
  34. new Min($minValue),
  35. new Max($maxValue)
  36. ),
  37. [
  38. 'minValue' => $minValue,
  39. 'maxValue' => $maxValue,
  40. ]
  41. );
  42. }
  43. }