Skip to content

Commit

Permalink
Merge 509a1a6 into 9408f65
Browse files Browse the repository at this point in the history
  • Loading branch information
tomothumb committed Jan 4, 2019
2 parents 9408f65 + 509a1a6 commit 8c52ade
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -78,6 +78,9 @@ Usage
```php
// For network 192.168.112.203/23
$sub = new IPv4\SubnetCalculator('192.168.112.203', 23);
// Or
$sub = IPv4\SubnetCalculator::factory('192.168.112.203/23');

```

### Various Network Information
Expand Down
13 changes: 13 additions & 0 deletions src/SubnetCalculator.php
Expand Up @@ -63,6 +63,19 @@ public function __construct($ip_address, $network_size, SubnetReportInterface $r
$this->report = $report ?: new SubnetReport();
}

/**
* SubnetCalculator factory when IP address is written with subnetmask (ex. "192.168.1.120/24")
*
* @param string $ip_address_with_network_size "192.168.1.120/24"
* @param SubnetReportInterface|null $report
* @return SubnetCalculator
*/
public static function factory($ip_address_with_network_size, SubnetReportInterface $report = null)
{
$network = explode('/', $ip_address_with_network_size);
return new self($network[0], $network[1], $report);
}

/* **************** *
* PUBLIC INTERFACE
* **************** */
Expand Down
12 changes: 12 additions & 0 deletions tests/SubnetCalculatorTest.php
Expand Up @@ -1551,4 +1551,16 @@ public function dataProviderForIpAddressesNotInSubnet()
['192.168.112.203', 32, '192.168.112.204'],
];
}

/**
* @testCase factory
*/
public function testSubnetCalculatorFactory()
{
// Given
$instance = IPv4\SubnetCalculator::factory('192.168.1.1/24');

// Then
$this->assertInstanceOf('IPv4\SubnetCalculator', $instance);
}
}

0 comments on commit 8c52ade

Please sign in to comment.