Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace libphonenumber;
6
 
7
/**
8
 * Phone Number Description
9
 * @interal
10
 * @phpstan-type PhoneNumberDescArray array{pattern?:string,example?:string,posLength?:int[],posLengthLocal?:int[]}
11
 */
12
class PhoneNumberDesc
13
{
14
    protected bool $hasNationalNumberPattern = false;
15
    protected string $nationalNumberPattern = '';
16
    protected bool $hasExampleNumber = false;
17
    protected string $exampleNumber = '';
18
    /**
19
     * @var int[]
20
     */
21
    protected array $possibleLength = [];
22
    /**
23
     * @var int[]
24
     */
25
    protected array $possibleLengthLocalOnly = [];
26
 
27
    public function __construct()
28
    {
29
        $this->clear();
30
    }
31
 
32
    public function clear(): PhoneNumberDesc
33
    {
34
        $this->clearNationalNumberPattern();
35
        $this->clearPossibleLength();
36
        $this->clearPossibleLengthLocalOnly();
37
        $this->clearExampleNumber();
38
 
39
        return $this;
40
    }
41
 
42
    /**
43
     * @return int[]
44
     */
45
    public function getPossibleLength(): array
46
    {
47
        return $this->possibleLength;
48
    }
49
 
50
    /**
51
     * @param int[] $possibleLength
52
     */
53
    public function setPossibleLength(array $possibleLength): void
54
    {
55
        $this->possibleLength = $possibleLength;
56
    }
57
 
58
    public function addPossibleLength(int $possibleLength): void
59
    {
60
        if (!in_array($possibleLength, $this->possibleLength)) {
61
            $this->possibleLength[] = $possibleLength;
62
        }
63
    }
64
 
65
    public function clearPossibleLength(): void
66
    {
67
        $this->possibleLength = [];
68
    }
69
 
70
    /**
71
     * @return int[]
72
     */
73
    public function getPossibleLengthLocalOnly(): array
74
    {
75
        return $this->possibleLengthLocalOnly;
76
    }
77
 
78
    /**
79
     * @param int[] $possibleLengthLocalOnly
80
     */
81
    public function setPossibleLengthLocalOnly(array $possibleLengthLocalOnly): void
82
    {
83
        $this->possibleLengthLocalOnly = $possibleLengthLocalOnly;
84
    }
85
 
86
    public function addPossibleLengthLocalOnly(int $possibleLengthLocalOnly): void
87
    {
88
        if (!in_array($possibleLengthLocalOnly, $this->possibleLengthLocalOnly)) {
89
            $this->possibleLengthLocalOnly[] = $possibleLengthLocalOnly;
90
        }
91
    }
92
 
93
    public function clearPossibleLengthLocalOnly(): void
94
    {
95
        $this->possibleLengthLocalOnly = [];
96
    }
97
 
98
    public function hasNationalNumberPattern(): bool
99
    {
100
        return $this->hasNationalNumberPattern;
101
    }
102
 
103
    public function getNationalNumberPattern(): string
104
    {
105
        return $this->nationalNumberPattern;
106
    }
107
 
108
    public function setNationalNumberPattern(string $value): PhoneNumberDesc
109
    {
110
        $this->hasNationalNumberPattern = true;
111
        $this->nationalNumberPattern = $value;
112
 
113
        return $this;
114
    }
115
 
116
    public function clearNationalNumberPattern(): PhoneNumberDesc
117
    {
118
        $this->hasNationalNumberPattern = false;
119
        $this->nationalNumberPattern = '';
120
        return $this;
121
    }
122
 
123
    public function hasExampleNumber(): bool
124
    {
125
        return $this->hasExampleNumber;
126
    }
127
 
128
    public function getExampleNumber(): string
129
    {
130
        return $this->exampleNumber;
131
    }
132
 
133
    public function setExampleNumber(string $value): PhoneNumberDesc
134
    {
135
        $this->hasExampleNumber = true;
136
        $this->exampleNumber = $value;
137
 
138
        return $this;
139
    }
140
 
141
    public function clearExampleNumber(): self
142
    {
143
        $this->hasExampleNumber = false;
144
        $this->exampleNumber = '';
145
 
146
        return $this;
147
    }
148
 
149
    public function mergeFrom(PhoneNumberDesc $other): self
150
    {
151
        if ($other->hasNationalNumberPattern()) {
152
            $this->setNationalNumberPattern($other->getNationalNumberPattern());
153
        }
154
        if ($other->hasExampleNumber()) {
155
            $this->setExampleNumber($other->getExampleNumber());
156
        }
157
        $this->setPossibleLength($other->getPossibleLength());
158
        $this->setPossibleLengthLocalOnly($other->getPossibleLengthLocalOnly());
159
 
160
        return $this;
161
    }
162
 
163
    public function exactlySameAs(PhoneNumberDesc $other): bool
164
    {
165
        return $this->nationalNumberPattern === $other->nationalNumberPattern &&
166
        $this->exampleNumber === $other->exampleNumber;
167
    }
168
 
169
    /**
170
     * @internal
171
     * @return PhoneNumberDescArray
172
     */
173
    public function toArray(): array
174
    {
175
        $data = [];
176
        if ($this->hasNationalNumberPattern()) {
177
            $data['pattern'] = $this->getNationalNumberPattern();
178
        }
179
        if ($this->hasExampleNumber()) {
180
            $data['example'] = $this->getExampleNumber();
181
        }
182
 
183
        $possibleLength = $this->getPossibleLength();
184
        if (!empty($possibleLength)) {
185
            $data['posLength'] = $possibleLength;
186
        }
187
 
188
        $possibleLengthLocalOnly = $this->getPossibleLengthLocalOnly();
189
        if (!empty($possibleLengthLocalOnly)) {
190
            $data['posLengthLocal'] = $possibleLengthLocalOnly;
191
        }
192
 
193
        return $data;
194
    }
195
 
196
    /**
197
     * @internal
198
     * @param PhoneNumberDescArray $input
199
     */
200
    public function fromArray(array $input): static
201
    {
202
        if (isset($input['pattern']) && $input['pattern'] !== '') {
203
            $this->setNationalNumberPattern($input['pattern']);
204
        }
205
        if (isset($input['example']) && $input['example'] !== '') {
206
            $this->setExampleNumber($input['example']);
207
        }
208
        if (isset($input['posLength'])) {
209
            $this->setPossibleLength($input['posLength']);
210
        }
211
        if (isset($input['posLengthLocal'])) {
212
            $this->setPossibleLengthLocalOnly($input['posLengthLocal']);
213
        }
214
 
215
        return $this;
216
    }
217
}