1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acamposm\IPv4AddressConverter\Converters; |
4
|
|
|
|
5
|
|
|
use Acamposm\IPv4AddressConverter\Enums\IPAddressFormatEnum as IPAddressFormat; |
6
|
|
|
use Acamposm\IPv4AddressConverter\Interfaces\IPv4AddressConverterInterface; |
7
|
|
|
|
8
|
|
|
abstract class BaseAddressConverter implements IPv4AddressConverterInterface |
9
|
|
|
{ |
10
|
|
|
protected int | string $address; |
11
|
|
|
protected int $inputFormat; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* BaseAddressConverter constructor. |
15
|
|
|
* |
16
|
|
|
* @param false $withDotNotation |
17
|
|
|
*/ |
18
|
|
|
public function __construct( |
19
|
|
|
protected $withDotNotation = false, |
20
|
|
|
) { |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Set the value of $address from Binary string. |
25
|
|
|
* |
26
|
|
|
* @param string $address |
27
|
|
|
* |
28
|
|
|
* @return BaseAddressConverter |
29
|
|
|
*/ |
30
|
|
|
public function fromBinary(string $address): BaseAddressConverter |
31
|
|
|
{ |
32
|
|
|
$this->address = $address; |
33
|
|
|
$this->inputFormat = IPAddressFormat::BINARY; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set the value of $address from Decimal string. |
40
|
|
|
* |
41
|
|
|
* @param string $address |
42
|
|
|
* |
43
|
|
|
* @return BaseAddressConverter |
44
|
|
|
*/ |
45
|
|
|
public function fromDecimal(string $address): BaseAddressConverter |
46
|
|
|
{ |
47
|
|
|
$this->address = $address; |
48
|
|
|
$this->inputFormat = IPAddressFormat::DECIMAL; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set the value of $address from Hexadecimal string. |
55
|
|
|
* |
56
|
|
|
* @param string $address |
57
|
|
|
* |
58
|
|
|
* @return BaseAddressConverter |
59
|
|
|
*/ |
60
|
|
|
public function fromHexadecimal(string $address): BaseAddressConverter |
61
|
|
|
{ |
62
|
|
|
$this->address = $address; |
63
|
|
|
$this->inputFormat = IPAddressFormat::HEXADECIMAL; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the value of $address from Long Integer. |
70
|
|
|
* |
71
|
|
|
* @param int $address |
72
|
|
|
* |
73
|
|
|
* @return BaseAddressConverter |
74
|
|
|
*/ |
75
|
|
|
public function fromLong(int $address): BaseAddressConverter |
76
|
|
|
{ |
77
|
|
|
$this->address = $address; |
78
|
|
|
$this->inputFormat = IPAddressFormat::LONG; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the output format as dot notation. |
85
|
|
|
* |
86
|
|
|
* @return BaseAddressConverter |
87
|
|
|
*/ |
88
|
|
|
public function withDotNotation(): BaseAddressConverter |
89
|
|
|
{ |
90
|
|
|
$this->withDotNotation = true; |
|
|
|
|
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
|
|
public function convert(): int | string |
99
|
|
|
{ |
100
|
|
|
} |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.