vendor/odolbeau/phone-number-bundle/src/Validator/Constraints/PhoneNumber.php line 65

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony2 PhoneNumberBundle.
  4. *
  5. * (c) University of Cambridge
  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 Misd\PhoneNumberBundle\Validator\Constraints;
  11. use Misd\PhoneNumberBundle\Exception\InvalidArgumentException;
  12. use Symfony\Component\Validator\Attribute\HasNamedArguments;
  13. use Symfony\Component\Validator\Constraint;
  14. /**
  15. * Phone number constraint.
  16. *
  17. * @Annotation
  18. * @NamedArgumentConstructor
  19. */
  20. #[\Attribute(\Attribute::TARGET_PROPERTY)]
  21. class PhoneNumber extends Constraint
  22. {
  23. public const ANY = 'any';
  24. public const FIXED_LINE = 'fixed_line';
  25. public const MOBILE = 'mobile';
  26. public const PAGER = 'pager';
  27. public const PERSONAL_NUMBER = 'personal_number';
  28. public const PREMIUM_RATE = 'premium_rate';
  29. public const SHARED_COST = 'shared_cost';
  30. public const TOLL_FREE = 'toll_free';
  31. public const UAN = 'uan';
  32. public const VOIP = 'voip';
  33. public const VOICEMAIL = 'voicemail';
  34. public const INVALID_PHONE_NUMBER_ERROR = 'ca23f4ca-38f4-4325-9bcc-eb570a4abe7f';
  35. protected const ERROR_NAMES = [
  36. self::INVALID_PHONE_NUMBER_ERROR => 'INVALID_PHONE_NUMBER_ERROR',
  37. ];
  38. /**
  39. * @deprecated since PhoneNumberBundle 3.6, use const ERROR_NAMES instead
  40. */
  41. protected static $errorNames = self::ERROR_NAMES;
  42. public $message;
  43. public $type = self::ANY;
  44. public $defaultRegion;
  45. public $regionPath;
  46. public $format;
  47. /**
  48. * @param int|array|null $format Specify the format (\libphonenumber\PhoneNumberFormat::*)
  49. * or options (an associative array)
  50. * @param string|array|null $type
  51. */
  52. #[HasNamedArguments]
  53. public function __construct($format = null, $type = null, string $defaultRegion = null, string $regionPath = null, string $message = null, array $groups = null, $payload = null, array $options = [])
  54. {
  55. if (\is_array($format)) {
  56. @trigger_error('Usage of the argument $format to specify options is deprecated and will be removed in 4.0. Use "$option" argument instead.', \E_USER_DEPRECATED);
  57. $options = array_merge($format, $options);
  58. } else {
  59. $phoneFormat = $format;
  60. }
  61. parent::__construct($options, $groups, $payload);
  62. $this->message = $message ?? $this->message;
  63. $this->format = $phoneFormat ?? $this->format;
  64. $this->type = $type ?? $this->type;
  65. $this->defaultRegion = $defaultRegion ?? $this->defaultRegion;
  66. $this->regionPath = $regionPath ?? $this->regionPath;
  67. }
  68. public function getType(): ?string
  69. {
  70. @trigger_error(__METHOD__.' is deprecated and will be removed in 4.0. Use "getTypes" instead.', \E_USER_DEPRECATED);
  71. $types = $this->getTypes();
  72. if (0 === \count($types)) {
  73. return null;
  74. }
  75. return reset($types);
  76. }
  77. public function getTypes(): array
  78. {
  79. if (\is_array($this->type)) {
  80. return $this->type;
  81. }
  82. return [$this->type];
  83. }
  84. public function getMessage(): string
  85. {
  86. if (null !== $this->message) {
  87. return $this->message;
  88. }
  89. $types = $this->getTypes();
  90. if (1 === \count($types)) {
  91. $typeName = $this->getTypeName($types[0]);
  92. return "This value is not a valid $typeName.";
  93. }
  94. return 'This value is not a valid phone number.';
  95. }
  96. public function getTypeNames(): array
  97. {
  98. $types = \is_array($this->type) ? $this->type : [$this->type];
  99. $typeNames = [];
  100. foreach ($types as $type) {
  101. $typeNames[] = $this->getTypeName($type);
  102. }
  103. return $typeNames;
  104. }
  105. private function getTypeName(string $type): string
  106. {
  107. switch ($type) {
  108. case self::FIXED_LINE:
  109. return 'fixed-line number';
  110. case self::MOBILE:
  111. return 'mobile number';
  112. case self::PAGER:
  113. return 'pager number';
  114. case self::PERSONAL_NUMBER:
  115. return 'personal number';
  116. case self::PREMIUM_RATE:
  117. return 'premium-rate number';
  118. case self::SHARED_COST:
  119. return 'shared-cost number';
  120. case self::TOLL_FREE:
  121. return 'toll-free number';
  122. case self::UAN:
  123. return 'UAN';
  124. case self::VOIP:
  125. return 'VoIP number';
  126. case self::VOICEMAIL:
  127. return 'voicemail access number';
  128. case self::ANY:
  129. return 'phone number';
  130. }
  131. throw new InvalidArgumentException("Unknown phone number type \"$type\".");
  132. }
  133. }