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