-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
IPv4Normalizer.php
195 lines (177 loc) · 5.47 KB
/
IPv4Normalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Uri;
use League\Uri\Components\Host;
use League\Uri\Contracts\AuthorityInterface;
use League\Uri\Contracts\HostInterface;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\MissingFeature;
use League\Uri\IPv4\Calculator;
use League\Uri\IPv4\Converter;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
/**
* DEPRECATION WARNING! This class will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Converter
*
* @codeCoverageIgnore
*/
final class IPv4Normalizer
{
private readonly Converter $converter;
public function __construct(
Converter|Calculator $calculator
) {
if (!$calculator instanceof Converter) {
$calculator = new Converter($calculator);
}
$this->converter = $calculator;
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Converter::toDecimal()
*
* @codeCoverageIgnore
*
* Normalizes the host content to a IPv4 dot-decimal notation if possible
* otherwise returns the Host instance unchanged.
*
* @see https://url.spec.whatwg.org/#concept-ipv4-parser
*/
public function normalize(Stringable|string|null $host): ?string
{
return $this->converter->toDecimal($host);
}
/**
* Returns an instance using a GMP calculator.
*/
public static function createFromGMP(): self
{
return new self(Converter::fromGMP());
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Converter::fromBCMath()
*
* @codeCoverageIgnore
*
* Returns an instance using a Bcmath calculator.
*/
public static function createFromBCMath(): self
{
return new self(Converter::fromBCMath());
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Converter::fromNative()
*
* @codeCoverageIgnore
*
* Returns an instance using a PHP native calculator (requires 64bits PHP).
*/
public static function createFromNative(): self
{
return new self(Converter::fromNative());
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @throws MissingFeature If no IPv4Calculator implementing object can be used on the platform
*
* @codeCoverageIgnore
*@see Converter::fromEnvironment()
*
* @codeCoverageIgnore
*
* Returns an instance using a detected calculator depending on the PHP environment.
*
* @deprecated Since version 7.0.0
*/
public static function createFromServer(): self
{
return new self(Converter::fromEnvironment());
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Modifier::hostToDecimal()
*
* @codeCoverageIgnore
*
* Normalizes the URI host content to a IPv4 dot-decimal notation if possible
* otherwise returns the uri instance unchanged.
*
* @see https://url.spec.whatwg.org/#concept-ipv4-parser
*/
public function normalizeUri(UriInterface|Psr7UriInterface $uri): UriInterface|Psr7UriInterface
{
$host = $uri->getHost();
$decimalIPv4 = $this->converter->toDecimal($host);
return match (true) {
null === $decimalIPv4,
$decimalIPv4 === $host => $uri,
default => $uri->withHost($decimalIPv4),
};
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Modifier::hostToDecimal()
*
* @codeCoverageIgnore
*
* Normalizes the authority host content to a IPv4 dot-decimal notation if possible
* otherwise returns the uri instance unchanged.
*
* @see https://url.spec.whatwg.org/#concept-ipv4-parser
*/
public function normalizeAuthority(AuthorityInterface $authority): AuthorityInterface
{
$host = $authority->getHost();
$decimalIpv4 = $this->converter->toDecimal($host);
return match (true) {
null === $decimalIpv4,
$decimalIpv4 === $host => $authority,
default => $authority->withHost($decimalIpv4),
};
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see Modifier::hostToDecimal()
*
* @codeCoverageIgnore
*
* Normalizes the host content to a IPv4 dot-decimal notation if possible
* otherwise returns the Host instance unchanged.
*
* @see https://url.spec.whatwg.org/#concept-ipv4-parser
*/
public function normalizeHost(HostInterface $host): HostInterface
{
$decimalIPv4 = $this->converter->toDecimal($host->value());
return match (null) {
$decimalIPv4 => $host,
default => Host::new($decimalIPv4),
};
}
}