11
11
12
12
namespace Pdp ;
13
13
14
+ use Countable ;
14
15
use JsonSerializable ;
15
16
16
17
/**
27
28
* @author Jeremy Kendall <[email protected] >
28
29
* @author Ignace Nyamagana Butera <[email protected] >
29
30
*/
30
- final class Domain implements JsonSerializable
31
+ final class Domain implements Countable, JsonSerializable
31
32
{
32
33
use IDNAConverterTrait;
33
34
@@ -67,56 +68,72 @@ public static function __set_state(array $properties): self
67
68
*/
68
69
public function __construct ($ domain = null , PublicSuffix $ publicSuffix = null )
69
70
{
70
- if (false !== strpos ((string ) $ domain , '% ' )) {
71
- $ domain = rawurldecode ($ domain );
71
+ $ this ->domain = $ this ->setDomain ($ domain );
72
+ $ this ->publicSuffix = $ this ->setPublicSuffix ($ publicSuffix );
73
+ $ this ->registrableDomain = $ this ->setRegistrableDomain ();
74
+ $ this ->subDomain = $ this ->setSubDomain ();
75
+ }
76
+
77
+ /**
78
+ * Normalize the given domain.
79
+ *
80
+ * @param string|null $domain
81
+ *
82
+ * @return string|null
83
+ */
84
+ private function setDomain (string $ domain = null )
85
+ {
86
+ if (null === $ domain ) {
87
+ return null ;
72
88
}
73
89
74
- if (null !== $ domain ) {
75
- $ domain = strtolower ($ domain );
90
+ if (false !== strpos ( $ domain, ' % ' ) ) {
91
+ $ domain = rawurldecode ($ domain );
76
92
}
77
93
78
- $ this ->domain = $ domain ;
79
- $ this ->publicSuffix = $ this ->setPublicSuffix ($ publicSuffix );
80
- $ this ->registrableDomain = $ this ->setRegistrableDomain ();
81
- $ this ->subDomain = $ this ->setSubDomain ();
94
+ return strtolower ($ domain );
82
95
}
83
96
84
97
/**
85
- * Filter the PublicSuffix
98
+ * Sets the public suffix domain part.
86
99
*
87
100
* @param PublicSuffix|null $publicSuffix
88
101
*
89
102
* @return PublicSuffix
90
103
*/
91
104
private function setPublicSuffix (PublicSuffix $ publicSuffix = null ): PublicSuffix
92
105
{
93
- if (null === $ publicSuffix || null === $ this ->domain ) {
106
+ $ publicSuffix = $ publicSuffix ?? new PublicSuffix ();
107
+ if (null === $ publicSuffix ->getContent ()) {
108
+ return $ publicSuffix ;
109
+ }
110
+
111
+ if (null === $ this ->domain || false === strpos ($ this ->domain , '. ' )) {
94
112
return new PublicSuffix ();
95
113
}
96
114
97
115
return $ publicSuffix ;
98
116
}
99
117
100
118
/**
101
- * Compute the registrable domain part.
119
+ * Computes the registrable domain part.
102
120
*
103
121
* @return string|null
104
122
*/
105
123
private function setRegistrableDomain ()
106
124
{
107
- if (false === strpos (( string ) $ this ->domain , ' . ' )) {
125
+ if (null === $ this ->publicSuffix -> getContent ( )) {
108
126
return null ;
109
127
}
110
128
111
- if (in_array ($ this ->publicSuffix ->getContent (), [null , $ this ->domain ], true )) {
129
+ $ labels = explode ('. ' , $ this ->domain );
130
+ $ countLabels = count ($ labels );
131
+ $ countPublicSuffixLabels = count ($ this ->publicSuffix );
132
+ if ($ countLabels === $ countPublicSuffixLabels ) {
112
133
return null ;
113
134
}
114
135
115
- $ nbLabelsToRemove = count ($ this ->publicSuffix ) + 1 ;
116
- $ domainLabels = explode ('. ' , $ this ->domain );
117
- $ registrableDomain = implode ('. ' , array_slice ($ domainLabels , count ($ domainLabels ) - $ nbLabelsToRemove ));
118
-
119
- return $ registrableDomain ;
136
+ return implode ('. ' , array_slice ($ labels , $ countLabels - $ countPublicSuffixLabels - 1 ));
120
137
}
121
138
122
139
/**
@@ -130,16 +147,14 @@ private function setSubDomain()
130
147
return null ;
131
148
}
132
149
133
- $ nbLabelsToRemove = count ( $ this ->publicSuffix ) + 1 ;
134
- $ domainLabels = explode ( ' . ' , $ this -> domain );
135
- $ countLabels = count ($ domainLabels );
136
- if ($ countLabels === $ nbLabelsToRemove ) {
150
+ $ labels = explode ( ' . ' , $ this ->domain ) ;
151
+ $ countLabels = count ( $ labels );
152
+ $ countLabelsToRemove = count (explode ( ' . ' , $ this -> registrableDomain ) );
153
+ if ($ countLabels === $ countLabelsToRemove ) {
137
154
return null ;
138
155
}
139
156
140
- $ subDomain = implode ('. ' , array_slice ($ domainLabels , 0 , $ countLabels - $ nbLabelsToRemove ));
141
-
142
- return $ subDomain ;
157
+ return implode ('. ' , array_slice ($ labels , 0 , $ countLabels - $ countLabelsToRemove ));
143
158
}
144
159
145
160
/**
@@ -162,16 +177,45 @@ public function __debugInfo()
162
177
return $ this ->jsonSerialize ();
163
178
}
164
179
180
+ /**
181
+ * {@inheritdoc}
182
+ */
183
+ public function count ()
184
+ {
185
+ if (null === $ this ->domain ) {
186
+ return 0 ;
187
+ }
188
+
189
+ return count (explode ('. ' , $ this ->domain ));
190
+ }
191
+
192
+ /**
193
+ * Returns the domain content.
194
+ *
195
+ * This method should return null on seriously malformed domain name
196
+ *
197
+ * @return string|null
198
+ */
199
+ public function getContent ()
200
+ {
201
+ return $ this ->domain ;
202
+ }
203
+
165
204
/**
166
205
* Returns the full domain name.
167
206
*
207
+ * DEPRECATION WARNING! This method will be removed in the next major point release
208
+ *
209
+ * @deprecated deprecated since version 5.3
210
+ * @see Domain::getContent
211
+ *
168
212
* This method should return null on seriously malformed domain name
169
213
*
170
214
* @return string|null
171
215
*/
172
216
public function getDomain ()
173
217
{
174
- return $ this ->domain ;
218
+ return $ this ->getContent () ;
175
219
}
176
220
177
221
/**
0 commit comments