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

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