11<?php
22/**
33 * @link https://github.com/phpviet/validation
4+ *
45 * @copyright (c) PHP Viet
56 * @license [MIT](https://opensource.org/licenses/MIT)
67 */
78
89namespace PHPViet \Validation \Rules ;
910
10- use IPLib \Factory as IpFactory ;
11- use IPLib \Address \Type as IpType ;
1211use IPLib \Address \AddressInterface as IpInterface ;
12+ use IPLib \Address \Type as IpType ;
13+ use IPLib \Factory as IpFactory ;
1314use Respect \Validation \Rules \AbstractRule ;
1415
1516/**
1617 * @author Vuong Minh <vuongxuongminh@gmail.com>
18+ *
1719 * @since 1.0.0
1820 */
1921class IpVN extends AbstractRule
@@ -32,17 +34,14 @@ public function __construct($version = null)
3234 public function validate ($ input )
3335 {
3436 if (!$ ip = IpFactory::addressFromString ($ input )) {
35-
3637 return false ;
3738 }
3839
3940 if (($ version = $ ip ->getAddressType ()) !== $ this ->version && null !== $ this ->version ) {
40-
4141 return false ;
4242 }
4343
4444 if (!$ ranges = $ this ->getIpRanges ($ input , $ version )) {
45-
4645 return false ;
4746 }
4847
@@ -60,15 +59,13 @@ protected function getIpRanges(string $ip, int $version): ?array
6059 }
6160
6261 while (!is_null ($ key = array_shift ($ keys ))) {
63-
6462 if (isset ($ map [$ key ])) {
6563 $ map = $ map [$ key ];
6664
6765 continue ;
6866 }
6967
7068 if (isset ($ map ['range ' ])) {
71-
7269 return $ map ['range ' ];
7370 }
7471
@@ -79,11 +76,9 @@ protected function getIpRanges(string $ip, int $version): ?array
7976 protected function validateIpInRange (IpInterface $ ip , array $ ranges )
8077 {
8178 foreach ($ ranges as $ range ) {
82-
8379 [$ begin , $ end ] = $ range ;
8480
8581 if (($ subnet = IpFactory::rangeFromBoundaries ($ begin , $ end )) && $ subnet ->contains ($ ip )) {
86-
8782 return true ;
8883 }
8984 }
@@ -96,7 +91,7 @@ protected static function getIpV4Range(): array
9691 static $ range = null ;
9792
9893 if (null === $ range ) {
99- $ range = require ( __DIR__ . '/../../resource/ip-v4-range.php ' ) ;
94+ $ range = require __DIR__ . '/../../resource/ip-v4-range.php ' ;
10095 }
10196
10297 return $ range ;
@@ -107,10 +102,9 @@ protected static function getIpV6Range(): array
107102 static $ range = null ;
108103
109104 if (null === $ range ) {
110- $ range = require ( __DIR__ . '/../../resource/ip-v6-range.php ' ) ;
105+ $ range = require __DIR__ . '/../../resource/ip-v6-range.php ' ;
111106 }
112107
113108 return $ range ;
114109 }
115-
116110}
0 commit comments