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

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