@@ -37,10 +37,20 @@ final class Domain
3737 */
3838 private $ publicSuffix ;
3939
40+ /**
41+ * @var string|null
42+ */
43+ private $ registrableDomain ;
44+
45+ /**
46+ * @var string|null
47+ */
48+ private $ subDomain ;
49+
4050 /**
4151 * @var bool
4252 */
43- private $ isValid ;
53+ private $ isValid = false ;
4454
4555 /**
4656 * New instance.
@@ -52,8 +62,101 @@ final class Domain
5262 public function __construct ($ domain = null , $ publicSuffix = null , bool $ isValid = false )
5363 {
5464 $ this ->domain = $ domain ;
55- $ this ->publicSuffix = null !== $ this ->domain ? $ publicSuffix : null ;
56- $ this ->isValid = null !== $ this ->publicSuffix ? $ isValid : false ;
65+ $ this ->setPublicSuffix ($ publicSuffix );
66+ $ this ->setValidity ($ isValid );
67+ $ this ->setRegistrableDomain ();
68+ $ this ->setSubDomain ();
69+ }
70+
71+ /**
72+ * Compute the public suffix part
73+ *
74+ * @param string|null $publicSuffix
75+ */
76+ private function setPublicSuffix ($ publicSuffix )
77+ {
78+ if (null === $ this ->domain ) {
79+ return ;
80+ }
81+
82+ $ this ->publicSuffix = $ publicSuffix ;
83+ }
84+
85+ /**
86+ * Compute the domain validity
87+ *
88+ * @param bool $isValid
89+ */
90+ private function setValidity (bool $ isValid )
91+ {
92+ if (null === $ this ->publicSuffix ) {
93+ return ;
94+ }
95+
96+ $ this ->isValid = $ isValid ;
97+ }
98+
99+ /**
100+ * Compute the registrable domain part
101+ */
102+ private function setRegistrableDomain ()
103+ {
104+ if (!$ this ->hasRegistrableDomain ()) {
105+ return ;
106+ }
107+
108+ $ countLabelsToRemove = count (explode ('. ' , $ this ->publicSuffix )) + 1 ;
109+ $ domainLabels = explode ('. ' , $ this ->domain );
110+ $ domain = implode ('. ' , array_slice ($ domainLabels , count ($ domainLabels ) - $ countLabelsToRemove ));
111+ $ this ->registrableDomain = $ this ->normalize ($ domain );
112+ }
113+
114+ /**
115+ * Tells whether the domain has a registrable domain part.
116+ *
117+ * @return bool
118+ */
119+ private function hasRegistrableDomain (): bool
120+ {
121+ return null !== $ this ->publicSuffix
122+ && strpos ($ this ->domain , '. ' ) > 0
123+ && $ this ->publicSuffix !== $ this ->domain ;
124+ }
125+
126+ /**
127+ * Normalize the domain according to its representation.
128+ *
129+ * @param string $domain
130+ *
131+ * @return string
132+ */
133+ private function normalize (string $ domain ): string
134+ {
135+ if (strpos ($ domain , 'xn-- ' ) !== false ) {
136+ return strtolower (idn_to_ascii ($ domain , 0 , INTL_IDNA_VARIANT_UTS46 ));
137+ }
138+
139+ return idn_to_utf8 ($ domain , 0 , INTL_IDNA_VARIANT_UTS46 );
140+ }
141+
142+ /**
143+ * Compute the sub domain part
144+ */
145+ private function setSubDomain ()
146+ {
147+ if (!$ this ->hasRegistrableDomain ()) {
148+ return ;
149+ }
150+
151+ $ domainLabels = explode ('. ' , $ this ->domain );
152+ $ countLabels = count ($ domainLabels );
153+ $ countLabelsToRemove = count (explode ('. ' , $ this ->publicSuffix )) + 1 ;
154+ if ($ countLabels === $ countLabelsToRemove ) {
155+ return ;
156+ }
157+
158+ $ domain = implode ('. ' , array_slice ($ domainLabels , 0 , $ countLabels - $ countLabelsToRemove ));
159+ $ this ->subDomain = $ this ->normalize ($ domain );
57160 }
58161
59162 /**
@@ -108,43 +211,7 @@ public function isValid(): bool
108211 */
109212 public function getRegistrableDomain ()
110213 {
111- if (!$ this ->hasRegistrableDomain ()) {
112- return null ;
113- }
114-
115- $ countLabelsToRemove = count (explode ('. ' , $ this ->publicSuffix )) + 1 ;
116- $ domainLabels = explode ('. ' , $ this ->domain );
117- $ domain = implode ('. ' , array_slice ($ domainLabels , count ($ domainLabels ) - $ countLabelsToRemove ));
118-
119- return $ this ->normalize ($ domain );
120- }
121-
122- /**
123- * Tells whether the domain has a registrable domain part.
124- *
125- * @return bool
126- */
127- private function hasRegistrableDomain (): bool
128- {
129- return null !== $ this ->publicSuffix
130- && strpos ($ this ->domain , '. ' ) > 0
131- && $ this ->publicSuffix !== $ this ->domain ;
132- }
133-
134- /**
135- * Normalize the domain according to its representation.
136- *
137- * @param string $domain
138- *
139- * @return string
140- */
141- private function normalize (string $ domain ): string
142- {
143- if (strpos ($ domain , 'xn-- ' ) !== false ) {
144- return strtolower (idn_to_ascii ($ domain , 0 , INTL_IDNA_VARIANT_UTS46 ));
145- }
146-
147- return idn_to_utf8 ($ domain , 0 , INTL_IDNA_VARIANT_UTS46 );
214+ return $ this ->registrableDomain ;
148215 }
149216
150217 /**
@@ -159,19 +226,6 @@ private function normalize(string $domain): string
159226 */
160227 public function getSubDomain ()
161228 {
162- if (!$ this ->hasRegistrableDomain ()) {
163- return null ;
164- }
165-
166- $ domainLabels = explode ('. ' , $ this ->domain );
167- $ countLabels = count ($ domainLabels );
168- $ countLabelsToRemove = count (explode ('. ' , $ this ->publicSuffix )) + 1 ;
169- if ($ countLabels === $ countLabelsToRemove ) {
170- return null ;
171- }
172-
173- $ domain = implode ('. ' , array_slice ($ domainLabels , 0 , $ countLabels - $ countLabelsToRemove ));
174-
175- return $ this ->normalize ($ domain );
229+ return $ this ->subDomain ;
176230 }
177231}
0 commit comments