1
1
<?php
2
+
2
3
/**
3
4
* PHP Domain Parser: Public Suffix List based URL parsing.
4
5
*
5
6
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
6
7
*
7
8
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
8
- * @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
9
+ *
10
+ * For the full copyright and license information, please view the LICENSE
11
+ * file that was distributed with this source code.
9
12
*/
13
+
10
14
declare (strict_types=1 );
11
15
12
16
namespace Pdp ;
23
27
* valid. The DNS is the proper source for this innormalizeion. If you must use
24
28
* it for this purpose, please do not bake static copies of the PSL into your
25
29
* software with no update mechanism."
26
- *
27
- * @author Jeremy Kendall <[email protected] >
28
- * @author Ignace Nyamagana Butera <[email protected] >
29
30
*/
30
31
final class Domain implements DomainInterface, JsonSerializable
31
32
{
@@ -122,7 +123,7 @@ private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
122
123
*
123
124
* @return PublicSuffix
124
125
*/
125
- private function normalize (PublicSuffix $ subject ): PublicSuffix
126
+ private function normalize ($ subject )
126
127
{
127
128
if (null === $ this ->domain || null === $ subject ->getContent ()) {
128
129
return $ subject ;
@@ -370,14 +371,7 @@ public function toAscii()
370
371
return $ this ;
371
372
}
372
373
373
- $ clone = clone $ this ;
374
- $ clone ->domain = $ domain ;
375
- $ clone ->labels = array_reverse (explode ('. ' , $ clone ->domain ));
376
- $ clone ->publicSuffix = $ this ->publicSuffix ->toAscii ();
377
- $ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
378
- $ clone ->subDomain = $ clone ->setSubDomain ();
379
-
380
- return $ clone ;
374
+ return new self ($ domain , $ this ->publicSuffix );
381
375
}
382
376
383
377
/**
@@ -389,14 +383,7 @@ public function toUnicode()
389
383
return $ this ;
390
384
}
391
385
392
- $ clone = clone $ this ;
393
- $ clone ->domain = $ this ->idnToUnicode ($ this ->domain );
394
- $ clone ->labels = array_reverse (explode ('. ' , $ clone ->domain ));
395
- $ clone ->publicSuffix = $ this ->publicSuffix ->toUnicode ();
396
- $ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
397
- $ clone ->subDomain = $ clone ->setSubDomain ();
398
-
399
- return $ clone ;
386
+ return new self ($ this ->idnToUnicode ($ this ->domain ), $ this ->publicSuffix );
400
387
}
401
388
402
389
/**
@@ -425,12 +412,7 @@ public function resolve($publicSuffix): self
425
412
return $ this ;
426
413
}
427
414
428
- $ clone = clone $ this ;
429
- $ clone ->publicSuffix = $ clone ->setPublicSuffix ($ publicSuffix );
430
- $ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
431
- $ clone ->subDomain = $ clone ->setSubDomain ();
432
-
433
- return $ clone ;
415
+ return new self ($ this ->domain , $ publicSuffix );
434
416
}
435
417
436
418
/**
@@ -460,12 +442,9 @@ public function withSubDomain($subDomain): self
460
442
return $ this ;
461
443
}
462
444
463
- $ clone = clone $ this ;
464
- $ clone ->labels = array_merge (array_slice ($ this ->labels , 0 , count ($ this ->publicSuffix ) + 1 ), iterator_to_array ($ subDomain ));
465
- $ clone ->domain = implode ('. ' , array_reverse ($ clone ->labels ));
466
- $ clone ->subDomain = $ subDomain ->getContent ();
445
+ $ labels = array_merge (array_slice ($ this ->labels , 0 , count ($ this ->publicSuffix ) + 1 ), iterator_to_array ($ subDomain ));
467
446
468
- return $ clone ;
447
+ return new self ( implode ( ' . ' , array_reverse ( array_values ( $ labels ))), $ this -> publicSuffix ) ;
469
448
}
470
449
471
450
/**
@@ -495,13 +474,9 @@ public function withPublicSuffix($publicSuffix): self
495
474
return $ this ;
496
475
}
497
476
498
- $ clone = clone $ this ;
499
- $ clone ->labels = array_merge (iterator_to_array ($ publicSuffix ), array_slice ($ this ->labels , count ($ this ->publicSuffix )));
500
- $ clone ->domain = implode ('. ' , array_reverse ($ clone ->labels ));
501
- $ clone ->publicSuffix = $ publicSuffix ;
502
- $ clone ->registrableDomain = $ this ->labels [count ($ this ->publicSuffix )].'. ' .$ publicSuffix ->getContent ();
477
+ $ labels = array_merge (iterator_to_array ($ publicSuffix ), array_slice ($ this ->labels , count ($ this ->publicSuffix )));
503
478
504
- return $ clone ;
479
+ return new self ( implode ( ' . ' , array_reverse ( array_values ( $ labels ))), $ publicSuffix ) ;
505
480
}
506
481
507
482
/**
@@ -523,15 +498,14 @@ public function withPublicSuffix($publicSuffix): self
523
498
*/
524
499
public function withLabel (int $ key , $ label ): self
525
500
{
526
- if (!$ label instanceof PublicSuffix ) {
527
- $ label = $ this -> normalize ( new PublicSuffix ($ label) );
501
+ if (!$ label instanceof Domain ) {
502
+ $ label = new Domain ($ label );
528
503
}
529
504
530
505
if (1 != count ($ label )) {
531
506
throw new Exception (sprintf ('The label `%s` is invalid ' , (string ) $ label ));
532
507
}
533
508
534
- $ label = (string ) $ label ;
535
509
$ nb_labels = count ($ this ->labels );
536
510
$ offset = filter_var ($ key , FILTER_VALIDATE_INT , ['options ' => ['min_range ' => - $ nb_labels - 1 , 'max_range ' => $ nb_labels ]]);
537
511
if (false === $ offset ) {
@@ -542,29 +516,23 @@ public function withLabel(int $key, $label): self
542
516
$ offset = $ nb_labels + $ offset ;
543
517
}
544
518
545
- if ($ label === ($ this ->labels [$ offset ] ?? null )) {
519
+ if (($ this ->labels [$ offset ] ?? null ) === ( string ) $ label ) {
546
520
return $ this ;
547
521
}
548
522
549
- $ labels = $ this ->labels ;
550
- $ labels [$ offset ] = $ label ;
551
- ksort ($ labels );
552
-
553
- $ clone = clone $ this ;
554
- $ clone ->labels = array_values ($ labels );
555
- $ clone ->domain = implode ('. ' , array_reverse ($ clone ->labels ));
556
- if (null !== $ this ->publicSuffix ->getLabel ($ offset )) {
557
- $ clone ->publicSuffix = new PublicSuffix ();
558
- $ clone ->registrableDomain = null ;
559
- $ clone ->subDomain = null ;
560
-
561
- return $ clone ;
523
+ if (null !== $ this ->domain ) {
524
+ static $ pattern = '/[^\x20-\x7f]/ ' ;
525
+ $ label = !preg_match ($ pattern , $ this ->domain ) ? $ label ->toAscii () : $ label ->toUnicode ();
562
526
}
563
527
564
- $ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
565
- $ clone ->subDomain = $ clone ->setSubDomain ();
528
+ $ labels = $ this ->labels ;
529
+ $ labels [$ offset ] = (string ) $ label ;
530
+ ksort ($ labels );
566
531
567
- return $ clone ;
532
+ return new self (
533
+ implode ('. ' , array_reverse (array_values ($ labels ))),
534
+ null === $ this ->publicSuffix ->getLabel ($ offset ) ? $ this ->publicSuffix : null
535
+ );
568
536
}
569
537
570
538
/**
@@ -594,20 +562,12 @@ public function withoutLabel(int $key): self
594
562
$ offset = $ nb_labels + $ offset ;
595
563
}
596
564
597
- $ clone = clone $ this ;
598
- unset($ clone ->labels [$ offset ]);
599
- $ clone ->domain = implode ('. ' , array_reverse ($ clone ->labels ));
600
- if (null !== $ this ->publicSuffix ->getLabel ($ offset )) {
601
- $ clone ->publicSuffix = new PublicSuffix ();
602
- $ clone ->registrableDomain = null ;
603
- $ clone ->subDomain = null ;
604
-
605
- return $ clone ;
606
- }
607
-
608
- $ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
609
- $ clone ->subDomain = $ clone ->setSubDomain ();
565
+ $ labels = $ this ->labels ;
566
+ unset($ labels [$ offset ]);
610
567
611
- return $ clone ;
568
+ return new self (
569
+ implode ('. ' , array_reverse (array_values ($ labels ))),
570
+ null === $ this ->publicSuffix ->getLabel ($ offset ) ? $ this ->publicSuffix : null
571
+ );
612
572
}
613
573
}
0 commit comments