5
5
namespace Pdp ;
6
6
7
7
use Iterator ;
8
- use TypeError ;
8
+ use Stringable ;
9
9
use function array_count_values ;
10
10
use function array_keys ;
11
11
use function array_reverse ;
14
14
use function count ;
15
15
use function explode ;
16
16
use function filter_var ;
17
- use function gettype ;
18
17
use function implode ;
19
18
use function in_array ;
20
- use function is_object ;
21
- use function is_scalar ;
22
19
use function ksort ;
23
- use function method_exists ;
24
20
use function preg_match ;
25
21
use function rawurldecode ;
26
22
use function strtolower ;
@@ -44,19 +40,11 @@ final class Domain implements DomainName
44
40
45
41
private const REGEXP_URI_DELIMITERS = '/[:\/?#\[\]@ ]/ ' ;
46
42
47
- /**
48
- * @var array<int, string>
49
- */
43
+ /** @var array<int, string> */
50
44
private array $ labels ;
51
-
52
45
private ?string $ domain ;
53
46
54
- private string $ type ;
55
-
56
- /**
57
- * @param null|mixed $domain
58
- */
59
- private function __construct (string $ type , $ domain )
47
+ private function __construct (private string $ type , DomainNameProvider |Host |Stringable |string |int |null $ domain )
60
48
{
61
49
$ this ->type = $ type ;
62
50
$ this ->domain = $ this ->parseDomain ($ domain );
@@ -71,26 +59,17 @@ public static function __set_state(array $properties): self
71
59
return new self ($ properties ['type ' ], $ properties ['domain ' ]);
72
60
}
73
61
74
- /**
75
- * @param null|mixed $domain
76
- */
77
- public static function fromIDNA2003 ($ domain ): self
62
+ public static function fromIDNA2003 (DomainNameProvider |Host |Stringable |string |int |null $ domain ): self
78
63
{
79
64
return new self (self ::IDNA_2003 , $ domain );
80
65
}
81
66
82
- /**
83
- * @param null|mixed $domain
84
- */
85
- public static function fromIDNA2008 ($ domain ): self
67
+ public static function fromIDNA2008 (DomainNameProvider |Host |Stringable |string |int |null $ domain ): self
86
68
{
87
69
return new self (self ::IDNA_2008 , $ domain );
88
70
}
89
71
90
- /**
91
- * @param mixed $domain a domain
92
- */
93
- private function parseDomain ($ domain ): ?string
72
+ private function parseDomain (DomainNameProvider |Host |Stringable |string |int |null $ domain ): ?string
94
73
{
95
74
if ($ domain instanceof DomainNameProvider) {
96
75
$ domain = $ domain ->domain ();
@@ -110,25 +89,19 @@ private function parseDomain($domain): ?string
110
89
*
111
90
* For example: parse('wWw.uLb.Ac.be') should return ['www.ulb.ac.be', ['be', 'ac', 'ulb', 'www']];.
112
91
*
113
- * @param mixed $domain a domain
114
- *
115
92
* @throws SyntaxError If the host is not a domain
116
93
* @throws SyntaxError If the domain is not a host
117
94
*/
118
- private function parseValue ($ domain ): ?string
95
+ private function parseValue (Stringable | string | int | null $ domain ): ?string
119
96
{
120
97
if (null === $ domain ) {
121
98
return null ;
122
99
}
123
100
124
- if (is_object ( $ domain) && method_exists ( $ domain , ' __toString ' ) ) {
101
+ if ($ domain instanceof Stringable ) {
125
102
$ domain = (string ) $ domain ;
126
103
}
127
104
128
- if (!is_scalar ($ domain )) {
129
- throw new TypeError ('The domain must be a string, a stringable object, a Host object or NULL; ` ' .gettype ($ domain ).'` given. ' );
130
- }
131
-
132
105
$ domain = (string ) $ domain ;
133
106
if ('' === $ domain ) {
134
107
return '' ;
@@ -235,10 +208,6 @@ public function labels(): array
235
208
return $ this ->labels ;
236
209
}
237
210
238
- /**
239
- * @psalm-suppress MoreSpecificReturnType
240
- * @psalm-suppress LessSpecificReturnStatement
241
- */
242
211
public function toAscii (): self
243
212
{
244
213
if (null === $ this ->domain ) {
@@ -253,10 +222,6 @@ public function toAscii(): self
253
222
return new self ($ this ->type , $ domain );
254
223
}
255
224
256
- /**
257
- * @psalm-suppress MoreSpecificReturnType
258
- * @psalm-suppress LessSpecificReturnStatement
259
- */
260
225
public function toUnicode (): self
261
226
{
262
227
if (null === $ this ->domain ) {
@@ -273,12 +238,8 @@ public function toUnicode(): self
273
238
274
239
/**
275
240
* Filter a subdomain to update the domain part.
276
- *
277
- * @param string|object|null $domain a domain
278
- *
279
- * @throws TypeError if the domain can not be converted
280
241
*/
281
- private function normalize ($ domain ): ?string
242
+ private function normalize (DomainNameProvider | Host | Stringable | string | null $ domain ): ?string
282
243
{
283
244
if ($ domain instanceof DomainNameProvider) {
284
245
$ domain = $ domain ->domain ();
@@ -292,33 +253,32 @@ private function normalize($domain): ?string
292
253
return $ domain ;
293
254
}
294
255
295
- if ((!is_string ($ domain ) && !method_exists ($ domain , '__toString ' ))) {
296
- throw new TypeError ('The label must be a ' .Host::class.', a stringable object or a string, ` ' .gettype ($ domain ).'` given. ' );
297
- }
298
-
299
256
$ domain = (string ) $ domain ;
300
- if (null === $ this ->domain ) {
301
- return $ domain ;
302
- }
303
-
304
- if (!$ this ->isAscii ()) {
305
- return $ this ->domainToUnicode ($ domain );
306
- }
307
257
308
- return $ this ->domainToAscii ($ domain );
258
+ return match (true ) {
259
+ null === $ this ->domain => $ domain ,
260
+ $ this ->isAscii () => $ this ->domainToAscii ($ domain ),
261
+ default => $ this ->domainToUnicode ($ domain ),
262
+ };
309
263
}
310
264
311
- public function prepend ($ label ): self
265
+ /**
266
+ * @throws CannotProcessHost
267
+ */
268
+ public function prepend (DomainNameProvider |Host |string |Stringable |null $ label ): self
312
269
{
313
270
return $ this ->withLabel (count ($ this ->labels ), $ label );
314
271
}
315
272
316
- public function append ($ label ): self
273
+ /**
274
+ * @throws CannotProcessHost
275
+ */
276
+ public function append (DomainNameProvider |Host |string |Stringable |null $ label ): self
317
277
{
318
278
return $ this ->withLabel (- count ($ this ->labels ) - 1 , $ label );
319
279
}
320
280
321
- public function withLabel (int $ key , $ label ): self
281
+ public function withLabel (int $ key , DomainNameProvider | Host | string | Stringable | null $ label ): self
322
282
{
323
283
$ nbLabels = count ($ this ->labels );
324
284
if ($ key < - $ nbLabels - 1 || $ key > $ nbLabels ) {
0 commit comments