Skip to content

Commit

Permalink
fix moveTo(); update tests; update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
S1lentium committed Aug 21, 2016
1 parent f95451d commit cb4843d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 133 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: php
php:
# aliased to a recent 5.4.x version
- 5.4
# aliased to a recent 5.5.x version
- 5.5
# aliased to a recent 5.6.x version
php:
- 5.4
- 5.5
- 5.6
- 7.0

before_script:
- wget http://getcomposer.org/composer.phar
Expand Down
86 changes: 38 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ PHP Library for manipulating network addresses (IPv4 and IPv6).
[![Build Status](https://travis-ci.org/S1lentium/IPTools.svg)](https://travis-ci.org/S1lentium/IPTools)
[![Coverage Status](https://coveralls.io/repos/S1lentium/IPTools/badge.svg?branch=master&service=github)](https://coveralls.io/github/S1lentium/IPTools?branch=master)
[![Code Climate](https://codeclimate.com/github/S1lentium/IPTools/badges/gpa.svg)](https://codeclimate.com/github/S1lentium/IPTools)
[![PHP 5.4](https://img.shields.io/badge/PHP-5.4-8892BF.svg)](http://php.net)

## Installation
Composer:
Run in command line:
```
composer require s1lentium/iptools
```
or put in composer.json:
```json
{
"require": {
Expand All @@ -29,7 +35,7 @@ $ip = new IP('fc00::');
echo $ip->version; // IPv6
```

**Parsing IP from integer, binary and hex representation:**
**Parsing IP from integer, binary and hex:**
```php
echo (string)IP::parse(2130706433); // 127.0.0.1
echo (string)IP::parse('0b11000000101010000000000100000001') // 192.168.1.1
Expand Down Expand Up @@ -64,49 +70,14 @@ echo new IP::parse('192.0.2.5')->reversePointer // 5.2.0.192.in-addr.arpa
echo new IP::parse('2001:db8::567:89ab')->reversePointer // b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
```

### Networking
```php
$network = new Network(new IP('192.168.1.1'), new IP('255.255.255.0'));
print_r($network->info);
```
Array
(
[IP] => 192.168.1.1
[netmask] => 255.255.255.0
[network] => 192.168.1.0
[prefixLength] => 24
[CIDR] => 192.168.1.0/24
[wildcard] => 0.0.0.255
[broadcast] => 192.168.1.255
[firstIP] => 192.168.1.0
[lastIP] => 192.168.1.255
[blockSize] => 256
[firstHost] => 192.168.1.1
[lastHost] => 192.168.1.254
[hostsCount] => 254
)


### Network Operations
```php
echo Network::parse('192.0.0.1 255.0.0.0')->CIDR; // 192.0.0.0/8
echo (string)Network::parse('192.0.0.1/8')->netmask; // 255.0.0.0
echo (string)Network::parse('192.0.0.1'); // 192.0.0.1/32
```

**Iterate over Network`s Host-IPs:**
```php
$network = Network::parse('192.168.1.0/24');
foreach($network as $ip) {
echo (string)$ip . '<br>';
}
```
192.168.1.1
...
192.168.1.254



**Excluding IP from Network:**
**Exclude IP from Network:**
```php
$excluded = Network::parse('192.0.0.0/8')->exclude(new IP('192.168.1.1'));
foreach($excluded as $network) {
Expand All @@ -122,8 +93,7 @@ foreach($excluded as $network) {
...
192.192.0.0/10


**Excluding Subnet from Network:**
**Exclude Subnet from Network:**
```php
$excluded = Network::parse('192.0.0.0/8')->exclude(new Network('192.168.1.0/24'));
foreach($excluded as $network) {
Expand All @@ -138,7 +108,7 @@ foreach($excluded as $network) {
...
192.192.0.0/10

**Splitting network into equal subnets**
**Split network into equal subnets**
```php
$networks = Network::parse('192.168.0.0/22')->moveTo('24');
foreach ($networks as $network) {
Expand All @@ -150,13 +120,35 @@ foreach ($networks as $network) {
192.168.2.0/24
192.168.3.0/24

**Count of Host-IPs**
**Iterate over Network IP adresses:**
```php
$network = Network::parse('192.168.1.0/24');
foreach($network as $ip) {
echo (string)$ip . '<br>';
}
```
192.168.1.0
...
192.168.1.255

**Get Network hosts adresses as Range:**
```php
$hosts = Network::parse('192.168.1.0/24')->hosts // Range(192.168.1.1, 192.168.1.254);
foreach($hosts as $ip) {
echo (string)$ip . '<br>';
}
```
192.168.1.1
...
192.168.1.254

**Count Network IP adresses**
```php
echo count(Network::parse('192.168.1.0/24')) // 254
```

### Range Operations
**Defining a range in different formats:**
**Define the range in different formats:**
```php
$range = new Range(new IP('192.168.1.0'), new IP('192.168.1.255'));
$range = Range::parse('192.168.1.0-192.168.1.255');
Expand All @@ -169,7 +161,7 @@ echo Range::parse('192.168.1.1-192.168.1.254')->contains(new IP('192.168.1.5'));
echo Range::parse('::1-::ffff')->contains(new IP('::1234')); // true
```

**Iterating over Range IPs:**
**Iterate over Range IP adresses:**
```php
$range = Range::parse('192.168.1.1-192.168.1.254');
foreach($range as $ip) {
Expand All @@ -180,8 +172,7 @@ foreach($range as $ip) {
...
192.168.1.254


**Get Networks that fit into a specified range of IPs:**
**Get Networks that fit into a specified range of IP Adresses:**
```php
$networks = Range::parse('192.168.1.1-192.168.1.254')->getNetworks();

Expand All @@ -204,8 +195,7 @@ foreach($networks as $network) {
192.168.1.252/31
192.168.1.254/32


**Count of IPs in Range**
**Count IP adresses in Range**
```php
echo count(Range::parse('192.168.1.1-192.168.1.254')) // 254
```
Expand Down
64 changes: 12 additions & 52 deletions src/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,55 +229,17 @@ public function getBlockSize()
*/
public function getHosts()
{
return new Range($this->getFirstHost, $this->getLastHost);
}

/**
* @return IP
*/
public function getFirstHost()
{
$network = $this->getNetwork();
$firstHost = $this->getNetwork();
$lastHost = $this->getBroadcast();

if ($network->getVersion() === IP::IP_V4) {
if ($this->getBlockSize() > 2) {
return IP::parseBin(substr($network->toBin(), 0, $network->getMaxPrefixLength() - 1) . '1');
}
}

return $network;

}

/**
* @return IP
*/
public function getLastHost()
{
$broadcast = $this->getBroadcast();

if ($broadcast->getVersion() === IP::IP_V4) {
if ($this->ip->getVersion() === IP::IP_V4) {
if ($this->getBlockSize() > 2) {
return IP::parseBin(substr($broadcast->toBin(), 0, $broadcast->getMaxPrefixLength() - 1) . '0');
$firstHost = IP::parseBin(substr($firstHost->toBin(), 0, $firstHost->getMaxPrefixLength() - 1) . '1');
$lastHost = IP::parseBin(substr($lastHost->toBin(), 0, $lastHost->getMaxPrefixLength() - 1) . '0');
}
}

return $broadcast;

}

/**
* @return int|string
*/
public function getHostsCount()
{
$blockSize = $this->getBlockSize();

if ($this->ip->getVersion() === IP::IP_V4) {
return $blockSize > 2 ? $blockSize - 2 : $blockSize;
}

return $blockSize;
return new Range($firstHost, $lastHost);
}

/**
Expand Down Expand Up @@ -342,17 +304,15 @@ public function moveTo($prefixLength)
throw new \Exception('Invalid prefix length ');
}

$prefixAsMask = self::prefix2netmask($prefixLength, $this->ip->getVersion());
$netmask = self::prefix2netmask($prefixLength, $this->ip->getVersion());
$networks = array();

$current = clone $this;
$current->setPrefixLength($prefixLength);

$last = new self($this->getLastHost(), $prefixAsMask);
$subnet = clone $this;
$subnet->setPrefixLength($prefixLength);

while ($current->ip->inAddr() < $last->ip->inAddr()) {
$networks[] = $current;
$current = new self($current->getLastIP()->next(), $prefixAsMask);
while ($subnet->ip->inAddr() < $this->getLastIP()->inAddr()) {
$networks[] = $subnet;
$subnet = new self($subnet->getLastIP()->next(), $netmask);
}

return $networks;
Expand Down
28 changes: 9 additions & 19 deletions tests/NetworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public function testProperties()

$this->assertEquals('192.0.0.2', $network->ip);
$this->assertEquals('192.0.0.0/24', (string)$network);
$this->assertEquals('192.0.0.1', (string)$network->firstHost);
$this->assertEquals('192.0.0.254', (string)$network->lastHost);
$this->assertEquals('0.0.0.255', (string)$network->wildcard);
$this->assertEquals('192.0.0.0', (string)$network->firstIP);
$this->assertEquals('192.0.0.255', (string)$network->lastIP);
}

/**
* @dataProvider getTestParseData
*/
public function testParse($data, $expected)
{
$network = Network::parse($data);
$this->assertEquals($expected, (string)$network);
$this->assertEquals($expected, (string)Network::parse($data));
}

/**
Expand Down Expand Up @@ -73,9 +73,7 @@ public function testPrefix2MaskInvalidPrefix($prefix, $version)
*/
public function testHosts($data, $expected)
{
$hosts = Network::parse($data)->hosts;

foreach($hosts as $ip) {
foreach(Network::parse($data)->getHosts as $ip) {
$result[] = (string)$ip;
}

Expand All @@ -87,11 +85,9 @@ public function testHosts($data, $expected)
*/
public function testExclude($data, $exclude, $expected)
{
$excluded = Network::parse($data)->exclude($exclude);

$result = array();

foreach($excluded as $network) {
foreach(Network::parse($data)->exclude($exclude) as $network) {
$result[] = (string)$network;
}

Expand All @@ -112,11 +108,9 @@ public function testExcludeException()
*/
public function testMoveTo($network, $prefixLength, $expected)
{
$networks = Network::parse($network)->moveTo($prefixLength);

$result = array();

foreach ($networks as $network) {
foreach (Network::parse($network)->moveTo($prefixLength) as $network) {
$result[] = (string)$network;
}

Expand All @@ -138,9 +132,7 @@ public function testMoveToException($network, $prefixLength)
*/
public function testNetworkIteration($data, $expected)
{
$network = Network::parse($data);

foreach ($network as $ip) {
foreach (Network::parse($data) as $key => $ip) {
$result[] = (string)$ip;
}

Expand All @@ -152,9 +144,7 @@ public function testNetworkIteration($data, $expected)
*/
public function testCount($data, $expected)
{
$network = Network::parse($data);

$this->assertEquals($expected, count($network));
$this->assertEquals($expected, count(Network::parse($data)));
}

public function getTestParseData()
Expand Down
10 changes: 2 additions & 8 deletions tests/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function testParse($data, $expected)
*/
public function testGetNetworks($data, $expected)
{
$range = Range::parse($data);

$result = array();

foreach (Range::parse($data)->getNetworks() as $network) {
Expand All @@ -46,9 +44,7 @@ public function testContains($data, $find, $expected)
*/
public function testRangeIteration($data, $expected)
{
$range = Range::parse($data);

foreach ($range as $ip) {
foreach (Range::parse($data) as $key => $ip) {
$result[] = (string)$ip;
}

Expand All @@ -60,9 +56,7 @@ public function testRangeIteration($data, $expected)
*/
public function testCount($data, $expected)
{
$range = Range::parse($data);

$this->assertEquals($expected, count($range));
$this->assertEquals($expected, count(Range::parse($data)));
}

public function getTestParseData()
Expand Down

0 comments on commit cb4843d

Please sign in to comment.