private function validateSubnet($ip)
{
if (!is_string($ip)) {
return [$this->message, []];
}
$negation = null;
$cidr = null;
$isCidrDefault = false;
if (preg_match($this->getIpParsePattern(), $ip, $matches)) {
$negation = $matches[1] !== '' ? $matches[1] : null;
$ip = $matches[2];
$cidr = isset($matches[4]) ? $matches[4] : null;
}
if ($this->subnet === true && $cidr === null) {
return [$this->noSubnet, []];
}
if ($this->subnet === false && $cidr !== null) {
return [$this->hasSubnet, []];
}
if ($this->negation === false && $negation !== null) {
return [$this->message, []];
}
if ($this->getIpVersion($ip) == 6) {
if ($cidr !== null) {
if ($cidr > static::IPV6_ADDRESS_LENGTH || $cidr < 0) {
return [$this->wrongCidr, []];
}
} else {
$isCidrDefault = true;
$cidr = static::IPV6_ADDRESS_LENGTH;
}
if (!$this->ipv6) {
return [$this->ipv6NotAllowed, []];
}
if (!$this->validateIPv6($ip)) {
return [$this->message, []];
}
if ($this->expandIPv6) {
$ip = $this->expandIPv6($ip);
}
} else {
if ($cidr !== null) {
if ($cidr > static::IPV4_ADDRESS_LENGTH || $cidr < 0) {
return [$this->wrongCidr, []];
}
} else {
$isCidrDefault = true;
$cidr = static::IPV4_ADDRESS_LENGTH;
}
if (!$this->ipv4) {
return [$this->ipv4NotAllowed, []];
}
if (!$this->validateIPv4($ip)) {
return [$this->message, []];
}
}
if (!$this->isAllowed($ip, $cidr)) {
return [$this->notInRange, []];
}
$result = $negation . $ip;
if ($this->subnet !== false && (!$isCidrDefault || $isCidrDefault && $this->normalize)) {
$result .= "/{$cidr}";
}
return $result;
}