|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acamposm\IPv4AddressConverter; |
|
4
|
|
|
|
|
5
|
|
|
use Acamposm\IPv4AddressConverter\Converters\BinaryIPv4AddressConverter; |
|
6
|
|
|
use Acamposm\IPv4AddressConverter\Converters\DecimalIPv4AddressConverter; |
|
7
|
|
|
use Acamposm\IPv4AddressConverter\Converters\HexadecimalIPv4AddressConverter; |
|
8
|
|
|
use Acamposm\IPv4AddressConverter\Converters\LongIPv4AddressConverter; |
|
9
|
|
|
use Acamposm\IPv4AddressConverter\Enums\IPAddressFormatEnum as IPAddressFormat; |
|
10
|
|
|
use Acamposm\IPv4AddressConverter\Exceptions\OutputFormatException; |
|
11
|
|
|
use Acamposm\IPv4AddressConverter\Interfaces\IPv4AddressConverterInterface; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
|
|
14
|
|
|
class IPv4AddressConverter |
|
15
|
|
|
{ |
|
16
|
|
|
private int | string $address; |
|
17
|
|
|
private IPv4AddressConverterInterface $converter; |
|
18
|
|
|
private int $inputFormat; |
|
19
|
|
|
private bool $withDotNotation; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* IPv4AddressConverter constructor. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->withDotNotation = false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return IPv4AddressConverter |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function convert(): IPv4AddressConverter |
|
33
|
|
|
{ |
|
34
|
|
|
return new static(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Set the value of $address from a binary string IP Address. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $address |
|
41
|
|
|
* |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function fromBinary(string $address): IPv4AddressConverter |
|
45
|
|
|
{ |
|
46
|
|
|
$this->address = $address; |
|
47
|
|
|
$this->inputFormat = IPAddressFormat::BINARY; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set the value of $address from a dotted decimal string IP Address. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $address |
|
56
|
|
|
* |
|
57
|
|
|
* @return $this |
|
58
|
|
|
*/ |
|
59
|
|
|
public function fromDecimal(string $address): IPv4AddressConverter |
|
60
|
|
|
{ |
|
61
|
|
|
$this->address = $address; |
|
62
|
|
|
$this->inputFormat = IPAddressFormat::DECIMAL; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set the value of $address from a hexadecimal string IP Address. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $address |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
public function fromHexadecimal(string $address): IPv4AddressConverter |
|
75
|
|
|
{ |
|
76
|
|
|
$this->address = $address; |
|
77
|
|
|
$this->inputFormat = IPAddressFormat::HEXADECIMAL; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set the value of $address from a long integer IP Address. |
|
84
|
|
|
* |
|
85
|
|
|
* @param int $address |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function fromLong(int $address): IPv4AddressConverter |
|
90
|
|
|
{ |
|
91
|
|
|
$this->address = $address; |
|
92
|
|
|
$this->inputFormat = IPAddressFormat::LONG; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the output format as Binary string. |
|
99
|
|
|
* |
|
100
|
|
|
* @return IPv4AddressConverter |
|
101
|
|
|
*/ |
|
102
|
|
|
public function toBinary(): IPv4AddressConverter |
|
103
|
|
|
{ |
|
104
|
|
|
$this->converter = $this->getBinaryConverter(); |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set the output format as Decimal string. |
|
111
|
|
|
* |
|
112
|
|
|
* @return IPv4AddressConverter |
|
113
|
|
|
*/ |
|
114
|
|
|
public function toDecimal(): IPv4AddressConverter |
|
115
|
|
|
{ |
|
116
|
|
|
$this->converter = $this->getDecimalConverter(); |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set the output format as Hexadecimal string. |
|
123
|
|
|
* |
|
124
|
|
|
* @return IPv4AddressConverter |
|
125
|
|
|
*/ |
|
126
|
|
|
public function toHexadecimal(): IPv4AddressConverter |
|
127
|
|
|
{ |
|
128
|
|
|
$this->converter = $this->getHexadecimalConverter(); |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Set the output format as Long Integer. |
|
135
|
|
|
* |
|
136
|
|
|
* @return IPv4AddressConverter |
|
137
|
|
|
*/ |
|
138
|
|
|
public function toLong(): IPv4AddressConverter |
|
139
|
|
|
{ |
|
140
|
|
|
$this->converter = $this->getLongConverter(); |
|
141
|
|
|
|
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Apply Dot notation to the output format. |
|
147
|
|
|
* |
|
148
|
|
|
* @return IPv4AddressConverter |
|
149
|
|
|
*/ |
|
150
|
|
|
public function withDotNotation(): IPv4AddressConverter |
|
151
|
|
|
{ |
|
152
|
|
|
$this->withDotNotation = true; |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns the converted IP Address from the original value to specified format. |
|
159
|
|
|
* |
|
160
|
|
|
* @throws OutputFormatException |
|
161
|
|
|
* |
|
162
|
|
|
* @return int|string |
|
163
|
|
|
*/ |
|
164
|
|
|
public function get(): int | string |
|
165
|
|
|
{ |
|
166
|
|
|
if (!isset($this->converter)) { |
|
167
|
|
|
throw new OutputFormatException(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return match ($this->inputFormat) { |
|
171
|
|
|
IPAddressFormat::BINARY => $this->getFromBinary(), |
|
172
|
|
|
|
|
173
|
|
|
IPAddressFormat::DECIMAL => $this->getFromDecimal(), |
|
174
|
|
|
|
|
175
|
|
|
IPAddressFormat::HEXADECIMAL => $this->getFromHexadecimal(), |
|
176
|
|
|
|
|
177
|
|
|
IPAddressFormat::LONG => $this->getFromLong(), |
|
178
|
|
|
|
|
179
|
|
|
default => $this->address, |
|
180
|
|
|
}; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return int|string |
|
185
|
|
|
*/ |
|
186
|
|
|
private function getFromBinary(): int | string |
|
187
|
|
|
{ |
|
188
|
|
|
$converter = $this->converter->fromBinary($this->address); |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
return $this->withDotNotation |
|
191
|
|
|
? $converter->withDotNotation()->convert() |
|
192
|
|
|
: $converter->convert(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return int|string |
|
197
|
|
|
*/ |
|
198
|
|
|
private function getFromDecimal(): int | string |
|
199
|
|
|
{ |
|
200
|
|
|
$converter = $this->converter->fromDecimal($this->address); |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
return $this->withDotNotation |
|
203
|
|
|
? $converter->withDotNotation()->convert() |
|
204
|
|
|
: $converter->convert(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return int|string |
|
209
|
|
|
*/ |
|
210
|
|
|
private function getFromHexadecimal(): int | string |
|
211
|
|
|
{ |
|
212
|
|
|
$converter = $this->converter->fromHexadecimal($this->address); |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
return $this->withDotNotation |
|
215
|
|
|
? $converter->withDotNotation()->convert() |
|
216
|
|
|
: $converter->convert(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return int|string |
|
221
|
|
|
*/ |
|
222
|
|
|
private function getFromLong(): int | string |
|
223
|
|
|
{ |
|
224
|
|
|
$converter = $this->converter->fromLong($this->address); |
|
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
return $this->withDotNotation |
|
227
|
|
|
? $converter->withDotNotation()->convert() |
|
228
|
|
|
: $converter->convert(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Returns an object with all address conversions. |
|
233
|
|
|
* |
|
234
|
|
|
* @return object |
|
235
|
|
|
*/ |
|
236
|
|
|
public function all(): object |
|
237
|
|
|
{ |
|
238
|
|
|
return match ($this->inputFormat) { |
|
239
|
|
|
IPAddressFormat::BINARY => $this->getOutputFromBinaryAddress(), |
|
240
|
|
|
|
|
241
|
|
|
IPAddressFormat::DECIMAL => $this->getOutputFromDecimalAddress(), |
|
242
|
|
|
|
|
243
|
|
|
IPAddressFormat::HEXADECIMAL => $this->getOutputFromHexadecimalAddress(), |
|
244
|
|
|
|
|
245
|
|
|
IPAddressFormat::LONG => $this->getOutputFromLongAddress(), |
|
246
|
|
|
|
|
247
|
|
|
default => $this->address, |
|
248
|
|
|
}; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Returns a BinaryIPv4AddressConverter instance. |
|
253
|
|
|
* |
|
254
|
|
|
* @return IPv4AddressConverterInterface |
|
255
|
|
|
*/ |
|
256
|
|
|
private function getBinaryConverter(): IPv4AddressConverterInterface |
|
257
|
|
|
{ |
|
258
|
|
|
return new BinaryIPv4AddressConverter(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Returns a DecimalIPv4AddressConverter instance. |
|
263
|
|
|
* |
|
264
|
|
|
* @return IPv4AddressConverterInterface |
|
265
|
|
|
*/ |
|
266
|
|
|
private function getDecimalConverter(): IPv4AddressConverterInterface |
|
267
|
|
|
{ |
|
268
|
|
|
return new DecimalIPv4AddressConverter(); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Returns a HexadecimalIPv4AddressConverter instance. |
|
273
|
|
|
* |
|
274
|
|
|
* @return IPv4AddressConverterInterface |
|
275
|
|
|
*/ |
|
276
|
|
|
private function getHexadecimalConverter(): IPv4AddressConverterInterface |
|
277
|
|
|
{ |
|
278
|
|
|
return new HexadecimalIPv4AddressConverter(); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Returns a LongIPv4AddressConverter instance. |
|
283
|
|
|
* |
|
284
|
|
|
* @return IPv4AddressConverterInterface |
|
285
|
|
|
*/ |
|
286
|
|
|
private function getLongConverter(): IPv4AddressConverterInterface |
|
287
|
|
|
{ |
|
288
|
|
|
return new LongIPv4AddressConverter(); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Returns an object with all converter instances. |
|
293
|
|
|
* |
|
294
|
|
|
* @return object |
|
295
|
|
|
*/ |
|
296
|
|
|
private function getAddressConverters(): stdClass |
|
297
|
|
|
{ |
|
298
|
|
|
return (object) [ |
|
299
|
|
|
'binary' => $this->withDotNotation |
|
300
|
|
|
? $this->getBinaryConverter()->withDotNotation() |
|
301
|
|
|
: $this->getHexadecimalConverter(), |
|
302
|
|
|
'decimal' => $this->getDecimalConverter(), |
|
303
|
|
|
'hexadecimal' => $this->withDotNotation |
|
304
|
|
|
? $this->getHexadecimalConverter()->withDotNotation() |
|
305
|
|
|
: $this->getHexadecimalConverter(), |
|
306
|
|
|
'long' => $this->getLongConverter(), |
|
307
|
|
|
]; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Returns an object with all address conversions from binary address. |
|
312
|
|
|
* |
|
313
|
|
|
* @return stdClass |
|
314
|
|
|
*/ |
|
315
|
|
|
private function getOutputFromBinaryAddress(): stdClass |
|
316
|
|
|
{ |
|
317
|
|
|
$address = $this->address; |
|
318
|
|
|
$converters = $this->getAddressConverters(); |
|
319
|
|
|
|
|
320
|
|
|
return (object) [ |
|
321
|
|
|
'binary' => $address, |
|
322
|
|
|
'decimal' => $converters->decimal->fromBinary($address)->convert(), |
|
323
|
|
|
'hexadecimal' => $converters->hexadecimal->fromBinary($address)->convert(), |
|
324
|
|
|
'long' => $converters->long->fromBinary($address)->convert(), |
|
325
|
|
|
]; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Returns an object with all address conversions from decimal address. |
|
330
|
|
|
* |
|
331
|
|
|
* @return stdClass |
|
332
|
|
|
*/ |
|
333
|
|
|
private function getOutputFromDecimalAddress(): stdClass |
|
334
|
|
|
{ |
|
335
|
|
|
$address = $this->address; |
|
336
|
|
|
$converters = $this->getAddressConverters(); |
|
337
|
|
|
|
|
338
|
|
|
return (object) [ |
|
339
|
|
|
'binary' => $converters->binary->fromDecimal($address)->convert(), |
|
340
|
|
|
'decimal' => $address, |
|
341
|
|
|
'hexadecimal' => $converters->hexadecimal->fromDecimal($address)->convert(), |
|
342
|
|
|
'long' => $converters->long->fromDecimal($address)->convert(), |
|
343
|
|
|
]; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Returns an object with all address conversions from hexadecimal address. |
|
348
|
|
|
* |
|
349
|
|
|
* @return object |
|
350
|
|
|
*/ |
|
351
|
|
|
private function getOutputFromHexadecimalAddress(): stdClass |
|
352
|
|
|
{ |
|
353
|
|
|
$address = $this->address; |
|
354
|
|
|
$converters = $this->getAddressConverters(); |
|
355
|
|
|
|
|
356
|
|
|
return (object) [ |
|
357
|
|
|
'binary' => $converters->binary->fromHexadecimal($address)->convert(), |
|
358
|
|
|
'decimal' => $converters->decimal->fromHexadecimal($address)->convert(), |
|
359
|
|
|
'hexadecimal' => $address, |
|
360
|
|
|
'long' => $converters->long->fromHexadecimal($address)->convert(), |
|
361
|
|
|
]; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Returns an object with all address conversions from long address. |
|
366
|
|
|
* |
|
367
|
|
|
* @return object |
|
368
|
|
|
*/ |
|
369
|
|
|
private function getOutputFromLongAddress(): stdClass |
|
370
|
|
|
{ |
|
371
|
|
|
$address = $this->address; |
|
372
|
|
|
$converters = $this->getAddressConverters(); |
|
373
|
|
|
|
|
374
|
|
|
return (object) [ |
|
375
|
|
|
'binary' => $converters->binary->fromLong($address)->convert(), |
|
376
|
|
|
'decimal' => $converters->decimal->fromLong($address)->convert(), |
|
377
|
|
|
'hexadecimal' => $converters->hexadecimal->fromLong($address)->convert(), |
|
378
|
|
|
'long' => $address, |
|
379
|
|
|
]; |
|
380
|
|
|
} |
|
381
|
|
|
} |
|
382
|
|
|
|