LengthException.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Exceptions;
  8. /**
  9. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  10. * @author Danilo Correa <danilosilva87@gmail.com>
  11. * @author Henrique Moody <henriquemoody@gmail.com>
  12. * @author Mazen Touati <mazen_touati@hotmail.com>
  13. */
  14. final class LengthException extends ValidationException
  15. {
  16. public const BOTH = 'both';
  17. public const LOWER = 'lower';
  18. public const LOWER_INCLUSIVE = 'lower_inclusive';
  19. public const GREATER = 'greater';
  20. public const GREATER_INCLUSIVE = 'greater_inclusive';
  21. public const EXACT = 'exact';
  22. /**
  23. * {@inheritDoc}
  24. */
  25. protected $defaultTemplates = [
  26. self::MODE_DEFAULT => [
  27. self::BOTH => '{{name}} 长度必须在 {{minValue}} 与 {{maxValue}} 之间',
  28. self::LOWER => '{{name}} 长度必须大于 {{minValue}}',
  29. self::LOWER_INCLUSIVE => '{{name}} 的长度必须大于或等于 {{minValue}}',
  30. self::GREATER => '{{name}} 长度必须小于 {{maxValue}}',
  31. self::GREATER_INCLUSIVE => '{{name}} 长度必须小于或等于 {{maxValue}}',
  32. self::EXACT => '{{name}} 长度必须是 {{maxValue}}',
  33. ],
  34. self::MODE_NEGATIVE => [
  35. self::BOTH => '{{name}} 的长度不能介于 {{minValue}} 和 {{maxValue}} 之间',
  36. self::LOWER => '{{name}} 长度不能大于 {{minValue}}',
  37. self::LOWER_INCLUSIVE => '{{name}} 长度不能大于或等于 {{minValue}}',
  38. self::GREATER => '{{name}} 长度不得小于 {{maxValue}}',
  39. self::GREATER_INCLUSIVE => '{{name}} 长度不能小于或等于 {{maxValue}}',
  40. self::EXACT => '{{name}} 长度不能是 {{maxValue}}',
  41. ],
  42. ];
  43. /**
  44. * {@inheritDoc}
  45. */
  46. protected function chooseTemplate(): string
  47. {
  48. $isInclusive = $this->getParam('inclusive');
  49. if (!$this->getParam('minValue')) {
  50. return $isInclusive === true ? self::GREATER_INCLUSIVE : self::GREATER;
  51. }
  52. if (!$this->getParam('maxValue')) {
  53. return $isInclusive === true ? self::LOWER_INCLUSIVE : self::LOWER;
  54. }
  55. if ($this->getParam('minValue') == $this->getParam('maxValue')) {
  56. return self::EXACT;
  57. }
  58. return self::BOTH;
  59. }
  60. }