From 3ee2c28f197af15acb938ca0867e4f01d812f3f5 Mon Sep 17 00:00:00 2001 From: Tomoyuki Tsujimoto Date: Fri, 4 Jan 2019 22:14:18 +0900 Subject: [PATCH 1/3] Add factory static method --- src/SubnetCalculator.php | 12 ++++++++++++ tests/SubnetCalculatorTest.php | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/SubnetCalculator.php b/src/SubnetCalculator.php index a34e97b..133db99 100644 --- a/src/SubnetCalculator.php +++ b/src/SubnetCalculator.php @@ -63,6 +63,18 @@ 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 "192.168.1.120/24" + * @return SubnetCalculator + */ + public static function factory($ip_address_with_network_size) + { + $network = explode('/', $ip_address_with_network_size); + return new self($network[0], $network[1]); + } + /* **************** * * PUBLIC INTERFACE * **************** */ diff --git a/tests/SubnetCalculatorTest.php b/tests/SubnetCalculatorTest.php index 2e00327..67ccb2b 100644 --- a/tests/SubnetCalculatorTest.php +++ b/tests/SubnetCalculatorTest.php @@ -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); + } } From e5350bd11d063115ff89fc85a09bd092598e2df6 Mon Sep 17 00:00:00 2001 From: Tomoyuki Tsujimoto Date: Fri, 4 Jan 2019 22:15:47 +0900 Subject: [PATCH 2/3] Add readme of factory --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index c7eebef..7c950c2 100644 --- a/README.md +++ b/README.md @@ -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 From 509a1a6c9b0b20fab68210fc3f123ae012420de8 Mon Sep 17 00:00:00 2001 From: Tomoyuki Tsujimoto Date: Fri, 4 Jan 2019 22:48:18 +0900 Subject: [PATCH 3/3] Update PHPDocs --- src/SubnetCalculator.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/SubnetCalculator.php b/src/SubnetCalculator.php index 133db99..e390a94 100644 --- a/src/SubnetCalculator.php +++ b/src/SubnetCalculator.php @@ -66,13 +66,14 @@ public function __construct($ip_address, $network_size, SubnetReportInterface $r /** * SubnetCalculator factory when IP address is written with subnetmask (ex. "192.168.1.120/24") * - * @param string $ip_address "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) + 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]); + return new self($network[0], $network[1], $report); } /* **************** *