ValidPhoneNumber::getNumber()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
 * ValidPhoneNumber.
25
 *
26
 * Role: Value object to represent a phone number that the Numverify returned as valid.
27
 *
28
 * @phpstan-import-type ValidPhoneNumberObject from Factory
29
 *
30
 * @phpstan-type ValidNumberArray = array{
31
 *     valid: bool,
32
 *     number: string,
33
 *     localFormat: string,
34
 *     internationalFormat: string,
35
 *     countryPrefix: string,
36
 *     countryCode: string,
37
 *     countryName: string,
38
 *     location: string,
39
 *     carrier: string,
40
 *     lineType: string
41
 * }
42
 *
43
 * @see \Numverify\Tests\PhoneNumber\ValidPhoneNumberTest
44
 */
45
46
class ValidPhoneNumber implements PhoneNumberInterface
47
{
48
    /**
49
     * @var string[]
50
     */
51
    private const FIELDS = [
52
        'valid', 'number', 'local_format', 'international_format', 'country_prefix',
53
        'country_code', 'country_name', 'location', 'carrier', 'line_type',
54
    ];
55
56
    private readonly string $carrier;
57
58
    private readonly string $countryCode;
59
60
    private readonly string $countryName;
61
62
    private readonly string $countryPrefix;
63
64
    private readonly string $internationalFormat;
65
66
    private readonly string $lineType;
67
68
    private readonly string $localFormat;
69
70
    private readonly string $location;
71
72
    private readonly string $number;
73
74
    private readonly bool $valid;
75
76
    /**
77
     * ValidPhoneNumber constructor.
78
     *
79
     * @param ValidPhoneNumberObject $validatedPhoneNumberData
80
     */
81 21
    public function __construct(stdClass $validatedPhoneNumberData)
82
    {
83 21
        $this->verifyPhoneNumberData($validatedPhoneNumberData);
84
85 9
        $this->valid               = (bool) $validatedPhoneNumberData->valid;
86 9
        $this->number              = (string) $validatedPhoneNumberData->number;
87 9
        $this->localFormat         = $validatedPhoneNumberData->local_format;
88 9
        $this->internationalFormat = $validatedPhoneNumberData->international_format;
89 9
        $this->countryPrefix       = $validatedPhoneNumberData->country_prefix;
90 9
        $this->countryCode         = $validatedPhoneNumberData->country_code;
91 9
        $this->countryName         = $validatedPhoneNumberData->country_name;
92 9
        $this->location            = $validatedPhoneNumberData->location;
93 9
        $this->carrier             = $validatedPhoneNumberData->carrier;
94 9
        $this->lineType            = $validatedPhoneNumberData->line_type;
95
    }
96
97
    /**
98
     * Debug info.
99
     *
100
     * @return ValidNumberArray
101
     */
102 1
    public function __debugInfo(): array
103
    {
104 1
        return $this->jsonSerialize();
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110 1
    public function __toString(): string
111
    {
112 1
        return $this->number;
113
    }
114
115
    /**
116
     * Get carrier.
117
     */
118 1
    public function getCarrier(): string
119
    {
120 1
        return $this->carrier;
121
    }
122
123
    /**
124
     * Get country code.
125
     */
126 1
    public function getCountryCode(): string
127
    {
128 1
        return $this->countryCode;
129
    }
130
131
    /**
132
     * Get country name.
133
     */
134 1
    public function getCountryName(): string
135
    {
136 1
        return $this->countryName;
137
    }
138
139
    /**
140
     * Get country prefix.
141
     */
142 1
    public function getCountryPrefix(): string
143
    {
144 1
        return $this->countryPrefix;
145
    }
146
147
    /**
148
     * Get international format.
149
     */
150 1
    public function getInternationalFormat(): string
151
    {
152 1
        return $this->internationalFormat;
153
    }
154
155
    /**
156
     * Get line type.
157
     */
158 1
    public function getLineType(): string
159
    {
160 1
        return $this->lineType;
161
    }
162
163
    /**
164
     * Get local format.
165
     */
166 1
    public function getLocalFormat(): string
167
    {
168 1
        return $this->localFormat;
169
    }
170
171
    /**
172
     * Get location.
173
     */
174 1
    public function getLocation(): string
175
    {
176 1
        return $this->location;
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182 1
    public function getNumber(): string
183
    {
184 1
        return $this->number;
185
    }
186
187
    /**
188
     * @inheritDoc
189
     */
190 1
    public function isValid(): bool
191
    {
192 1
        return true;
193
    }
194
195
    /**
196
     * @inheritDoc
197
     *
198
     * @return ValidNumberArray
199
     */
200 2
    public function jsonSerialize(): array
201
    {
202 2
        return [
203 2
            'valid'               => $this->valid,
204 2
            'number'              => $this->number,
205 2
            'localFormat'         => $this->localFormat,
206 2
            'internationalFormat' => $this->internationalFormat,
207 2
            'countryPrefix'       => $this->countryPrefix,
208 2
            'countryCode'         => $this->countryCode,
209 2
            'countryName'         => $this->countryName,
210 2
            'location'            => $this->location,
211 2
            'carrier'             => $this->carrier,
212 2
            'lineType'            => $this->lineType,
213 2
        ];
214
    }
215
216
    /**
217
     * Verify the phone number data contains the expected fields.
218
     *
219
     * @throws NumverifyApiResponseException
220
     */
221 21
    private function verifyPhoneNumberData(stdClass $phoneNumberData): void
222
    {
223 21
        $missingFields = [];
224
225 21
        foreach (self::FIELDS as $field) {
226 21
            if (!isset($phoneNumberData->$field)) {
227 12
                $missingFields[] = $field;
228
            }
229
        }
230
231 21
        if ($missingFields !== []) {
232 12
            throw new NumverifyApiResponseException(\sprintf(
233 12
                'API response does not contain one or more expected fields: %s',
234 12
                implode(', ', $missingFields)
235 12
            ));
236
        }
237
    }
238
}
239