php-ip icon indicating copy to clipboard operation
php-ip copied to clipboard

Improvement - Collections for IPBlock and IP

Open bkuebler opened this issue 3 years ago • 3 comments

Add Classes which are working as Collections at least for IPBlock. These collection could contain one or many IPBlock Objects and could implement same methods like contain(), isIn(), and so on. And additional methods like aggregate() which gives you back a collection of aggregated IPBlocks. Also a method summarize() which gives you back an IPBlock Object where all given IPBlock Objects in a given collection belongs to. All these things I do always by my own classes, but I think this could also fit into this library and will improve them.

bkuebler avatar Jul 18 '21 18:07 bkuebler

Can you give an example? I'm not sure I understand what you want to do (compared to what the lib already offers).

rlanvin avatar Jul 18 '21 19:07 rlanvin

Okay let me try... The current IPBlock supports contain() isIn() and so on. Now imagine you have a list like an IP Blocklist Many IPs / Subnet prefixes let's say 100 Networks. Now you create for each network an IPBlock so you have now an array of 100 IPBlocks (IPBlockCollection) and you would like to check if each IPBlock contains in the other or vice versa to get an aggregated IPBlock array with only the elements which are not contained in any of the other blocks. This is the method aggregate() for example. The summarize() method give you just one IPBlock back / use the superblock method from IPBlock which contains all IPBlocks from the given array / collection.

It's complex but already a use case which i often use. Maybe I can create a code example which makes this easier to understand.

bkuebler avatar Jul 18 '21 20:07 bkuebler

Let me try to reformulate to see if I understand correctly.

  • You want to be able to group together a set of random IP addresses and blocks, not necessarily continuous, for example in case of an IP blocklist.
  • Then you want to be able to inspect this list and check if an IP is in it
  • aggregate would merge (?) the IP blocks that are continuous together, to optimise (?) the set?
  • summarize would give you the smallest IP block necessary to contain everything that is in this set?

Some code example would definitely be helpful.

rlanvin avatar Jul 22 '21 18:07 rlanvin