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

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.  * @NamedArgumentConstructor
  18.  */
  19. #[\Attribute(\Attribute::TARGET_PROPERTY)]
  20. class PhoneNumber extends Constraint
  21. {
  22.     public const ANY 'any';
  23.     public const FIXED_LINE 'fixed_line';
  24.     public const MOBILE 'mobile';
  25.     public const PAGER 'pager';
  26.     public const PERSONAL_NUMBER 'personal_number';
  27.     public const PREMIUM_RATE 'premium_rate';
  28.     public const SHARED_COST 'shared_cost';
  29.     public const TOLL_FREE 'toll_free';
  30.     public const UAN 'uan';
  31.     public const VOIP 'voip';
  32.     public const VOICEMAIL 'voicemail';
  33.     public const INVALID_PHONE_NUMBER_ERROR 'ca23f4ca-38f4-4325-9bcc-eb570a4abe7f';
  34.     protected const ERROR_NAMES = [
  35.         self::INVALID_PHONE_NUMBER_ERROR => 'INVALID_PHONE_NUMBER_ERROR',
  36.     ];
  37.     /**
  38.      * @deprecated since PhoneNumberBundle 3.6, use const ERROR_NAMES instead
  39.      */
  40.     protected static $errorNames self::ERROR_NAMES;
  41.     public $message null;
  42.     public $type self::ANY;
  43.     public $defaultRegion null;
  44.     public $regionPath null;
  45.     public $format null;
  46.     /**
  47.      * {@inheritdoc}
  48.      *
  49.      * @param int|array|null    $format Specify the format (\libphonenumber\PhoneNumberFormat::*)
  50.      *                                  or options (an associative array)
  51.      * @param string|array|null $type
  52.      */
  53.     public function __construct($format null$type nullstring $defaultRegion nullstring $regionPath nullstring $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 (=== \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 (=== \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 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. }