1<?php
2
3namespace IPLib\Range;
4
5use IPLib\Address\AddressInterface;
6use IPLib\Address\IPv4;
7use IPLib\Address\Type as AddressType;
8use IPLib\Factory;
9
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 */
16class Single implements RangeInterface
17{
18    /**
19     * @var \IPLib\Address\AddressInterface
20     */
21    protected $address;
22
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    /**
34     * Try get the range instance starting from its string representation.
35     *
36     * @param string|mixed $range
37     *
38     * @return static|null
39     */
40    public static function fromString($range)
41    {
42        $result = null;
43        $address = Factory::addressFromString($range);
44        if ($address !== null) {
45            $result = new static($address);
46        }
47
48        return $result;
49    }
50
51    /**
52     * Create the range instance starting from an address instance.
53     *
54     * @param \IPLib\Address\AddressInterface $address
55     *
56     * @return static
57     */
58    public static function fromAddress(AddressInterface $address)
59    {
60        return new static($address);
61    }
62
63    /**
64     * {@inheritdoc}
65     *
66     * @see \IPLib\Range\RangeInterface::toString()
67     */
68    public function toString($long = false)
69    {
70        return $this->address->toString($long);
71    }
72
73    /**
74     * {@inheritdoc}
75     *
76     * @see \IPLib\Range\RangeInterface::__toString()
77     */
78    public function __toString()
79    {
80        return $this->address->__toString();
81    }
82
83    /**
84     * {@inheritdoc}
85     *
86     * @see \IPLib\Range\RangeInterface::getAddressType()
87     */
88    public function getAddressType()
89    {
90        return $this->address->getAddressType();
91    }
92
93    /**
94     * {@inheritdoc}
95     *
96     * @see \IPLib\Range\RangeInterface::getRangeType()
97     */
98    public function getRangeType()
99    {
100        return $this->address->getRangeType();
101    }
102
103    /**
104     * {@inheritdoc}
105     *
106     * @see \IPLib\Range\RangeInterface::contains()
107     */
108    public function contains(AddressInterface $address)
109    {
110        $result = false;
111        if ($address->getAddressType() === $this->getAddressType()) {
112            if ($address->toString(false) === $this->address->toString(false)) {
113                $result = true;
114            }
115        }
116
117        return $result;
118    }
119
120    /**
121     * {@inheritdoc}
122     *
123     * @see \IPLib\Range\RangeInterface::containsRange()
124     */
125    public function containsRange(RangeInterface $range)
126    {
127        $result = false;
128        if ($range->getAddressType() === $this->getAddressType()) {
129            if ($range->toString(false) === $this->toString(false)) {
130                $result = true;
131            }
132        }
133
134        return $result;
135    }
136
137    /**
138     * {@inheritdoc}
139     *
140     * @see \IPLib\Range\RangeInterface::getStartAddress()
141     */
142    public function getStartAddress()
143    {
144        return $this->address;
145    }
146
147    /**
148     * {@inheritdoc}
149     *
150     * @see \IPLib\Range\RangeInterface::getEndAddress()
151     */
152    public function getEndAddress()
153    {
154        return $this->address;
155    }
156
157    /**
158     * {@inheritdoc}
159     *
160     * @see \IPLib\Range\RangeInterface::getComparableStartString()
161     */
162    public function getComparableStartString()
163    {
164        return $this->address->getComparableString();
165    }
166
167    /**
168     * {@inheritdoc}
169     *
170     * @see \IPLib\Range\RangeInterface::getComparableEndString()
171     */
172    public function getComparableEndString()
173    {
174        return $this->address->getComparableString();
175    }
176
177    /**
178     * {@inheritdoc}
179     *
180     * @see \IPLib\Range\RangeInterface::getSubnetMask()
181     */
182    public function getSubnetMask()
183    {
184        if ($this->getAddressType() !== AddressType::T_IPv4) {
185            return null;
186        }
187
188        return IPv4::fromBytes(array(255, 255, 255, 255));
189    }
190}
191