Single.php 5.8 KB

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