PhoneException.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. use Respect\Validation\Helpers\CountryInfo;
  9. /**
  10. * @author Danilo Correa <danilosilva87@gmail.com>
  11. * @author Henrique Moody <henriquemoody@gmail.com>
  12. * @author Michael Firsikov <michael.firsikov@gmail.com>
  13. */
  14. final class PhoneException extends ValidationException
  15. {
  16. public const FOR_COUNTRY = 'for_country';
  17. public const INTERNATIONAL = 'international';
  18. /**
  19. * {@inheritDoc}
  20. */
  21. protected $defaultTemplates = [
  22. self::MODE_DEFAULT => [
  23. self::INTERNATIONAL => '{{name}} 必须是有效的电话号码',
  24. self::FOR_COUNTRY => '{{name}} 必须是 {{countryName}} 的有效电话号码',
  25. ],
  26. self::MODE_NEGATIVE => [
  27. self::INTERNATIONAL => '{{name}} 不能是有效的电话号码',
  28. self::FOR_COUNTRY => '{{name}} 必须是 {{countryName}} 的有效电话号码',
  29. ],
  30. ];
  31. /**
  32. * {@inheritDoc}
  33. */
  34. protected function chooseTemplate(): string
  35. {
  36. $countryCode = $this->getParam('countryCode');
  37. if (!$countryCode) {
  38. return self::INTERNATIONAL;
  39. }
  40. $countryInfo = new CountryInfo($countryCode);
  41. $this->setParam('countryName', $countryInfo->getCountry());
  42. return self::FOR_COUNTRY;
  43. }
  44. }