1010
1111namespace blobfolio \domain ;
1212
13- use \blobfolio \common ;
13+ use \blobfolio \common \constants ;
14+ use \blobfolio \common \data as c_data ;
15+ use \blobfolio \common \mb as v_mb ;
16+ use \blobfolio \common \ref \cast as r_cast ;
17+ use \blobfolio \common \ref \file as r_file ;
18+ use \blobfolio \common \ref \mb as r_mb ;
19+ use \blobfolio \common \ref \sanitize as r_sanitize ;
1420
1521class domain {
1622
@@ -41,9 +47,7 @@ class domain {
4147 * @param bool $www Strip leading www.
4248 * @return bool True/false.
4349 */
44- public function __construct ($ host ='' , $ www =false ) {
45- common \ref \cast::to_bool ($ www , true );
46-
50+ public function __construct ($ host ='' , bool $ www =false ) {
4751 // Parse the parts.
4852 if (false === ($ parsed = static ::parse_host_parts ($ host ))) {
4953 return false ;
@@ -73,44 +77,45 @@ public function __construct($host='', $www=false) {
7377 */
7478 public static function parse_host ($ host ) {
7579 // Try to parse it the easy way.
76- $ tmp = common \mb ::parse_url ($ host , PHP_URL_HOST );
80+ $ tmp = v_mb ::parse_url ($ host , PHP_URL_HOST );
7781 if ($ tmp ) {
7882 $ host = $ tmp ;
7983 }
8084 // Or the hard way?
8185 else {
82- common \ref \cast::to_string ($ host , true );
83- common \ref \mb::trim ($ host );
86+ r_cast::string ($ host , true );
87+
88+ r_mb::trim ($ host , true );
8489
8590 // Cut off the path, if any.
86- if (false !== ($ start = common \mb ::strpos ($ host , '/ ' ))) {
87- $ host = common \mb ::substr ($ host , 0 , $ start );
91+ if (false !== ($ start = v_mb ::strpos ($ host , '/ ' ))) {
92+ $ host = v_mb ::substr ($ host , 0 , $ start, true );
8893 }
8994
9095 // Cut off the query, if any.
91- if (false !== ($ start = common \mb ::strpos ($ host , '? ' ))) {
92- $ host = common \mb ::substr ($ host , 0 , $ start );
96+ if (false !== ($ start = v_mb ::strpos ($ host , '? ' ))) {
97+ $ host = v_mb ::substr ($ host , 0 , $ start, true );
9398 }
9499
95100 // Cut off credentials, if any.
96- if (false !== ($ start = common \mb ::strpos ($ host , '@ ' ))) {
97- $ host = common \mb ::substr ($ host , $ start + 1 );
101+ if (false !== ($ start = v_mb ::strpos ($ host , '@ ' ))) {
102+ $ host = v_mb ::substr ($ host , $ start + 1 , null , true );
98103 }
99104
100105 // Is this an IPv6 address?
101106 if (filter_var ($ host , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
102- common \ ref \sanitize ::ip ($ host , true );
107+ r_sanitize ::ip ($ host , true );
103108 }
104109 elseif (
105- 0 === common \mb:: strpos ($ host , '[ ' ) &&
106- false !== ($ end = common \mb ::strpos ($ host , '] ' ))
110+ ( 0 === strpos ($ host , '[ ' ) ) &&
111+ false !== ($ end = v_mb ::strpos ($ host , '] ' ))
107112 ) {
108- $ host = common \mb ::substr ($ host , 1 , $ end - 1 );
109- common \ ref \sanitize ::ip ($ host , true );
113+ $ host = v_mb ::substr ($ host , 1 , $ end - 1 , true );
114+ r_sanitize ::ip ($ host , true );
110115 }
111116 // Cut off port, if any.
112- elseif (false !== ($ start = common \mb ::strpos ($ host , ': ' ))) {
113- $ host = common \mb ::substr ($ host , 0 , $ start );
117+ elseif (false !== ($ start = v_mb ::strpos ($ host , ': ' ))) {
118+ $ host = v_mb ::substr ($ host , 0 , $ start, true );
114119 }
115120
116121 // If it is empty or invalid, there is nothing we can do.
@@ -121,22 +126,22 @@ public static function parse_host($host) {
121126 // Convert to ASCII if possible.
122127 if (function_exists ('idn_to_ascii ' )) {
123128 $ host = explode ('. ' , $ host );
124- common \ ref \file ::idn_to_ascii ($ host );
129+ r_file ::idn_to_ascii ($ host );
125130 $ host = implode ('. ' , $ host );
126131 }
127132
128133 // Lowercase it.
129- common \ ref \mb ::strtolower ($ host );
134+ r_mb ::strtolower ($ host, false , true );
130135
131136 // Get rid of trailing periods.
132137 $ host = ltrim ($ host , '. ' );
133138 $ host = rtrim ($ host , '. ' );
134139 }
135140
136141 // Liberate IPv6 from its walls.
137- if (0 === common \mb:: strpos ($ host , '[ ' )) {
142+ if (0 === strpos ($ host , '[ ' )) {
138143 $ host = str_replace (array ('[ ' , '] ' ), '' , $ host );
139- common \ ref \sanitize ::ip ($ host , true );
144+ r_sanitize ::ip ($ host , true );
140145 }
141146
142147 // Is this an IP address? If so, we're done!
@@ -154,8 +159,8 @@ public static function parse_host($host) {
154159 // Gotta have length, and can't start or end with a dash.
155160 if (
156161 !strlen ($ v ) ||
157- ' - ' === substr ($ v , 0 , 1 ) ||
158- '- ' === substr ($ v , -1 )
162+ ( 0 === strpos ($ v , ' - ' ) ) ||
163+ ( '- ' === substr ($ v , -1 ) )
159164 ) {
160165 return false ;
161166 }
@@ -197,7 +202,7 @@ public static function parse_host_parts($host) {
197202
198203 foreach ($ parts as $ k =>$ part ) {
199204 // Override rule.
200- if (isset ($ suffixes [$ part ]) && isset ( $ suffixes [$ part ]['! ' ])) {
205+ if (isset ($ suffixes [$ part ], $ suffixes [$ part ]['! ' ])) {
201206 break ;
202207 }
203208
@@ -259,8 +264,8 @@ public function strip_www() {
259264 }
260265
261266 if (
262- 'www ' === $ this ->subdomain ||
263- ' www. ' === common \mb:: substr ($ this ->subdomain , 0 , 4 )
267+ ( 'www ' === $ this ->subdomain ) ||
268+ ( 0 === strpos ($ this ->subdomain , ' www. ' ) )
264269 ) {
265270 $ this ->subdomain = preg_replace ('/^www\.?/u ' , '' , $ this ->subdomain );
266271 if (!strlen ($ this ->subdomain )) {
@@ -288,8 +293,7 @@ public function strip_www() {
288293 * @param bool $dns Has DNS.
289294 * @return bool True/false.
290295 */
291- public function is_valid ($ dns =false ) {
292- common \ref \cast::to_bool ($ dns , true );
296+ public function is_valid (bool $ dns =false ) {
293297 return !is_null ($ this ->host ) && (!$ dns || $ this ->has_dns ());
294298 }
295299
@@ -311,9 +315,7 @@ public function is_fqdn() {
311315 * @param bool $restricted Allow restricted.
312316 * @return bool True/false.
313317 */
314- public function is_ip ($ restricted =true ) {
315- common \ref \cast::to_bool ($ restricted , true );
316-
318+ public function is_ip (bool $ restricted =true ) {
317319 if (!$ this ->is_valid ()) {
318320 return false ;
319321 }
@@ -322,7 +324,11 @@ public function is_ip($restricted=true) {
322324 return !!filter_var ($ this ->host , FILTER_VALIDATE_IP );
323325 }
324326
325- return !!filter_var ($ this ->host , FILTER_VALIDATE_IP , FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE );
327+ return !!filter_var (
328+ $ this ->host ,
329+ FILTER_VALIDATE_IP ,
330+ FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
331+ );
326332 }
327333
328334 /**
@@ -339,7 +345,11 @@ public function has_dns() {
339345 $ this ->dns = $ this ->is_ip (false );
340346 }
341347 else {
342- $ this ->dns = !!filter_var (gethostbyname ("{$ this ->host }. " ), FILTER_VALIDATE_IP , FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE );
348+ $ this ->dns = !!filter_var (
349+ gethostbyname ("{$ this ->host }. " ),
350+ FILTER_VALIDATE_IP ,
351+ FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
352+ );
343353 }
344354 }
345355
@@ -401,14 +411,14 @@ public function __call($method, $args) {
401411 preg_match_all ('/^get_(.+)$/ ' , $ method , $ matches );
402412 if (
403413 count ($ matches [0 ]) &&
404- 'dns ' !== $ matches [1 ][0 ] &&
414+ ( 'dns ' !== $ matches [1 ][0 ]) &&
405415 property_exists ($ this , $ matches [1 ][0 ])
406416 ) {
407417 $ variable = $ matches [1 ][0 ];
408418
409419 if (is_array ($ args ) && count ($ args )) {
410- $ args = common \data ::array_pop_top ($ args );
411- common \ ref \cast:: to_bool ($ args );
420+ $ args = c_data ::array_pop_top ($ args );
421+ r_cast:: bool ($ args );
412422 if ($ args ) {
413423 return $ this ->to_unicode ($ variable );
414424 }
@@ -417,7 +427,13 @@ public function __call($method, $args) {
417427 return $ this ->{$ variable };
418428 }
419429
420- throw new \Exception (sprintf ('The required method "%s" does not exist for %s ' , $ method , get_called_class ()));
430+ throw new \Exception (
431+ sprintf (
432+ 'The required method "%s" does not exist for %s ' ,
433+ $ method ,
434+ get_called_class ()
435+ )
436+ );
421437 }
422438
423439 /**
@@ -430,7 +446,7 @@ protected function to_unicode($key) {
430446 $ value = $ this ->{$ key };
431447 if (function_exists ('idn_to_utf8 ' ) && is_string ($ value )) {
432448 $ value = explode ('. ' , $ value );
433- common \ ref \file ::idn_to_utf8 ($ value );
449+ r_file ::idn_to_utf8 ($ value );
434450 return implode ('. ' , $ value );
435451 }
436452
@@ -443,9 +459,7 @@ protected function to_unicode($key) {
443459 * @param bool $unicode Unicode.
444460 * @return array|bool Host data or false.
445461 */
446- public function get_data ($ unicode =false ) {
447- common \ref \cast::to_bool ($ unicode , true );
448-
462+ public function get_data (bool $ unicode =false ) {
449463 if (!$ this ->is_valid ()) {
450464 return false ;
451465 }
0 commit comments