@@ -99,11 +99,7 @@ private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
99
99
throw new Exception (sprintf ('The domain `%s` can not contain a public suffix ' , $ this ->domain ));
100
100
}
101
101
102
- static $ pattern = '/[^\x20-\x7f]/ ' ;
103
- if (preg_match ($ pattern , $ this ->domain )) {
104
- $ publicSuffix = $ publicSuffix ->toUnicode ();
105
- }
106
-
102
+ $ publicSuffix = $ this ->normalize ($ publicSuffix );
107
103
$ publicSuffixContent = $ publicSuffix ->getContent ();
108
104
if ($ this ->domain === $ publicSuffixContent ) {
109
105
throw new Exception (sprintf ('The public suffix `%s` can not be equal to the domain name `%s` ' , $ publicSuffixContent , $ this ->domain ));
@@ -116,6 +112,23 @@ private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
116
112
return $ publicSuffix ;
117
113
}
118
114
115
+ /**
116
+ * Normalize the domain name encoding content.
117
+ *
118
+ * @param mixed $domain
119
+ *
120
+ * @return mixed
121
+ */
122
+ private function normalize ($ domain )
123
+ {
124
+ static $ pattern = '/[^\x20-\x7f]/ ' ;
125
+ if (null !== $ this ->domain && preg_match ($ pattern , $ this ->domain )) {
126
+ return $ domain ->toUnicode ();
127
+ }
128
+
129
+ return $ domain ->toAscii ();
130
+ }
131
+
119
132
/**
120
133
* Computes the registrable domain part.
121
134
*/
@@ -358,8 +371,8 @@ public function toUnicode()
358
371
* are: be, ac.be, ulb.ac.be, or the null public suffix. Any other public
359
372
* suffix will throw an Exception.
360
373
*
361
- * This method does not change the domain conent it only updates/changes/removes
362
- * its public suffix information .
374
+ * This method MUST retain the state of the current instance, and return
375
+ * an instance that contains the modified Public Suffix Information .
363
376
*
364
377
* @param mixed $publicSuffix
365
378
*
@@ -371,11 +384,7 @@ public function resolve($publicSuffix): self
371
384
$ publicSuffix = new PublicSuffix ($ publicSuffix );
372
385
}
373
386
374
- static $ pattern = '/[^\x20-\x7f]/ ' ;
375
- if (null !== $ this ->domain && preg_match ($ pattern , $ this ->domain )) {
376
- $ publicSuffix = $ publicSuffix ->toUnicode ();
377
- }
378
-
387
+ $ publicSuffix = $ this ->normalize ($ publicSuffix );
379
388
if ($ this ->publicSuffix == $ publicSuffix ) {
380
389
return $ this ;
381
390
}
@@ -392,7 +401,7 @@ public function resolve($publicSuffix): self
392
401
* Returns an instance with the specified sub domain added.
393
402
*
394
403
* This method MUST retain the state of the current instance, and return
395
- * an instance that contains the modified domain with the new sub domain
404
+ * an instance that contains the new sub domain
396
405
*
397
406
* @param mixed $subDomain the subdomain to add
398
407
*
@@ -407,33 +416,30 @@ public function withSubDomain($subDomain): self
407
416
}
408
417
409
418
if (null === $ this ->publicSuffix ->getContent ()) {
410
- throw new Exception ('A subdomain can not be added to domain without a public suffix. ' );
411
- }
412
-
413
- static $ pattern = '/[^\x20-\x7f]/ ' ;
414
- $ subDomain = $ subDomain ->toAscii ();
415
- if (null !== $ this ->domain && preg_match ($ pattern , $ this ->domain )) {
416
- $ subDomain = $ subDomain ->toUnicode ();
419
+ throw new Exception ('A subdomain can not be added to a domain without a public suffix part. ' );
417
420
}
418
421
419
- $ subDomain = $ subDomain -> getContent ( );
420
- if ($ subDomain === $ this -> subDomain ) {
422
+ $ subDomain = $ this -> normalize ( $ subDomain );
423
+ if ($ this -> subDomain === $ subDomain -> getContent () ) {
421
424
return $ this ;
422
425
}
423
426
424
- $ newDomain = $ this ->registrableDomain ;
425
- if (null !== $ subDomain ) {
426
- $ newDomain = $ subDomain .'. ' .$ newDomain ;
427
- }
427
+ $ clone = clone $ this ;
428
+ $ clone ->labels = array_merge (
429
+ array_slice ($ this ->labels , 0 , count ($ this ->publicSuffix ) + 1 ),
430
+ iterator_to_array ($ subDomain )
431
+ );
432
+ $ clone ->domain = implode ('. ' , array_reverse ($ clone ->labels ));
433
+ $ clone ->subDomain = $ subDomain ->getContent ();
428
434
429
- return new Domain ( $ newDomain , $ this -> publicSuffix ) ;
435
+ return $ clone ;
430
436
}
431
437
432
438
/**
433
439
* Returns an instance with the specified public suffix added.
434
440
*
435
441
* This method MUST retain the state of the current instance, and return
436
- * an instance that contains the modified component with the new public suffix
442
+ * an instance that contains the new public suffix
437
443
*
438
444
* @param mixed $publicSuffix
439
445
*
@@ -451,33 +457,31 @@ public function withPublicSuffix($publicSuffix): self
451
457
throw new Exception ('A public suffix can not be added to domain without a public suffix. ' );
452
458
}
453
459
454
- static $ pattern = '/[^\x20-\x7f]/ ' ;
455
- $ publicSuffix = $ publicSuffix ->toAscii ();
456
- if (null !== $ this ->domain && preg_match ($ pattern , $ this ->domain )) {
457
- $ publicSuffix = $ publicSuffix ->toUnicode ();
458
- }
459
-
460
+ $ publicSuffix = $ this ->normalize ($ publicSuffix );
460
461
if ($ this ->publicSuffix == $ publicSuffix ) {
461
462
return $ this ;
462
463
}
463
464
464
- $ newDomain = $ this -> labels [ count ( $ this -> publicSuffix )] ;
465
- if ( null !== $ this -> subDomain ) {
466
- $ newDomain = $ this -> subDomain . ' . ' . $ newDomain ;
467
- }
468
-
469
- if ( null !== $ publicSuffix -> getContent ()) {
470
- $ newDomain .= ' . ' . $ publicSuffix-> getContent () ;
471
- }
465
+ $ clone = clone $ this ;
466
+ $ clone -> labels = array_merge (
467
+ iterator_to_array ( $ publicSuffix ),
468
+ array_slice ( $ this -> labels , count ( $ this -> publicSuffix ))
469
+ );
470
+ $ clone -> domain = implode ( ' . ' , array_reverse ( $ clone -> labels ));
471
+ $ clone -> publicSuffix = $ publicSuffix ;
472
+ $ clone -> registrableDomain = $ this -> labels [ count ( $ this -> publicSuffix )]. ' . ' . $ publicSuffix -> getContent ();
472
473
473
- return new Domain ( $ newDomain , $ publicSuffix ) ;
474
+ return $ clone ;
474
475
}
475
476
476
477
/**
477
478
* Returns an instance with the specified label added at the specified key.
478
479
*
479
480
* This method MUST retain the state of the current instance, and return
480
- * an instance that contains the modified domain with the new label
481
+ * an instance that contains the new label
482
+ *
483
+ * If $key is non-negative, the added label will be the label at $key position from the start.
484
+ * If $key is negative, the added label will be the label at $key position from the end.
481
485
*
482
486
* @param int $key
483
487
* @param mixed $label
@@ -493,45 +497,52 @@ public function withLabel(int $key, $label): self
493
497
$ label = new PublicSuffix ($ label );
494
498
}
495
499
496
- if (null === $ label -> getContent () || 1 !== count ($ label )) {
500
+ if (1 !== count ($ label )) {
497
501
throw new Exception (sprintf ('The label `%s` is invalid ' , $ label ->getContent ()));
498
502
}
499
503
500
- static $ pattern = '/[^\x20-\x7f]/ ' ;
501
- $ label = $ label ->toAscii ();
502
- if (null !== $ this ->domain && preg_match ($ pattern , $ this ->domain )) {
503
- $ label = $ label ->toUnicode ();
504
- }
505
-
506
- $ label = $ label ->getContent ();
507
504
$ nb_labels = count ($ this ->labels );
508
505
$ offset = filter_var ($ key , FILTER_VALIDATE_INT , ['options ' => ['min_range ' => - $ nb_labels - 1 , 'max_range ' => $ nb_labels ]]);
509
506
if (false === $ offset ) {
510
507
throw new Exception (sprintf ('the given key `%s` is invalid ' , $ key ));
511
508
}
512
509
513
- if ($ offset < 0 ) {
510
+ if (0 > $ offset ) {
514
511
$ offset = $ nb_labels + $ offset ;
515
512
}
516
513
514
+ $ label = $ this ->normalize ($ label )->getContent ();
517
515
if ($ label === ($ this ->labels [$ offset ] ?? null )) {
518
516
return $ this ;
519
517
}
520
518
521
- $ labels = $ this ->labels ;
522
- $ labels [$ offset ] = $ label ;
519
+ $ clone = clone $ this ;
520
+ $ clone ->labels [$ offset ] = $ label ;
521
+ ksort ($ clone ->labels );
522
+ $ clone ->labels = array_values ($ clone ->labels );
523
+ $ clone ->domain = implode ('. ' , array_reverse ($ clone ->labels ));
524
+ if (null !== $ this ->publicSuffix ->getLabel ($ offset )) {
525
+ $ clone ->publicSuffix = new PublicSuffix ();
526
+ $ clone ->registrableDomain = null ;
527
+ $ clone ->subDomain = null ;
523
528
524
- return new Domain (
525
- implode ('. ' , array_reverse ($ labels )),
526
- $ offset < 0 || null === $ this ->publicSuffix ->getLabel ($ offset ) ? $ this ->publicSuffix : null
527
- );
529
+ return $ clone ;
530
+ }
531
+
532
+ $ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
533
+ $ clone ->subDomain = $ clone ->setSubDomain ();
534
+
535
+ return $ clone ;
528
536
}
529
537
530
538
/**
531
539
* Returns an instance with the label at the specified key removed.
532
540
*
533
541
* This method MUST retain the state of the current instance, and return
534
- * an instance that contains the modified domain with the label removed
542
+ * an instance without the specified label
543
+ *
544
+ * If $key is non-negative, the removed label will be the label at $key position from the start.
545
+ * If $key is negative, the removed label will be the label at $key position from the end.
535
546
*
536
547
* @param int $key
537
548
*
@@ -547,16 +558,24 @@ public function withoutLabel(int $key): self
547
558
throw new Exception (sprintf ('the given key `%s` is invalid ' , $ key ));
548
559
}
549
560
550
- if ($ offset < 0 ) {
561
+ if (0 > $ offset ) {
551
562
$ offset = $ nb_labels + $ offset ;
552
563
}
553
564
554
- $ labels = $ this ->labels ;
555
- unset($ labels [$ offset ]);
565
+ $ clone = clone $ this ;
566
+ unset($ clone ->labels [$ offset ]);
567
+ $ clone ->domain = implode ('. ' , array_reverse ($ clone ->labels ));
568
+ if (null !== $ this ->publicSuffix ->getLabel ($ offset )) {
569
+ $ clone ->publicSuffix = new PublicSuffix ();
570
+ $ clone ->registrableDomain = null ;
571
+ $ clone ->subDomain = null ;
572
+
573
+ return $ clone ;
574
+ }
575
+
576
+ $ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
577
+ $ clone ->subDomain = $ clone ->setSubDomain ();
556
578
557
- return new Domain (
558
- implode ('. ' , array_reverse ($ labels )),
559
- $ offset < 0 || null == $ this ->publicSuffix ->getLabel ($ offset ) ? $ this ->publicSuffix : null
560
- );
579
+ return $ clone ;
561
580
}
562
581
}
0 commit comments