|
21 | 21 | */ |
22 | 22 |
|
23 | 23 | use DCarbone\PHPConsulAPI\AbstractModel; |
24 | | -use DCarbone\PHPConsulAPI\Transcoding; |
25 | 24 |
|
26 | 25 | class ExposePath extends AbstractModel |
27 | 26 | { |
28 | | - protected const FIELDS = [ |
29 | | - self::FIELD_LISTENER_PORT => Transcoding::OMITEMPTY_INTEGER_FIELD, |
30 | | - self::FIELD_PATH => Transcoding::OMITEMPTY_STRING_FIELD, |
31 | | - self::FIELD_LOCAL_PORT_PATH => Transcoding::OMITEMPTY_INTEGER_FIELD, |
32 | | - self::FIELD_PROTOCOL => Transcoding::OMITEMPTY_STRING_FIELD, |
33 | | - ]; |
34 | | - |
35 | | - private const FIELD_LISTENER_PORT = 'ListenerPort'; |
36 | | - private const FIELD_PATH = 'Path'; |
37 | | - private const FIELD_LOCAL_PORT_PATH = 'LocalPortPath'; |
38 | | - private const FIELD_PROTOCOL = 'Protocol'; |
39 | | - |
40 | 27 | public int $ListenerPort; |
41 | 28 | public string $Path; |
42 | 29 | public int $LocalPathPort; |
43 | 30 | public string $Protocol; |
44 | 31 | public bool $ParsedFromCheck; |
45 | 32 |
|
| 33 | + /** |
| 34 | + * @param array<string,mixed>|null $data |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + null|array $data = null, // Deprecated, will be removed. |
| 38 | + int $ListenerPort = 0, |
| 39 | + string $Path = '', |
| 40 | + int $LocalPathPort = 0, |
| 41 | + string $Protocol = '', |
| 42 | + bool $ParsedFromCheck = false |
| 43 | + ) { |
| 44 | + $this->ListenerPort = $ListenerPort; |
| 45 | + $this->Path = $Path; |
| 46 | + $this->LocalPathPort = $LocalPathPort; |
| 47 | + $this->Protocol = $Protocol; |
| 48 | + $this->ParsedFromCheck = $ParsedFromCheck; |
| 49 | + if (null !== $data && [] !== $data) { |
| 50 | + self::jsonUnserialize((object)$data, $this); |
| 51 | + } |
| 52 | + } |
| 53 | + |
46 | 54 | public function getListenerPort(): int |
47 | 55 | { |
48 | 56 | return $this->ListenerPort; |
@@ -97,4 +105,43 @@ public function setParsedFromCheck(bool $ParsedFromCheck): self |
97 | 105 | $this->ParsedFromCheck = $ParsedFromCheck; |
98 | 106 | return $this; |
99 | 107 | } |
| 108 | + |
| 109 | + public static function jsonUnserialize(\stdClass $decoded, null|self $n = null): static |
| 110 | + { |
| 111 | + $n = $n ?? new self(); |
| 112 | + foreach ($decoded as $k => $v) { |
| 113 | + if ('listener_port' === $k) { |
| 114 | + $n->ListenerPort = $v; |
| 115 | + } elseif ('local_path_port' === $k) { |
| 116 | + $n->LocalPathPort = $v; |
| 117 | + } else { |
| 118 | + $n->{$k} = $v; |
| 119 | + } |
| 120 | + } |
| 121 | + return $n; |
| 122 | + } |
| 123 | + |
| 124 | + public function jsonSerialize(): \stdClass |
| 125 | + { |
| 126 | + $out = new \stdClass(); |
| 127 | + foreach ($this->_getDynamicFields() as $k => $v) { |
| 128 | + $out->{$k} = $v; |
| 129 | + } |
| 130 | + if (0 !== $this->ListenerPort) { |
| 131 | + $out->listener_port = $this->ListenerPort; |
| 132 | + } |
| 133 | + if ('' !== $this->Path) { |
| 134 | + $out->Path = $this->Path; |
| 135 | + } |
| 136 | + if (0 !== $this->LocalPathPort) { |
| 137 | + $out->local_path_port = $this->LocalPathPort; |
| 138 | + } |
| 139 | + if ('' !== $this->Protocol) { |
| 140 | + $out->Protocol = $this->Protocol; |
| 141 | + } |
| 142 | + if ($this->ParsedFromCheck) { |
| 143 | + $out->ParsedFromCheck = true; |
| 144 | + } |
| 145 | + return $out; |
| 146 | + } |
100 | 147 | } |
0 commit comments