2626
2727namespace easyops \easykin \core ;
2828
29+ use InvalidArgumentException ;
2930
3031/**
3132 * Class Endpoint
@@ -45,14 +46,24 @@ private function __construct()
4546
4647 /**
4748 * @param string $serviceName
48- * @param string $ipv4
49+ * @param string $ip
4950 * @param int $port
5051 */
51- public static function init ($ serviceName , $ ipv4 , $ port )
52+ public static function init ($ serviceName , $ ip , $ port )
5253 {
54+ $ isIPv6 = filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 );
55+ if (! $ isIPv6 ) {
56+ if (filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 ) === false ) {
57+ throw new InvalidArgumentException (
58+ sprintf ('Invalid IPv4. Expected something in the range 0.0.0.0 and 255.255.255.255, got %s ' , $ ip )
59+ );
60+ }
61+ }
62+
5363 static ::$ endpoint = [
5464 'serviceName ' => $ serviceName ,
55- 'ipv4 ' => $ ipv4 ,
65+ 'ipv4 ' => !$ isIPv6 ? $ ip : null ,
66+ 'ipv6 ' => $ isIPv6 ? $ ip : null ,
5667 'port ' => $ port ,
5768 ];
5869 }
@@ -63,7 +74,7 @@ public static function init($serviceName, $ipv4, $port)
6374 public static function toArray ()
6475 {
6576 if (!isset (static ::$ endpoint ['serviceName ' ]) ||
66- !isset (static ::$ endpoint ['ipv4 ' ]) ||
77+ ( !isset (static ::$ endpoint ['ipv4 ' ]) && ! isset ( static :: $ endpoint [ ' ipv6 ' ]) ) ||
6778 !isset (static ::$ endpoint ['port ' ])) {
6879 throw new \BadMethodCallException ('Endpoint not initialized. ' );
6980 }
0 commit comments