Single.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace IPLib\Range;
  3. use IPLib\Address\AddressInterface;
  4. use IPLib\Address\IPv4;
  5. use IPLib\Address\Type as AddressType;
  6. use IPLib\Factory;
  7. use IPLib\ParseStringFlag;
  8. /**
  9. * Represents a single address (eg a range that contains just one address).
  10. *
  11. * @example 127.0.0.1
  12. * @example ::1
  13. */
  14. class Single extends AbstractRange
  15. {
  16. /**
  17. * @var \IPLib\Address\AddressInterface
  18. */
  19. protected $address;
  20. /**
  21. * Initializes the instance.
  22. *
  23. * @param \IPLib\Address\AddressInterface $address
  24. */
  25. protected function __construct(AddressInterface $address)
  26. {
  27. $this->address = $address;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. *
  32. * @see \IPLib\Range\RangeInterface::__toString()
  33. */
  34. public function __toString()
  35. {
  36. return $this->address->__toString();
  37. }
  38. /**
  39. * @deprecated since 1.17.0: use the parseString() method instead.
  40. * For upgrading:
  41. * - if $supportNonDecimalIPv4 is true, use the ParseStringFlag::IPV4_MAYBE_NON_DECIMAL flag
  42. *
  43. * @param string|mixed $range
  44. * @param bool $supportNonDecimalIPv4
  45. *
  46. * @return static|null
  47. *
  48. * @see \IPLib\Range\Single::parseString()
  49. * @since 1.10.0 added the $supportNonDecimalIPv4 argument
  50. */
  51. public static function fromString($range, $supportNonDecimalIPv4 = false)
  52. {
  53. return static::parseString($range, ParseStringFlag::MAY_INCLUDE_PORT | ParseStringFlag::MAY_INCLUDE_ZONEID | ($supportNonDecimalIPv4 ? ParseStringFlag::IPV4_MAYBE_NON_DECIMAL : 0));
  54. }
  55. /**
  56. * Try get the range instance starting from its string representation.
  57. *
  58. * @param string|mixed $range
  59. * @param int $flags A combination or zero or more flags
  60. *
  61. * @return static|null
  62. *
  63. * @see \IPLib\ParseStringFlag
  64. * @since 1.17.0
  65. */
  66. public static function parseString($range, $flags = 0)
  67. {
  68. $result = null;
  69. $flags = (int) $flags;
  70. $address = Factory::parseAddressString($range, $flags);
  71. if ($address !== null) {
  72. $result = new static($address);
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Create the range instance starting from an address instance.
  78. *
  79. * @param \IPLib\Address\AddressInterface $address
  80. *
  81. * @return static
  82. *
  83. * @since 1.2.0
  84. */
  85. public static function fromAddress(AddressInterface $address)
  86. {
  87. return new static($address);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. *
  92. * @see \IPLib\Range\RangeInterface::toString()
  93. */
  94. public function toString($long = false)
  95. {
  96. return $this->address->toString($long);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. *
  101. * @see \IPLib\Range\RangeInterface::getAddressType()
  102. */
  103. public function getAddressType()
  104. {
  105. return $this->address->getAddressType();
  106. }
  107. /**
  108. * {@inheritdoc}
  109. *
  110. * @see \IPLib\Range\RangeInterface::getRangeType()
  111. */
  112. public function getRangeType()
  113. {
  114. return $this->address->getRangeType();
  115. }
  116. /**
  117. * {@inheritdoc}
  118. *
  119. * @see \IPLib\Range\RangeInterface::contains()
  120. */
  121. public function contains(AddressInterface $address)
  122. {
  123. $result = false;
  124. if ($address->getAddressType() === $this->getAddressType()) {
  125. if ($address->toString(false) === $this->address->toString(false)) {
  126. $result = true;
  127. }
  128. }
  129. return $result;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. *
  134. * @see \IPLib\Range\RangeInterface::getStartAddress()
  135. */
  136. public function getStartAddress()
  137. {
  138. return $this->address;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. *
  143. * @see \IPLib\Range\RangeInterface::getEndAddress()
  144. */
  145. public function getEndAddress()
  146. {
  147. return $this->address;
  148. }
  149. /**
  150. * {@inheritdoc}
  151. *
  152. * @see \IPLib\Range\RangeInterface::getComparableStartString()
  153. */
  154. public function getComparableStartString()
  155. {
  156. return $this->address->getComparableString();
  157. }
  158. /**
  159. * {@inheritdoc}
  160. *
  161. * @see \IPLib\Range\RangeInterface::getComparableEndString()
  162. */
  163. public function getComparableEndString()
  164. {
  165. return $this->address->getComparableString();
  166. }
  167. /**
  168. * {@inheritdoc}
  169. *
  170. * @see \IPLib\Range\RangeInterface::asSubnet()
  171. */
  172. public function asSubnet()
  173. {
  174. return new Subnet($this->address, $this->address, $this->getNetworkPrefix());
  175. }
  176. /**
  177. * {@inheritdoc}
  178. *
  179. * @see \IPLib\Range\RangeInterface::asPattern()
  180. */
  181. public function asPattern()
  182. {
  183. return new Pattern($this->address, $this->address, 0);
  184. }
  185. /**
  186. * {@inheritdoc}
  187. *
  188. * @see \IPLib\Range\RangeInterface::getSubnetMask()
  189. */
  190. public function getSubnetMask()
  191. {
  192. if ($this->getAddressType() !== AddressType::T_IPv4) {
  193. return null;
  194. }
  195. return IPv4::fromBytes(array(255, 255, 255, 255));
  196. }
  197. /**
  198. * {@inheritdoc}
  199. *
  200. * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName()
  201. */
  202. public function getReverseDNSLookupName()
  203. {
  204. return array($this->getStartAddress()->getReverseDNSLookupName());
  205. }
  206. /**
  207. * {@inheritdoc}
  208. *
  209. * @see \IPLib\Range\RangeInterface::getSize()
  210. */
  211. public function getSize()
  212. {
  213. return 1;
  214. }
  215. /**
  216. * {@inheritdoc}
  217. *
  218. * @see \IPLib\Range\RangeInterface::getNetworkPrefix()
  219. */
  220. public function getNetworkPrefix()
  221. {
  222. switch ($this->getAddressType()) {
  223. case AddressType::T_IPv4:
  224. return 32;
  225. case AddressType::T_IPv6:
  226. return 128;
  227. }
  228. }
  229. }