|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of the Numverify API Client for PHP. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2024 Eric Sizemore <[email protected]> |
|
9
|
|
|
* (c) 2018-2021 Mark Rogoyski <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* This source file is subject to the MIT license. For the full copyright, |
|
12
|
|
|
* license information, and credits/acknowledgements, please view the LICENSE |
|
13
|
|
|
* and README files that were distributed with this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Numverify\Country; |
|
17
|
|
|
|
|
18
|
|
|
use Countable; |
|
19
|
|
|
use Iterator; |
|
20
|
|
|
use JsonSerializable; |
|
21
|
|
|
use LogicException; |
|
22
|
|
|
|
|
23
|
|
|
use function current; |
|
24
|
|
|
use function key; |
|
25
|
|
|
use function next; |
|
26
|
|
|
use function reset; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Country Collection |
|
30
|
|
|
* Role: Collection of callable countries. |
|
31
|
|
|
* |
|
32
|
|
|
* @implements Iterator<Country> |
|
33
|
|
|
* |
|
34
|
|
|
* @see \Numverify\Tests\Country\CollectionTest |
|
35
|
|
|
*/ |
|
36
|
|
|
class Collection implements Countable, Iterator, JsonSerializable |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var Country[] */ |
|
39
|
|
|
private array $byCountryCode = []; |
|
40
|
|
|
|
|
41
|
|
|
/** @var Country[] */ |
|
42
|
|
|
private array $byName = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Collection constructor. |
|
46
|
|
|
*/ |
|
47
|
16 |
|
public function __construct(Country ...$countries) |
|
48
|
|
|
{ |
|
49
|
16 |
|
foreach ($countries as $country) { |
|
50
|
14 |
|
$this->byCountryCode[$country->getCountryCode()] = $country; |
|
51
|
14 |
|
$this->byName[$country->getCountryName()] = $country; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritDoc |
|
57
|
|
|
* |
|
58
|
|
|
* @psalm-api |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function count(): int |
|
61
|
|
|
{ |
|
62
|
6 |
|
return \count($this->byCountryCode); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritDoc |
|
67
|
|
|
*/ |
|
68
|
7 |
|
public function current(): Country |
|
69
|
|
|
{ |
|
70
|
7 |
|
$country = current($this->byCountryCode); |
|
71
|
|
|
|
|
72
|
7 |
|
if ($country === false) { |
|
73
|
2 |
|
throw new LogicException('Iteration error - current returned false'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
5 |
|
return $country; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Find country by country code. |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function findByCountryCode(string $countryCode): ?Country |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->byCountryCode[$countryCode] ?? null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Find country by name. |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function findByCountryName(string $countryName): ?Country |
|
91
|
|
|
{ |
|
92
|
1 |
|
return $this->byName[$countryName] ?? null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritDoc |
|
97
|
|
|
* |
|
98
|
|
|
* @return object[] |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function jsonSerialize(): array |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->byCountryCode; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritDoc |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function key(): null|int|string |
|
109
|
|
|
{ |
|
110
|
3 |
|
return key($this->byCountryCode); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @inheritDoc |
|
115
|
|
|
*/ |
|
116
|
6 |
|
public function next(): void |
|
117
|
|
|
{ |
|
118
|
6 |
|
next($this->byCountryCode); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @inheritDoc |
|
123
|
|
|
*/ |
|
124
|
5 |
|
public function rewind(): void |
|
125
|
|
|
{ |
|
126
|
5 |
|
reset($this->byCountryCode); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @inheritDoc |
|
131
|
|
|
*/ |
|
132
|
5 |
|
public function valid(): bool |
|
133
|
|
|
{ |
|
134
|
5 |
|
return key($this->byCountryCode) !== null; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|