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\PhoneNumber; |
17
|
|
|
|
18
|
|
|
use Numverify\Exception\NumverifyApiResponseException; |
19
|
|
|
use stdClass; |
20
|
|
|
|
21
|
|
|
use function implode; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* InvalidPhoneNumber |
25
|
|
|
* Role: Value object to represent a phone number that the Numverify returned as invalid. |
26
|
|
|
* |
27
|
|
|
* @see \Numverify\Tests\PhoneNumber\InvalidPhoneNumberTest |
28
|
|
|
*/ |
29
|
|
|
class InvalidPhoneNumber implements PhoneNumberInterface |
30
|
|
|
{ |
31
|
|
|
private const FIELDS = ['valid', 'number']; |
32
|
|
|
|
33
|
|
|
private readonly string $number; |
34
|
|
|
|
35
|
|
|
private readonly bool $valid; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* InvalidPhoneNumber constructor. |
39
|
|
|
* |
40
|
|
|
* @param stdClass&object{valid: bool|string, number: int|string} $validatedPhoneNumber |
41
|
|
|
*/ |
42
|
9 |
|
public function __construct(stdClass $validatedPhoneNumber) |
43
|
|
|
{ |
44
|
9 |
|
$this->verifyPhoneNumberData($validatedPhoneNumber); |
45
|
|
|
|
46
|
7 |
|
$this->valid = (bool) $validatedPhoneNumber->valid; |
47
|
7 |
|
$this->number = (string) $validatedPhoneNumber->number; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Debug info. |
52
|
|
|
* |
53
|
|
|
* @return array{valid: bool, number: string} |
54
|
|
|
*/ |
55
|
1 |
|
public function __debugInfo(): array |
56
|
|
|
{ |
57
|
1 |
|
return $this->jsonSerialize(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
1 |
|
public function __toString(): string |
64
|
|
|
{ |
65
|
1 |
|
return $this->number; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
1 |
|
public function getNumber(): string |
72
|
|
|
{ |
73
|
1 |
|
return $this->number; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
1 |
|
public function isValid(): bool |
80
|
|
|
{ |
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
* |
87
|
|
|
* @return array{valid: bool, number: string} |
88
|
|
|
*/ |
89
|
2 |
|
public function jsonSerialize(): array |
90
|
|
|
{ |
91
|
2 |
|
return [ |
92
|
2 |
|
'valid' => $this->valid, |
93
|
2 |
|
'number' => $this->number, |
94
|
2 |
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Verify the phone number data contains the expected fields. |
99
|
|
|
* |
100
|
|
|
* @throws NumverifyApiResponseException |
101
|
|
|
*/ |
102
|
9 |
|
private function verifyPhoneNumberData(stdClass $phoneNumberData): void |
103
|
|
|
{ |
104
|
9 |
|
$missingFields = []; |
105
|
|
|
|
106
|
9 |
|
foreach (self::FIELDS as $field) { |
107
|
9 |
|
if (!isset($phoneNumberData->$field)) { |
108
|
2 |
|
$missingFields[] = $field; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
9 |
|
if ($missingFields !== []) { |
113
|
2 |
|
throw new NumverifyApiResponseException(\sprintf( |
114
|
2 |
|
'API response does not contain one or more expected fields: %s', |
115
|
2 |
|
implode(', ', $missingFields) |
116
|
2 |
|
)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|