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