Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SubnetCalculator.php #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/SubnetCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public function getBroadcastAddress()
*/
public function getMinHost()
{
if ($this->network_size === 32 || $this->network_size === 31) {
return $this->ip_address;
if ($this->network_size > 30) {
return $this->getNetworkPortion();
}
return $this->minHostCalculation(self::FORMAT_QUADS, '.');
}
Expand Down Expand Up @@ -253,9 +253,12 @@ function ($quad) {
*/
public function getMaxHost()
{
if ($this->network_size === 32 || $this->network_size === 31) {
return $this->ip_address;
if ($this->network_size == 32) {
return $this->getNetworkPortion();
} elseif ($this->network_size == 31) {
return $this->addIps($this->getNetworkPortion(), 1);
}

return $this->maxHostCalculation(self::FORMAT_QUADS, '.');
}

Expand Down Expand Up @@ -604,6 +607,18 @@ private function subnetCalculation($format, $separator = '')

return implode($separator, $mask_quads);
}

/**
* Return next (or more) valid IP
*
* @param string $ip IP input
* @param int $num Number to add, default 1
*
* @return string formatted subnet mask
*/
private function addIps($ip, $num=1) {
return long2ip(ip2long($ip)+$num);
}

/**
* Calculate network portion for formatting
Expand Down