Skip to content

Commit 91b73f9

Browse files
authored
Merge pull request #26 from leth/toArray
Add a toArray method to NetworkAddresses
2 parents c5ff003 + d62d9aa commit 91b73f9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

classes/Leth/IPAddress/IP/NetworkAddress.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,18 @@ public function getIterator()
572572
return new NetworkAddressIterator($this);
573573
}
574574

575+
/**
576+
* Get array of addresses in this network
577+
*
578+
* Warning: May use a lot of memory if used with large networks.
579+
* Consider using an iterator and the count() method instead.
580+
* @return array
581+
*/
582+
public function toArray()
583+
{
584+
return iterator_to_array($this, false);
585+
}
586+
575587
/**
576588
* Get count addresses in this network
577589
* Implement \Countable

tests/IPv4NetworkAddressTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public function testIteratorInterface()
144144
$actual[] = (string)$ip;
145145
}
146146
$this->assertEquals($expected, $actual);
147+
$this->assertEquals($expected, array_map('strval', $block->toArray()));
147148
}
148149

149150
public function testTwoIterators()

0 commit comments

Comments
 (0)