@@ -73,44 +73,47 @@ public static function __set_state(array $properties): self
73
73
public function __construct (string $ domain = null , PublicSuffix $ publicSuffix = null )
74
74
{
75
75
list ($ this ->domain , $ this ->labels ) = $ this ->setDomain ($ domain );
76
- $ this ->publicSuffix = $ this ->setPublicSuffix ($ publicSuffix );
77
- $ this ->assertValidState ();
76
+ $ this ->publicSuffix = $ this ->setPublicSuffix ($ publicSuffix ?? new PublicSuffix ());
78
77
$ this ->registrableDomain = $ this ->setRegistrableDomain ();
79
78
$ this ->subDomain = $ this ->setSubDomain ();
80
79
}
81
80
82
81
/**
83
82
* Sets the public suffix domain part.
84
83
*
85
- * @param PublicSuffix|null $publicSuffix
84
+ * @param PublicSuffix $publicSuffix
85
+ *
86
+ * @throws Exception If the domain can not contain a public suffix
87
+ * @throws Exception If the domain value is the same as the public suffix value
88
+ * @throws Exception If the domain can not be match with the public suffix
86
89
*
87
90
* @return PublicSuffix
88
91
*/
89
- private function setPublicSuffix (PublicSuffix $ publicSuffix = null ): PublicSuffix
92
+ private function setPublicSuffix (PublicSuffix $ publicSuffix ): PublicSuffix
90
93
{
91
- if (null === $ publicSuffix
92
- || null === $ this ->domain
93
- || false === strpos ($ this ->domain , '. ' )
94
- || count ($ this ->labels ) === count ($ publicSuffix )
95
- ) {
94
+ if (null === $ publicSuffix ->getContent ()) {
96
95
return new PublicSuffix ();
97
96
}
98
97
99
- return $ publicSuffix ;
100
- }
98
+ if (null === $ this ->domain || false === strpos ($ this ->domain , '. ' )) {
99
+ throw new Exception (sprintf ('The domain `%s` can not contain a public suffix ' , $ this ->domain ));
100
+ }
101
101
102
- /**
103
- * assert the domain internal state is valid
104
- *
105
- * @throws Exception if the public suffix does not match the domain
106
- */
107
- protected function assertValidState ()
108
- {
109
- foreach ($ this ->publicSuffix as $ offset => $ label ) {
110
- if ($ label !== $ this ->labels [$ offset ]) {
111
- throw new Exception (sprintf ('The public suffix `%s` is invalid for the domain `%s` ' , $ this ->publicSuffix ->getContent (), $ this ->domain ));
112
- }
102
+ static $ pattern = '/[^\x20-\x7f]/ ' ;
103
+ if (preg_match ($ pattern , $ this ->domain )) {
104
+ $ publicSuffix = $ publicSuffix ->toUnicode ();
105
+ }
106
+
107
+ $ publicSuffixContent = $ publicSuffix ->getContent ();
108
+ if ($ this ->domain === $ publicSuffixContent ) {
109
+ throw new Exception (sprintf ('The public suffix `%s` can not be equal to the domain name `%s` ' , $ publicSuffixContent , $ this ->domain ));
110
+ }
111
+
112
+ if ('. ' .$ publicSuffixContent !== substr ($ this ->domain , - strlen ($ publicSuffixContent ) - 1 )) {
113
+ throw new Exception (sprintf ('The public suffix `%s` can not be assign to the domain name `%s` ' , $ publicSuffixContent , $ this ->domain ));
113
114
}
115
+
116
+ return $ publicSuffix ;
114
117
}
115
118
116
119
/**
@@ -350,9 +353,6 @@ public function toUnicode()
350
353
*
351
354
* @param PublicSuffix $publicSuffix
352
355
*
353
- * @throws Exception if the domain can not contain a public suffix
354
- * @throws Exception if the public suffix can not be assign to the domain name
355
- *
356
356
* @return self
357
357
*/
358
358
public function withPublicSuffix (PublicSuffix $ publicSuffix ): self
@@ -361,31 +361,8 @@ public function withPublicSuffix(PublicSuffix $publicSuffix): self
361
361
return $ this ;
362
362
}
363
363
364
- if (null === $ publicSuffix ->getContent ()) {
365
- $ clone = clone $ this ;
366
- $ clone ->publicSuffix = $ publicSuffix ;
367
- $ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
368
- $ clone ->subDomain = $ clone ->setSubDomain ();
369
-
370
- return $ clone ;
371
- }
372
-
373
- if (null === $ this ->domain || false === strpos ($ this ->domain , '. ' )) {
374
- throw new Exception (sprintf ('The domain `%s` can not contain a public suffix ' , $ this ->domain ));
375
- }
376
-
377
- static $ pattern = '/[^\x20-\x7f]/ ' ;
378
- if (preg_match ($ pattern , $ this ->domain )) {
379
- $ publicSuffix = $ publicSuffix ->toUnicode ();
380
- }
381
-
382
- $ publicSuffixContent = $ publicSuffix ->getContent ();
383
- if ($ this ->domain === $ publicSuffixContent || '. ' .$ publicSuffixContent !== substr ($ this ->domain , - strlen ($ publicSuffixContent ) - 1 )) {
384
- throw new Exception (sprintf ('the public suffix `%s` can not be assign to the domain name `%s` ' , $ publicSuffixContent , $ this ->domain ));
385
- }
386
-
387
364
$ clone = clone $ this ;
388
- $ clone ->publicSuffix = $ publicSuffix ;
365
+ $ clone ->publicSuffix = $ clone -> setPublicSuffix ( $ publicSuffix) ;
389
366
$ clone ->registrableDomain = $ clone ->setRegistrableDomain ();
390
367
$ clone ->subDomain = $ clone ->setSubDomain ();
391
368
0 commit comments