11
11
12
12
namespace Pdp ;
13
13
14
+ use JsonSerializable ;
15
+
14
16
/**
15
17
* Domain Value Object
16
18
*
17
- * Lifted pretty much completely from Jeremy Kendall PDP
18
- * project
19
+ * WARNING: "Some people use the PSL to determine what is a valid domain name
20
+ * and what isn't. This is dangerous, particularly in these days where new
21
+ * gTLDs are arriving at a rapid pace, if your software does not regularly
22
+ * receive PSL updates, it will erroneously think new gTLDs are not
23
+ * valid. The DNS is the proper source for this innormalizeion. If you must use
24
+ * it for this purpose, please do not bake static copies of the PSL into your
25
+ * software with no update mechanism."
19
26
*
20
27
* @author Jeremy Kendall <[email protected] >
21
28
* @author Ignace Nyamagana Butera <[email protected] >
22
29
*/
23
- final class Domain
30
+ final class Domain implements JsonSerializable
24
31
{
25
32
/**
26
33
* @var string|null
@@ -63,8 +70,12 @@ public function __construct($domain = null, PublicSuffix $publicSuffix)
63
70
*/
64
71
private function setRegistrableDomain ()
65
72
{
66
- if (!$ this ->hasRegistrableDomain ()) {
67
- return ;
73
+ if (strpos ((string ) $ this ->domain , '. ' ) === false ) {
74
+ return null ;
75
+ }
76
+
77
+ if (in_array ($ this ->publicSuffix ->getContent (), [null , $ this ->domain ], true )) {
78
+ return null ;
68
79
}
69
80
70
81
$ nbLabelsToRemove = count ($ this ->publicSuffix ) + 1 ;
@@ -75,18 +86,7 @@ private function setRegistrableDomain()
75
86
}
76
87
77
88
/**
78
- * Tells whether the domain has a registrable domain part.
79
- *
80
- * @return bool
81
- */
82
- private function hasRegistrableDomain (): bool
83
- {
84
- return strpos ((string ) $ this ->domain , '. ' ) > 0
85
- && !in_array ($ this ->publicSuffix ->getContent (), [null , $ this ->domain ], true );
86
- }
87
-
88
- /**
89
- * Normalize the domain according to its representation.
89
+ * Normalizes the domain according to its representation.
90
90
*
91
91
* @param string $domain
92
92
*
@@ -108,13 +108,13 @@ private function normalize(string $domain)
108
108
}
109
109
110
110
/**
111
- * Compute the sub domain part.
111
+ * Computes the sub domain part.
112
112
*
113
113
* @return string|null
114
114
*/
115
115
private function setSubDomain ()
116
116
{
117
- if (! $ this ->hasRegistrableDomain () ) {
117
+ if (null === $ this ->registrableDomain ) {
118
118
return null ;
119
119
}
120
120
@@ -125,95 +125,61 @@ private function setSubDomain()
125
125
return null ;
126
126
}
127
127
128
- $ domain = implode ('. ' , array_slice ($ domainLabels , 0 , $ countLabels - $ nbLabelsToRemove ));
128
+ $ subDomain = implode ('. ' , array_slice ($ domainLabels , 0 , $ countLabels - $ nbLabelsToRemove ));
129
129
130
- return $ this ->normalize ($ domain );
130
+ return $ this ->normalize ($ subDomain );
131
131
}
132
132
133
133
/**
134
- * @return string|null
135
- */
136
- public function getDomain ()
137
- {
138
- return $ this ->domain ;
139
- }
140
-
141
- /**
142
- * @return string|null
134
+ * {@inheritdoc}
143
135
*/
144
- public function getPublicSuffix ()
136
+ public function jsonSerialize ()
145
137
{
146
- return $ this ->publicSuffix ->getContent ();
138
+ return [
139
+ 'domain ' => $ this ->domain ,
140
+ 'registrableDomain ' => $ this ->registrableDomain ,
141
+ 'subDomain ' => $ this ->subDomain ,
142
+ 'publicSuffix ' => $ this ->publicSuffix ->getContent (),
143
+ 'isKnown ' => $ this ->publicSuffix ->isKnown (),
144
+ 'isICANN ' => $ this ->publicSuffix ->isICANN (),
145
+ 'isPrivate ' => $ this ->publicSuffix ->isPrivate (),
146
+ ];
147
147
}
148
148
149
149
/**
150
- * Does the domain have a matching rule in the Public Suffix List?
151
- *
152
- * WARNING: "Some people use the PSL to determine what is a valid domain name
153
- * and what isn't. This is dangerous, particularly in these days where new
154
- * gTLDs are arriving at a rapid pace, if your software does not regularly
155
- * receive PSL updates, because it will erroneously think new gTLDs are not
156
- * valid. The DNS is the proper source for this innormalizeion. If you must use
157
- * it for this purpose, please do not bake static copies of the PSL into your
158
- * software with no update mechanism."
159
- *
160
- * @see https://publicsuffix.org/learn/
161
- *
162
- * @return bool
150
+ * {@inheritdoc}
163
151
*/
164
- public function isKnown (): bool
152
+ public function __debugInfo ()
165
153
{
166
- return $ this ->publicSuffix -> isKnown ();
154
+ return $ this ->jsonSerialize ();
167
155
}
168
156
169
157
/**
170
- * Does the domain have a matching rule in the Public Suffix List ICANN section
171
- *
172
- * WARNING: "Some people use the PSL to determine what is a valid domain name
173
- * and what isn't. This is dangerous, particularly in these days where new
174
- * gTLDs are arriving at a rapid pace, if your software does not regularly
175
- * receive PSL updates, because it will erroneously think new gTLDs are not
176
- * valid. The DNS is the proper source for this innormalizeion. If you must use
177
- * it for this purpose, please do not bake static copies of the PSL into your
178
- * software with no update mechanism."
179
- *
180
- * @see https://publicsuffix.org/learn/
181
- *
182
- * @return bool
158
+ * {@inheritdoc}
183
159
*/
184
- public function isICANN (): bool
160
+ public static function __set_state ( array $ properties )
185
161
{
186
- return $ this -> publicSuffix -> isICANN ( );
162
+ return new self ( $ properties [ ' domain ' ], $ properties [ ' publicSuffix ' ] );
187
163
}
188
164
189
165
/**
190
- * Does the domain have a matching rule in the Public Suffix List Private section
191
- *
192
- * WARNING: "Some people use the PSL to determine what is a valid domain name
193
- * and what isn't. This is dangerous, particularly in these days where new
194
- * gTLDs are arriving at a rapid pace, if your software does not regularly
195
- * receive PSL updates, because it will erroneously think new gTLDs are not
196
- * valid. The DNS is the proper source for this innormalizeion. If you must use
197
- * it for this purpose, please do not bake static copies of the PSL into your
198
- * software with no update mechanism."
166
+ * Returns the full domain name.
199
167
*
200
- * @see https://publicsuffix.org/learn/
168
+ * This method should return null on seriously malformed domain name
201
169
*
202
- * @return bool
170
+ * @return string|null
203
171
*/
204
- public function isPrivate (): bool
172
+ public function getDomain ()
205
173
{
206
- return $ this ->publicSuffix -> isPrivate () ;
174
+ return $ this ->domain ;
207
175
}
208
176
209
177
/**
210
- * Get registrable domain.
178
+ * Returns the registrable domain.
211
179
*
212
- * Algorithm #7: The registered or registrable domain is the public suffix
213
- * plus one additional label.
180
+ * The registered or registrable domain is the public suffix plus one additional label.
214
181
*
215
- * This method should return null if the domain provided is a public suffix,
216
- * per the test cases provided by Mozilla.
182
+ * This method should return null if the registrable domain is the same as the public suffix.
217
183
*
218
184
* @see https://publicsuffix.org/list/
219
185
* @see https://raw.githubusercontent.com/publicsuffix/list/master/tests/test_psl.txt
@@ -226,12 +192,12 @@ public function getRegistrableDomain()
226
192
}
227
193
228
194
/**
229
- * Get the sub domain.
195
+ * Returns the sub domain.
230
196
*
231
- * This method should return null if
197
+ * The sub domain represents the remaining labels without the registrable domain.
232
198
*
233
- * - the registrable domain is null
234
- * - the registrable domain is the same as the public suffix
199
+ * This method should return null if the registrable domain is null
200
+ * This method should return null if the registrable domain is the same as the public suffix
235
201
*
236
202
* @return string|null registrable domain
237
203
*/
@@ -241,26 +207,48 @@ public function getSubDomain()
241
207
}
242
208
243
209
/**
244
- * {@inheritdoc}
210
+ * Returns the public suffix.
211
+ *
212
+ * @return string|null
245
213
*/
246
- public function __debugInfo ()
214
+ public function getPublicSuffix ()
247
215
{
248
- return [
249
- 'domain ' => $ this ->domain ,
250
- 'publicSuffix ' => $ this ->publicSuffix ->getContent (),
251
- 'registrableDomain ' => $ this ->registrableDomain ,
252
- 'subDomain ' => $ this ->subDomain ,
253
- 'isKnown ' => $ this ->isKnown (),
254
- 'isICANN ' => $ this ->isICANN (),
255
- 'isPrivate ' => $ this ->isPrivate (),
256
- ];
216
+ return $ this ->publicSuffix ->getContent ();
257
217
}
258
218
259
219
/**
260
- * {@inheritdoc}
220
+ * Tells whether the public suffix has a matching rule in a Public Suffix List.
221
+ *
222
+ * @see https://publicsuffix.org/learn/
223
+ *
224
+ * @return bool
261
225
*/
262
- public static function __set_state ( array $ properties )
226
+ public function isKnown (): bool
263
227
{
264
- return new self ($ properties ['domain ' ], $ properties ['publicSuffix ' ]);
228
+ return $ this ->publicSuffix ->isKnown ();
229
+ }
230
+
231
+ /**
232
+ * Tells whether the public suffix has a matching rule in a Public Suffix List ICANN Section.
233
+ *
234
+ * @see https://publicsuffix.org/learn/
235
+ *
236
+ * @return bool
237
+ */
238
+ public function isICANN (): bool
239
+ {
240
+ return $ this ->publicSuffix ->isICANN ();
241
+ }
242
+
243
+ /**
244
+ * Tells whether the public suffix has a matching rule in a Public Suffix List Private Section.
245
+ *
246
+ * @see https://publicsuffix.org/learn/
247
+ *
248
+ * @return bool
249
+ */
250
+ public function isPrivate (): bool
251
+ {
252
+ return $ this ->publicSuffix ->isPrivate ();
265
253
}
266
254
}
0 commit comments