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