RangeInterface.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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|numeric-string $n the offset of the address (support negative offset)
  43. *
  44. * @return \IPLib\Address\AddressInterface|null return NULL if $n is neither an integer nor a string containing a valid integer, or if the offset out of range
  45. *
  46. * @since 1.15.0
  47. * @since 1.21.0 $n can also be a numeric string
  48. *
  49. * @example passing 256 to the range 127.0.0.0/16 will result in 127.0.1.0
  50. * @example passing -1 to the range 127.0.1.0/16 will result in 127.0.255.255
  51. * @example passing 256 to the range 127.0.0.0/24 will result in NULL
  52. */
  53. public function getAddressAtOffset($n);
  54. /**
  55. * Check if this range contains an IP address.
  56. *
  57. * @param \IPLib\Address\AddressInterface $address
  58. *
  59. * @return bool
  60. */
  61. public function contains(AddressInterface $address);
  62. /**
  63. * Check if this range contains another range.
  64. *
  65. * @param \IPLib\Range\RangeInterface $range
  66. *
  67. * @return bool
  68. *
  69. * @since 1.5.0
  70. */
  71. public function containsRange(RangeInterface $range);
  72. /**
  73. * Get the initial address contained in this range.
  74. *
  75. * @return \IPLib\Address\AddressInterface
  76. *
  77. * @since 1.4.0
  78. */
  79. public function getStartAddress();
  80. /**
  81. * Get the final address contained in this range.
  82. *
  83. * @return \IPLib\Address\AddressInterface
  84. *
  85. * @since 1.4.0
  86. */
  87. public function getEndAddress();
  88. /**
  89. * Get a string representation of the starting address of this range than can be used when comparing addresses and ranges.
  90. *
  91. * @return string
  92. */
  93. public function getComparableStartString();
  94. /**
  95. * Get a string representation of the final address of this range than can be used when comparing addresses and ranges.
  96. *
  97. * @return string
  98. */
  99. public function getComparableEndString();
  100. /**
  101. * Get the subnet mask representing this range (only for IPv4 ranges).
  102. *
  103. * @return \IPLib\Address\IPv4|null return NULL if the range is an IPv6 range, the subnet mask otherwise
  104. *
  105. * @since 1.8.0
  106. */
  107. public function getSubnetMask();
  108. /**
  109. * Get the subnet/CIDR representation of this range.
  110. *
  111. * @return \IPLib\Range\Subnet
  112. *
  113. * @since 1.13.0
  114. */
  115. public function asSubnet();
  116. /**
  117. * Get the pattern/asterisk representation (if applicable) of this range.
  118. *
  119. * @return \IPLib\Range\Pattern|null return NULL if this range can't be represented by a pattern notation
  120. *
  121. * @since 1.13.0
  122. */
  123. public function asPattern();
  124. /**
  125. * Get the Reverse DNS Lookup Addresses of this IP range.
  126. *
  127. * @return string[]
  128. *
  129. * @since 1.13.0
  130. *
  131. * @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)
  132. * @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)
  133. */
  134. public function getReverseDNSLookupName();
  135. /**
  136. * Get the count of addresses contained in this IP range (possibly approximated).
  137. *
  138. * @return int|float If the number of addresses exceeds PHP_INT_MAX a float containing an approximation will be returned
  139. *
  140. * @since 1.16.0
  141. */
  142. public function getSize();
  143. /**
  144. * Get the exact count of addresses contained in this IP range.
  145. *
  146. * @return int|numeric-string If the number of addresses exceeds PHP_INT_MAX a string containing the exact number of addresses will be returned
  147. *
  148. * @since 1.21.0
  149. */
  150. public function getExactSize();
  151. /**
  152. * Get the "network prefix", that is how many bits of the address are dedicated to the network portion.
  153. *
  154. * @return int
  155. *
  156. * @since 1.19.0
  157. *
  158. * @example for 10.0.0.0/24 it's 24
  159. * @example for 10.0.0.* it's 24
  160. */
  161. public function getNetworkPrefix();
  162. /**
  163. * Split the range into smaller ranges.
  164. *
  165. * @param int $networkPrefix
  166. * @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)
  167. *
  168. * @throws \OutOfBoundsException if $networkPrefix is not valid
  169. *
  170. * @return \IPLib\Range\RangeInterface[]
  171. *
  172. * @since 1.19.0
  173. */
  174. public function split($networkPrefix, $forceSubnet = false);
  175. }