RangeInterface.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace IPLib\Range;
  3. use IPLib\Address\AddressInterface;
  4. /**
  5. * Interface of all the range types.
  6. */
  7. interface RangeInterface
  8. {
  9. /**
  10. * Get the short string representation of this address.
  11. *
  12. * @return string
  13. */
  14. public function __toString();
  15. /**
  16. * Get the string representation of this address.
  17. *
  18. * @param bool $long set to true to have a long/full representation, false otherwise
  19. *
  20. * @return string
  21. *
  22. * @example If $long is true, you'll get '0000:0000:0000:0000:0000:0000:0000:0001/128', '::1/128' otherwise.
  23. */
  24. public function toString($long = false);
  25. /**
  26. * Get the type of the IP addresses contained in this range.
  27. *
  28. * @return int One of the \IPLib\Address\Type::T_... constants
  29. */
  30. public function getAddressType();
  31. /**
  32. * Get the type of range of the IP address.
  33. *
  34. * @return int One of the \IPLib\Range\Type::T_... constants
  35. *
  36. * @since 1.5.0
  37. */
  38. public function getRangeType();
  39. /**
  40. * Get the address at a certain offset of this range.
  41. *
  42. * @param int $n the offset of the address (support negative offset)
  43. *
  44. * @return \IPLib\Address\AddressInterface|null return NULL if $n is not an integer or if the offset out of range
  45. *
  46. * @since 1.15.0
  47. *
  48. * @example passing 256 to the range 127.0.0.0/16 will result in 127.0.1.0
  49. * @example passing -1 to the range 127.0.1.0/16 will result in 127.0.255.255
  50. * @example passing 256 to the range 127.0.0.0/24 will result in NULL
  51. */
  52. public function getAddressAtOffset($n);
  53. /**
  54. * Check if this range contains an IP address.
  55. *
  56. * @param \IPLib\Address\AddressInterface $address
  57. *
  58. * @return bool
  59. */
  60. public function contains(AddressInterface $address);
  61. /**
  62. * Check if this range contains another range.
  63. *
  64. * @param \IPLib\Range\RangeInterface $range
  65. *
  66. * @return bool
  67. *
  68. * @since 1.5.0
  69. */
  70. public function containsRange(RangeInterface $range);
  71. /**
  72. * Get the initial address contained in this range.
  73. *
  74. * @return \IPLib\Address\AddressInterface
  75. *
  76. * @since 1.4.0
  77. */
  78. public function getStartAddress();
  79. /**
  80. * Get the final address contained in this range.
  81. *
  82. * @return \IPLib\Address\AddressInterface
  83. *
  84. * @since 1.4.0
  85. */
  86. public function getEndAddress();
  87. /**
  88. * Get a string representation of the starting address of this range than can be used when comparing addresses and ranges.
  89. *
  90. * @return string
  91. */
  92. public function getComparableStartString();
  93. /**
  94. * Get a string representation of the final address of this range than can be used when comparing addresses and ranges.
  95. *
  96. * @return string
  97. */
  98. public function getComparableEndString();
  99. /**
  100. * Get the subnet mask representing this range (only for IPv4 ranges).
  101. *
  102. * @return \IPLib\Address\IPv4|null return NULL if the range is an IPv6 range, the subnet mask otherwise
  103. *
  104. * @since 1.8.0
  105. */
  106. public function getSubnetMask();
  107. /**
  108. * Get the subnet/CIDR representation of this range.
  109. *
  110. * @return \IPLib\Range\Subnet
  111. *
  112. * @since 1.13.0
  113. */
  114. public function asSubnet();
  115. /**
  116. * Get the pattern/asterisk representation (if applicable) of this range.
  117. *
  118. * @return \IPLib\Range\Pattern|null return NULL if this range can't be represented by a pattern notation
  119. *
  120. * @since 1.13.0
  121. */
  122. public function asPattern();
  123. /**
  124. * Get the Reverse DNS Lookup Addresses of this IP range.
  125. *
  126. * @return string[]
  127. *
  128. * @since 1.13.0
  129. *
  130. * @example for IPv4 it returns something like array('x.x.x.x.in-addr.arpa', 'x.x.x.x.in-addr.arpa') (where the number of 'x.' ranges from 1 to 4)
  131. * @example for IPv6 it returns something like array('x.x.x.x..x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.ip6.arpa', 'x.x.x.x..x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.ip6.arpa') (where the number of 'x.' ranges from 1 to 32)
  132. */
  133. public function getReverseDNSLookupName();
  134. /**
  135. * Get the count of addresses this IP range contains.
  136. *
  137. * @return int|float Return float as for huge IPv6 networks, int is not enough
  138. *
  139. * @since 1.16.0
  140. */
  141. public function getSize();
  142. /**
  143. * Get the "network prefix", that is how many bits of the address are dedicated to the network portion.
  144. *
  145. * @return int
  146. *
  147. * @since 1.19.0
  148. *
  149. * @example for 10.0.0.0/24 it's 24
  150. * @example for 10.0.0.* it's 24
  151. */
  152. public function getNetworkPrefix();
  153. /**
  154. * Split the range into smaller ranges.
  155. *
  156. * @param int $networkPrefix
  157. * @param bool $forceSubnet set to true to always have ranges in "subnet format" (ie 1.2.3.4/5), to false to try to keep the original format if possible (that is, pattern to pattern, single to single)
  158. *
  159. * @throws \OutOfBoundsException if $networkPrefix is not valid
  160. *
  161. * @return \IPLib\Range\RangeInterface[]
  162. *
  163. * @since 1.19.0
  164. */
  165. public function split($networkPrefix, $forceSubnet = false);
  166. }