forked from misd-service-development/phone-number-bundle
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathPhoneNumber.php
153 lines (132 loc) · 4.29 KB
/
PhoneNumber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
* (c) University of Cambridge
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Misd\PhoneNumberBundle\Validator\Constraints;
use Misd\PhoneNumberBundle\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
/**
* Phone number constraint.
*
* @Annotation
* @NamedArgumentConstructor
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class PhoneNumber extends Constraint
{
public const ANY = 'any';
public const FIXED_LINE = 'fixed_line';
public const MOBILE = 'mobile';
public const PAGER = 'pager';
public const PERSONAL_NUMBER = 'personal_number';
public const PREMIUM_RATE = 'premium_rate';
public const SHARED_COST = 'shared_cost';
public const TOLL_FREE = 'toll_free';
public const UAN = 'uan';
public const VOIP = 'voip';
public const VOICEMAIL = 'voicemail';
public const INVALID_PHONE_NUMBER_ERROR = 'ca23f4ca-38f4-4325-9bcc-eb570a4abe7f';
protected const ERROR_NAMES = [
self::INVALID_PHONE_NUMBER_ERROR => 'INVALID_PHONE_NUMBER_ERROR',
];
public ?string $message = null;
/**
* @var string|string[]
*/
public string|array $type = self::ANY;
public ?string $defaultRegion = null;
public ?string $regionPath = null;
public ?int $format = null;
/**
* @param int|null $format Specify the format (\libphonenumber\PhoneNumberFormat::*)
* @param string|string[]|null $type
* @param array<mixed> $options
*/
#[HasNamedArguments]
public function __construct(
?int $format = null,
string|array|null $type = null,
?string $defaultRegion = null,
?string $regionPath = null,
?string $message = null,
?array $groups = null,
$payload = null,
array $options = [],
) {
parent::__construct($options, $groups, $payload);
$this->message = $message ?? $this->message;
$this->format = $format ?? $this->format;
$this->type = $type ?? $this->type;
$this->defaultRegion = $defaultRegion ?? $this->defaultRegion;
$this->regionPath = $regionPath ?? $this->regionPath;
}
/**
* @return string[]
*/
public function getTypes(): array
{
if (\is_array($this->type)) {
return $this->type;
}
return [$this->type];
}
public function getMessage(): string
{
if (null !== $this->message) {
return $this->message;
}
$types = $this->getTypes();
if (1 === \count($types)) {
$typeName = $this->getTypeName($types[0]);
return "This value is not a valid $typeName.";
}
return 'This value is not a valid phone number.';
}
/**
* @return string[]
*/
public function getTypeNames(): array
{
$types = \is_array($this->type) ? $this->type : [$this->type];
$typeNames = [];
foreach ($types as $type) {
$typeNames[] = $this->getTypeName($type);
}
return $typeNames;
}
private function getTypeName(string $type): string
{
switch ($type) {
case self::FIXED_LINE:
return 'fixed-line number';
case self::MOBILE:
return 'mobile number';
case self::PAGER:
return 'pager number';
case self::PERSONAL_NUMBER:
return 'personal number';
case self::PREMIUM_RATE:
return 'premium-rate number';
case self::SHARED_COST:
return 'shared-cost number';
case self::TOLL_FREE:
return 'toll-free number';
case self::UAN:
return 'UAN';
case self::VOIP:
return 'VoIP number';
case self::VOICEMAIL:
return 'voicemail access number';
case self::ANY:
return 'phone number';
}
throw new InvalidArgumentException("Unknown phone number type \"$type\".");
}
}