1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace libphonenumber;
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* Number Format
|
|
|
9 |
* @interal
|
|
|
10 |
* @phpstan-type NumberFormatArray array{pattern:string|null,format:string|null,leadingDigitsPatterns:array<string>,nationalPrefixFormattingRule?:string,domesticCarrierCodeFormattingRule?:string,nationalPrefixOptionalWhenFormatting?:bool}
|
|
|
11 |
*/
|
|
|
12 |
class NumberFormat
|
|
|
13 |
{
|
|
|
14 |
protected ?string $pattern;
|
|
|
15 |
protected bool $hasPattern = false;
|
|
|
16 |
protected ?string $format;
|
|
|
17 |
protected bool $hasFormat = false;
|
|
|
18 |
/**
|
|
|
19 |
* @var array<string>
|
|
|
20 |
*/
|
|
|
21 |
protected array $leadingDigitsPattern = [];
|
|
|
22 |
protected string $nationalPrefixFormattingRule = '';
|
|
|
23 |
protected bool $hasNationalPrefixFormattingRule = false;
|
|
|
24 |
protected bool $nationalPrefixOptionalWhenFormatting = false;
|
|
|
25 |
protected bool $hasNationalPrefixOptionalWhenFormatting = false;
|
|
|
26 |
protected string $domesticCarrierCodeFormattingRule = '';
|
|
|
27 |
protected bool $hasDomesticCarrierCodeFormattingRule = false;
|
|
|
28 |
|
|
|
29 |
public function __construct()
|
|
|
30 |
{
|
|
|
31 |
$this->clear();
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
*/
|
|
|
36 |
public function clear(): NumberFormat
|
|
|
37 |
{
|
|
|
38 |
$this->hasPattern = false;
|
|
|
39 |
$this->pattern = null;
|
|
|
40 |
|
|
|
41 |
$this->hasFormat = false;
|
|
|
42 |
$this->format = null;
|
|
|
43 |
|
|
|
44 |
$this->leadingDigitsPattern = [];
|
|
|
45 |
|
|
|
46 |
$this->hasNationalPrefixFormattingRule = false;
|
|
|
47 |
$this->nationalPrefixFormattingRule = '';
|
|
|
48 |
|
|
|
49 |
$this->hasNationalPrefixOptionalWhenFormatting = false;
|
|
|
50 |
$this->nationalPrefixOptionalWhenFormatting = false;
|
|
|
51 |
|
|
|
52 |
$this->hasDomesticCarrierCodeFormattingRule = false;
|
|
|
53 |
$this->domesticCarrierCodeFormattingRule = '';
|
|
|
54 |
|
|
|
55 |
return $this;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function hasPattern(): bool
|
|
|
59 |
{
|
|
|
60 |
return $this->hasPattern;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public function getPattern(): ?string
|
|
|
64 |
{
|
|
|
65 |
return $this->pattern;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public function setPattern(string $value): NumberFormat
|
|
|
69 |
{
|
|
|
70 |
$this->hasPattern = true;
|
|
|
71 |
$this->pattern = $value;
|
|
|
72 |
|
|
|
73 |
return $this;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function hasNationalPrefixOptionalWhenFormatting(): bool
|
|
|
77 |
{
|
|
|
78 |
return $this->hasNationalPrefixOptionalWhenFormatting;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function getNationalPrefixOptionalWhenFormatting(): bool
|
|
|
82 |
{
|
|
|
83 |
return $this->nationalPrefixOptionalWhenFormatting;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
public function setNationalPrefixOptionalWhenFormatting(bool $nationalPrefixOptionalWhenFormatting): void
|
|
|
87 |
{
|
|
|
88 |
$this->hasNationalPrefixOptionalWhenFormatting = true;
|
|
|
89 |
$this->nationalPrefixOptionalWhenFormatting = $nationalPrefixOptionalWhenFormatting;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public function hasFormat(): bool
|
|
|
93 |
{
|
|
|
94 |
return $this->hasFormat;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public function getFormat(): ?string
|
|
|
98 |
{
|
|
|
99 |
return $this->format;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public function setFormat(string $value): NumberFormat
|
|
|
103 |
{
|
|
|
104 |
$this->hasFormat = true;
|
|
|
105 |
$this->format = $value;
|
|
|
106 |
|
|
|
107 |
return $this;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* @return string[]
|
|
|
112 |
*/
|
|
|
113 |
public function leadingDigitPatterns(): array
|
|
|
114 |
{
|
|
|
115 |
return $this->leadingDigitsPattern;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
public function leadingDigitsPatternSize(): int
|
|
|
119 |
{
|
|
|
120 |
return count($this->leadingDigitsPattern);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public function getLeadingDigitsPattern(int $index): string
|
|
|
124 |
{
|
|
|
125 |
return $this->leadingDigitsPattern[$index];
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
public function addLeadingDigitsPattern(string $value): NumberFormat
|
|
|
129 |
{
|
|
|
130 |
$this->leadingDigitsPattern[] = $value;
|
|
|
131 |
|
|
|
132 |
return $this;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public function hasNationalPrefixFormattingRule(): bool
|
|
|
136 |
{
|
|
|
137 |
return $this->hasNationalPrefixFormattingRule;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public function getNationalPrefixFormattingRule(): string
|
|
|
141 |
{
|
|
|
142 |
return $this->nationalPrefixFormattingRule;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
public function setNationalPrefixFormattingRule(string $value): NumberFormat
|
|
|
146 |
{
|
|
|
147 |
$this->hasNationalPrefixFormattingRule = true;
|
|
|
148 |
$this->nationalPrefixFormattingRule = $value;
|
|
|
149 |
|
|
|
150 |
return $this;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
public function clearNationalPrefixFormattingRule(): NumberFormat
|
|
|
154 |
{
|
|
|
155 |
$this->nationalPrefixFormattingRule = '';
|
|
|
156 |
|
|
|
157 |
return $this;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
public function hasDomesticCarrierCodeFormattingRule(): bool
|
|
|
161 |
{
|
|
|
162 |
return $this->hasDomesticCarrierCodeFormattingRule;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
public function getDomesticCarrierCodeFormattingRule(): string
|
|
|
166 |
{
|
|
|
167 |
return $this->domesticCarrierCodeFormattingRule;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
public function setDomesticCarrierCodeFormattingRule(string $value): NumberFormat
|
|
|
171 |
{
|
|
|
172 |
$this->hasDomesticCarrierCodeFormattingRule = true;
|
|
|
173 |
$this->domesticCarrierCodeFormattingRule = $value;
|
|
|
174 |
|
|
|
175 |
return $this;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
public function mergeFrom(NumberFormat $other): NumberFormat
|
|
|
179 |
{
|
|
|
180 |
if ($other->hasPattern()) {
|
|
|
181 |
$this->setPattern($other->getPattern());
|
|
|
182 |
}
|
|
|
183 |
if ($other->hasFormat()) {
|
|
|
184 |
$this->setFormat($other->getFormat());
|
|
|
185 |
}
|
|
|
186 |
$leadingDigitsPatternSize = $other->leadingDigitsPatternSize();
|
|
|
187 |
for ($i = 0; $i < $leadingDigitsPatternSize; $i++) {
|
|
|
188 |
$this->addLeadingDigitsPattern($other->getLeadingDigitsPattern($i));
|
|
|
189 |
}
|
|
|
190 |
if ($other->hasNationalPrefixFormattingRule()) {
|
|
|
191 |
$this->setNationalPrefixFormattingRule($other->getNationalPrefixFormattingRule());
|
|
|
192 |
}
|
|
|
193 |
if ($other->hasDomesticCarrierCodeFormattingRule()) {
|
|
|
194 |
$this->setDomesticCarrierCodeFormattingRule($other->getDomesticCarrierCodeFormattingRule());
|
|
|
195 |
}
|
|
|
196 |
if ($other->hasNationalPrefixOptionalWhenFormatting()) {
|
|
|
197 |
$this->setNationalPrefixOptionalWhenFormatting($other->getNationalPrefixOptionalWhenFormatting());
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
return $this;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* @internal
|
|
|
205 |
* @return NumberFormatArray
|
|
|
206 |
*/
|
|
|
207 |
public function toArray(): array
|
|
|
208 |
{
|
|
|
209 |
$output = [];
|
|
|
210 |
$output['pattern'] = $this->getPattern();
|
|
|
211 |
$output['format'] = $this->getFormat();
|
|
|
212 |
|
|
|
213 |
$output['leadingDigitsPatterns'] = $this->leadingDigitPatterns();
|
|
|
214 |
|
|
|
215 |
if ($this->hasNationalPrefixFormattingRule()) {
|
|
|
216 |
$output['nationalPrefixFormattingRule'] = $this->getNationalPrefixFormattingRule();
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
if ($this->hasDomesticCarrierCodeFormattingRule()) {
|
|
|
220 |
$output['domesticCarrierCodeFormattingRule'] = $this->getDomesticCarrierCodeFormattingRule();
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
if ($this->hasNationalPrefixOptionalWhenFormatting() && $this->getNationalPrefixOptionalWhenFormatting() !== false) {
|
|
|
224 |
$output['nationalPrefixOptionalWhenFormatting'] = $this->getNationalPrefixOptionalWhenFormatting();
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
return $output;
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
/**
|
|
|
231 |
* @internal
|
|
|
232 |
* @param NumberFormatArray $input
|
|
|
233 |
*/
|
|
|
234 |
public function fromArray(array $input): void
|
|
|
235 |
{
|
|
|
236 |
$this->setPattern($input['pattern']);
|
|
|
237 |
$this->setFormat($input['format']);
|
|
|
238 |
foreach ($input['leadingDigitsPatterns'] as $leadingDigitsPattern) {
|
|
|
239 |
$this->addLeadingDigitsPattern($leadingDigitsPattern);
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
if (isset($input['nationalPrefixFormattingRule']) && $input['nationalPrefixFormattingRule'] !== '') {
|
|
|
243 |
$this->setNationalPrefixFormattingRule($input['nationalPrefixFormattingRule']);
|
|
|
244 |
}
|
|
|
245 |
if (isset($input['domesticCarrierCodeFormattingRule']) && $input['domesticCarrierCodeFormattingRule'] !== '') {
|
|
|
246 |
$this->setDomesticCarrierCodeFormattingRule($input['domesticCarrierCodeFormattingRule']);
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
if (isset($input['nationalPrefixOptionalWhenFormatting'])) {
|
|
|
250 |
$this->setNationalPrefixOptionalWhenFormatting($input['nationalPrefixOptionalWhenFormatting']);
|
|
|
251 |
}
|
|
|
252 |
}
|
|
|
253 |
}
|