| @@ 17-251 (lines=235) @@ | ||
| 14 | use GpsLab\Component\Interval\IntervalInterface; |
|
| 15 | use GpsLab\Component\Interval\IntervalType; |
|
| 16 | ||
| 17 | class IPv4Interval implements IntervalInterface |
|
| 18 | { |
|
| 19 | /** |
|
| 20 | * @var string |
|
| 21 | */ |
|
| 22 | const REGEXP = '/^ |
|
| 23 | (?:\(|\[) # start type char |
|
| 24 | \s* |
|
| 25 | (?<start>\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}) # start point |
|
| 26 | \s*,\s* # separator |
|
| 27 | (?<end>\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}) # end point |
|
| 28 | \s* |
|
| 29 | (?:\)|\]) # end type char |
|
| 30 | $/x'; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @var IntervalType |
|
| 34 | */ |
|
| 35 | private $type; |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @var IPv4IntervalComparator |
|
| 39 | */ |
|
| 40 | private $comparator; |
|
| 41 | ||
| 42 | /** |
|
| 43 | * @var IPv4IntervalPoint |
|
| 44 | */ |
|
| 45 | private $start; |
|
| 46 | ||
| 47 | /** |
|
| 48 | * @var IPv4IntervalPoint |
|
| 49 | */ |
|
| 50 | private $end; |
|
| 51 | ||
| 52 | /** |
|
| 53 | * @param IPv4IntervalPoint $start |
|
| 54 | * @param IPv4IntervalPoint $end |
|
| 55 | * @param IntervalType $type |
|
| 56 | */ |
|
| 57 | private function __construct(IPv4IntervalPoint $start, IPv4IntervalPoint $end, IntervalType $type) |
|
| 58 | { |
|
| 59 | if ($start->gte($end)) { |
|
| 60 | throw IncorrectIntervalException::create(); |
|
| 61 | } |
|
| 62 | ||
| 63 | $this->type = $type; |
|
| 64 | $this->start = $start; |
|
| 65 | $this->end = $end; |
|
| 66 | $this->comparator = new IPv4IntervalComparator($this); |
|
| 67 | } |
|
| 68 | ||
| 69 | /** |
|
| 70 | * @param string $start |
|
| 71 | * @param string $end |
|
| 72 | * @param IntervalType $type |
|
| 73 | * |
|
| 74 | * @return self |
|
| 75 | */ |
|
| 76 | public static function create($start, $end, IntervalType $type) |
|
| 77 | { |
|
| 78 | return new self(new IPv4IntervalPoint($start), new IPv4IntervalPoint($end), $type); |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * @param string $start |
|
| 83 | * @param string $end |
|
| 84 | * |
|
| 85 | * @return self |
|
| 86 | */ |
|
| 87 | public static function closed($start, $end) |
|
| 88 | { |
|
| 89 | return static::create($start, $end, IntervalType::closed()); |
|
| 90 | } |
|
| 91 | ||
| 92 | /** |
|
| 93 | * @param string $start |
|
| 94 | * @param string $end |
|
| 95 | * |
|
| 96 | * @return self |
|
| 97 | */ |
|
| 98 | public static function halfClosed($start, $end) |
|
| 99 | { |
|
| 100 | return static::create($start, $end, IntervalType::halfClosed()); |
|
| 101 | } |
|
| 102 | ||
| 103 | /** |
|
| 104 | * @param string $start |
|
| 105 | * @param string $end |
|
| 106 | * |
|
| 107 | * @return self |
|
| 108 | */ |
|
| 109 | public static function halfOpen($start, $end) |
|
| 110 | { |
|
| 111 | return static::create($start, $end, IntervalType::halfOpen()); |
|
| 112 | } |
|
| 113 | ||
| 114 | /** |
|
| 115 | * @param string $start |
|
| 116 | * @param string $end |
|
| 117 | * |
|
| 118 | * @return self |
|
| 119 | */ |
|
| 120 | public static function open($start, $end) |
|
| 121 | { |
|
| 122 | return static::create($start, $end, IntervalType::open()); |
|
| 123 | } |
|
| 124 | ||
| 125 | /** |
|
| 126 | * Create interval from string. |
|
| 127 | * |
|
| 128 | * Example formats for all interval types: |
|
| 129 | * [10.0.1.0, 10.0.1.255] |
|
| 130 | * (10.0.0.0, 10.255.255.255] |
|
| 131 | * [172.16.0.0, 172.31.255.255) |
|
| 132 | * (192.168.0.0, 192.168.255.255) |
|
| 133 | * |
|
| 134 | * Spaces are ignored in format. |
|
| 135 | * |
|
| 136 | * @param string $string |
|
| 137 | * |
|
| 138 | * @return self |
|
| 139 | */ |
|
| 140 | public static function fromString($string) |
|
| 141 | { |
|
| 142 | if (!preg_match(self::REGEXP, $string, $match)) { |
|
| 143 | throw InvalidIntervalFormatException::create('[0.0.0.0, 255.255.255.255]', $string); |
|
| 144 | } |
|
| 145 | ||
| 146 | return self::create($match['start'], $match['end'], IntervalType::fromString($string)); |
|
| 147 | } |
|
| 148 | ||
| 149 | /** |
|
| 150 | * @param string $point |
|
| 151 | * |
|
| 152 | * @return bool |
|
| 153 | */ |
|
| 154 | public function contains($point) |
|
| 155 | { |
|
| 156 | return $this->comparator->contains(new IPv4IntervalPoint($point)); |
|
| 157 | } |
|
| 158 | ||
| 159 | /** |
|
| 160 | * @param IPv4Interval $interval |
|
| 161 | * @param bool $check_interval_type |
|
| 162 | * |
|
| 163 | * @return bool |
|
| 164 | */ |
|
| 165 | public function intersect(IPv4Interval $interval, $check_interval_type = true) |
|
| 166 | { |
|
| 167 | return $this->comparator->intersect($interval, $check_interval_type); |
|
| 168 | } |
|
| 169 | ||
| 170 | /** |
|
| 171 | * @param IPv4Interval $interval |
|
| 172 | * |
|
| 173 | * @return IPv4Interval|null |
|
| 174 | */ |
|
| 175 | public function intersectInterval(IPv4Interval $interval) |
|
| 176 | { |
|
| 177 | return $this->comparator->intersectInterval($interval); |
|
| 178 | } |
|
| 179 | ||
| 180 | /** |
|
| 181 | * The point is before the interval |
|
| 182 | * |
|
| 183 | * @param string $point |
|
| 184 | * |
|
| 185 | * @return bool |
|
| 186 | */ |
|
| 187 | public function before($point) |
|
| 188 | { |
|
| 189 | return $this->comparator->before(new IPv4IntervalPoint($point)); |
|
| 190 | } |
|
| 191 | ||
| 192 | /** |
|
| 193 | * The point is after the interval |
|
| 194 | * |
|
| 195 | * @param string $point |
|
| 196 | * |
|
| 197 | * @return bool |
|
| 198 | */ |
|
| 199 | public function after($point) |
|
| 200 | { |
|
| 201 | return $this->comparator->after(new IPv4IntervalPoint($point)); |
|
| 202 | } |
|
| 203 | ||
| 204 | /** |
|
| 205 | * @return IntervalType |
|
| 206 | */ |
|
| 207 | public function type() |
|
| 208 | { |
|
| 209 | return $this->type; |
|
| 210 | } |
|
| 211 | ||
| 212 | /** |
|
| 213 | * @return string |
|
| 214 | */ |
|
| 215 | public function start() |
|
| 216 | { |
|
| 217 | return (string)$this->start; |
|
| 218 | } |
|
| 219 | ||
| 220 | /** |
|
| 221 | * @return string |
|
| 222 | */ |
|
| 223 | public function end() |
|
| 224 | { |
|
| 225 | return (string)$this->end; |
|
| 226 | } |
|
| 227 | ||
| 228 | /** |
|
| 229 | * @return IPv4IntervalPoint |
|
| 230 | */ |
|
| 231 | public function startPoint() |
|
| 232 | { |
|
| 233 | return $this->start; |
|
| 234 | } |
|
| 235 | ||
| 236 | /** |
|
| 237 | * @return IPv4IntervalPoint |
|
| 238 | */ |
|
| 239 | public function endPoint() |
|
| 240 | { |
|
| 241 | return $this->end; |
|
| 242 | } |
|
| 243 | ||
| 244 | /** |
|
| 245 | * @return string |
|
| 246 | */ |
|
| 247 | public function __toString() |
|
| 248 | { |
|
| 249 | return $this->type->getReadable($this); |
|
| 250 | } |
|
| 251 | } |
|
| 252 | ||
| @@ 17-251 (lines=235) @@ | ||
| 14 | use GpsLab\Component\Interval\IntervalInterface; |
|
| 15 | use GpsLab\Component\Interval\IntervalType; |
|
| 16 | ||
| 17 | class NumberInterval implements IntervalInterface |
|
| 18 | { |
|
| 19 | /** |
|
| 20 | * @var string |
|
| 21 | */ |
|
| 22 | const REGEXP = '/^ |
|
| 23 | (?:\(|\[) # start type char |
|
| 24 | \s* |
|
| 25 | (?<start>\-?\d+) # start point |
|
| 26 | \s*,\s* # separator |
|
| 27 | (?<end>\-?\d+) # end point |
|
| 28 | \s* |
|
| 29 | (?:\)|\]) # end type char |
|
| 30 | $/x'; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @var IntervalType |
|
| 34 | */ |
|
| 35 | private $type; |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @var NumberIntervalComparator |
|
| 39 | */ |
|
| 40 | private $comparator; |
|
| 41 | ||
| 42 | /** |
|
| 43 | * @var NumberIntervalPoint |
|
| 44 | */ |
|
| 45 | private $start; |
|
| 46 | ||
| 47 | /** |
|
| 48 | * @var NumberIntervalPoint |
|
| 49 | */ |
|
| 50 | private $end; |
|
| 51 | ||
| 52 | /** |
|
| 53 | * @param NumberIntervalPoint $start |
|
| 54 | * @param NumberIntervalPoint $end |
|
| 55 | * @param IntervalType $type |
|
| 56 | */ |
|
| 57 | private function __construct(NumberIntervalPoint $start, NumberIntervalPoint $end, IntervalType $type) |
|
| 58 | { |
|
| 59 | if ($start->gte($end)) { |
|
| 60 | throw IncorrectIntervalException::create(); |
|
| 61 | } |
|
| 62 | ||
| 63 | $this->start = $start; |
|
| 64 | $this->end = $end; |
|
| 65 | $this->type = $type; |
|
| 66 | $this->comparator = new NumberIntervalComparator($this); |
|
| 67 | } |
|
| 68 | ||
| 69 | /** |
|
| 70 | * @param int|float $start |
|
| 71 | * @param int|float $end |
|
| 72 | * @param IntervalType $type |
|
| 73 | * |
|
| 74 | * @return self |
|
| 75 | */ |
|
| 76 | public static function create($start, $end, IntervalType $type) |
|
| 77 | { |
|
| 78 | return new self(new NumberIntervalPoint($start), new NumberIntervalPoint($end), $type); |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * @param int|float $start |
|
| 83 | * @param int|float $end |
|
| 84 | * |
|
| 85 | * @return self |
|
| 86 | */ |
|
| 87 | public static function closed($start, $end) |
|
| 88 | { |
|
| 89 | return self::create($start, $end, IntervalType::closed()); |
|
| 90 | } |
|
| 91 | ||
| 92 | /** |
|
| 93 | * @param int|float $start |
|
| 94 | * @param int|float $end |
|
| 95 | * |
|
| 96 | * @return self |
|
| 97 | */ |
|
| 98 | public static function halfClosed($start, $end) |
|
| 99 | { |
|
| 100 | return self::create($start, $end, IntervalType::halfClosed()); |
|
| 101 | } |
|
| 102 | ||
| 103 | /** |
|
| 104 | * @param int|float $start |
|
| 105 | * @param int|float $end |
|
| 106 | * |
|
| 107 | * @return self |
|
| 108 | */ |
|
| 109 | public static function halfOpen($start, $end) |
|
| 110 | { |
|
| 111 | return self::create($start, $end, IntervalType::halfOpen()); |
|
| 112 | } |
|
| 113 | ||
| 114 | /** |
|
| 115 | * @param int|float $start |
|
| 116 | * @param int|float $end |
|
| 117 | * |
|
| 118 | * @return self |
|
| 119 | */ |
|
| 120 | public static function open($start, $end) |
|
| 121 | { |
|
| 122 | return self::create($start, $end, IntervalType::open()); |
|
| 123 | } |
|
| 124 | ||
| 125 | /** |
|
| 126 | * Create interval from string. |
|
| 127 | * |
|
| 128 | * Example formats: |
|
| 129 | * [0, 5] |
|
| 130 | * (-3, 2] |
|
| 131 | * [-3, -1) |
|
| 132 | * (3, 9) |
|
| 133 | * |
|
| 134 | * Spaces are ignored in format. |
|
| 135 | * |
|
| 136 | * @param string $string |
|
| 137 | * |
|
| 138 | * @return self |
|
| 139 | */ |
|
| 140 | public static function fromString($string) |
|
| 141 | { |
|
| 142 | if (!preg_match(self::REGEXP, $string, $match)) { |
|
| 143 | throw InvalidIntervalFormatException::create('[N, N]', $string); |
|
| 144 | } |
|
| 145 | ||
| 146 | return self::create($match['start'], $match['end'], IntervalType::fromString($string)); |
|
| 147 | } |
|
| 148 | ||
| 149 | /** |
|
| 150 | * @param int|float $point |
|
| 151 | * |
|
| 152 | * @return bool |
|
| 153 | */ |
|
| 154 | public function contains($point) |
|
| 155 | { |
|
| 156 | return $this->comparator->contains(new NumberIntervalPoint($point)); |
|
| 157 | } |
|
| 158 | ||
| 159 | /** |
|
| 160 | * @param NumberInterval $interval |
|
| 161 | * @param bool $check_interval_type |
|
| 162 | * |
|
| 163 | * @return bool |
|
| 164 | */ |
|
| 165 | public function intersect(NumberInterval $interval, $check_interval_type = true) |
|
| 166 | { |
|
| 167 | return $this->comparator->intersect($interval, $check_interval_type); |
|
| 168 | } |
|
| 169 | ||
| 170 | /** |
|
| 171 | * @param NumberInterval $interval |
|
| 172 | * |
|
| 173 | * @return NumberInterval|null |
|
| 174 | */ |
|
| 175 | public function intersectInterval(NumberInterval $interval) |
|
| 176 | { |
|
| 177 | return $this->comparator->intersectInterval($interval); |
|
| 178 | } |
|
| 179 | ||
| 180 | /** |
|
| 181 | * The point is before the interval |
|
| 182 | * |
|
| 183 | * @param int|float $point |
|
| 184 | * |
|
| 185 | * @return bool |
|
| 186 | */ |
|
| 187 | public function before($point) |
|
| 188 | { |
|
| 189 | return $this->comparator->before(new NumberIntervalPoint($point)); |
|
| 190 | } |
|
| 191 | ||
| 192 | /** |
|
| 193 | * The point is after the interval |
|
| 194 | * |
|
| 195 | * @param int|float $point |
|
| 196 | * |
|
| 197 | * @return bool |
|
| 198 | */ |
|
| 199 | public function after($point) |
|
| 200 | { |
|
| 201 | return $this->comparator->after(new NumberIntervalPoint($point)); |
|
| 202 | } |
|
| 203 | ||
| 204 | /** |
|
| 205 | * @return IntervalType |
|
| 206 | */ |
|
| 207 | public function type() |
|
| 208 | { |
|
| 209 | return $this->type; |
|
| 210 | } |
|
| 211 | ||
| 212 | /** |
|
| 213 | * @return int|float |
|
| 214 | */ |
|
| 215 | public function start() |
|
| 216 | { |
|
| 217 | return $this->start->value(); |
|
| 218 | } |
|
| 219 | ||
| 220 | /** |
|
| 221 | * @return int|float |
|
| 222 | */ |
|
| 223 | public function end() |
|
| 224 | { |
|
| 225 | return $this->end->value(); |
|
| 226 | } |
|
| 227 | ||
| 228 | /** |
|
| 229 | * @return NumberIntervalPoint |
|
| 230 | */ |
|
| 231 | public function startPoint() |
|
| 232 | { |
|
| 233 | return $this->start; |
|
| 234 | } |
|
| 235 | ||
| 236 | /** |
|
| 237 | * @return NumberIntervalPoint |
|
| 238 | */ |
|
| 239 | public function endPoint() |
|
| 240 | { |
|
| 241 | return $this->end; |
|
| 242 | } |
|
| 243 | ||
| 244 | /** |
|
| 245 | * @return string |
|
| 246 | */ |
|
| 247 | public function __toString() |
|
| 248 | { |
|
| 249 | return $this->type->getReadable($this); |
|
| 250 | } |
|
| 251 | } |
|
| 252 | ||