Skip to content

Commit f3ae30c

Browse files
Fix LoadBalancer hydration and serialization (#360)
1 parent 2e5b734 commit f3ae30c

4 files changed

Lines changed: 534 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGE LOG
77
* Add PHP 8.5 support
88
* Fixed the `Droplet::create` SSH keys parameter type documentation
99
* Fixed hydration of object-backed App Platform and project resource fields
10+
* Fixed `LoadBalancer` hydration and update serialization
1011

1112

1213
## 5.0.5 (03/05/2025)

src/Entity/LoadBalancer.php

Lines changed: 123 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ final class LoadBalancer extends AbstractEntity
2525

2626
public string $ip;
2727

28+
public string $ipv6;
29+
30+
public string $projectId;
31+
32+
public int $sizeUnit;
33+
34+
public string $size;
35+
2836
public string $algorithm;
2937

3038
public string $status;
@@ -48,44 +56,146 @@ final class LoadBalancer extends AbstractEntity
4856

4957
public bool $redirectHttpToHttps;
5058

59+
public bool $enableProxyProtocol;
60+
61+
public bool $enableBackendKeepalive;
62+
5163
/**
5264
* @var int<30, 600>
5365
*/
5466
public int $httpIdleTimeoutSeconds;
5567

68+
public string $vpcUuid;
69+
70+
public bool $disableLetsEncryptDnsRecords;
71+
72+
public array $firewall;
73+
74+
public string $network;
75+
76+
public string $networkStack;
77+
78+
public string $type;
79+
80+
public array $domains;
81+
82+
public array $glbSettings;
83+
84+
public array $targetLoadBalancerIds;
85+
86+
public string $tlsCipherPolicy;
87+
5688
public function build(array $parameters): void
5789
{
5890
foreach ($parameters as $property => $value) {
5991
$property = static::convertToCamelCase($property);
6092

6193
if ('forwardingRules' === $property) {
6294
$this->forwardingRules = \array_map(fn ($v) => new ForwardingRule($v), $value);
95+
continue;
6396
} elseif ('healthCheck' === $property) {
6497
$this->healthCheck = new HealthCheck($value);
98+
continue;
6599
} elseif ('stickySessions' === $property) {
66100
$this->stickySessions = new StickySession($value);
101+
continue;
67102
} elseif ('region' === $property) {
68103
$this->region = new Region($value);
69-
} elseif (\property_exists($this, $property)) {
104+
continue;
105+
} elseif (
106+
\in_array($property, ['firewall', 'domains', 'glbSettings', 'targetLoadBalancerIds'], true) &&
107+
($value instanceof \stdClass || \is_array($value))
108+
) {
109+
$value = self::normalizeArray($value);
110+
}
111+
112+
if (\property_exists($this, $property)) {
70113
$this->$property = $value;
71114
}
72115
}
73116
}
74117

75118
public function toArray(): array
76119
{
77-
return [
78-
'name' => $this->name,
79-
'region' => $this->region->slug,
80-
'algorithm' => $this->algorithm,
81-
'forwarding_rules' => \array_map(function ($rule): array {
120+
$payload = parent::toArray();
121+
122+
unset($payload['id'], $payload['ip'], $payload['ipv6'], $payload['status'], $payload['created_at']);
123+
124+
if (isset($payload['region']) && $payload['region'] instanceof Region) {
125+
$payload['region'] = $payload['region']->slug;
126+
}
127+
128+
if (isset($payload['forwarding_rules']) && \is_array($payload['forwarding_rules'])) {
129+
/** @var ForwardingRule[] $forwardingRules */
130+
$forwardingRules = $payload['forwarding_rules'];
131+
$payload['forwarding_rules'] = \array_map(function (ForwardingRule $rule): array {
82132
return $rule->toArray();
83-
}, $this->forwardingRules),
84-
'health_check' => $this->healthCheck->toArray(),
85-
'sticky_sessions' => $this->stickySessions->toArray(),
86-
'droplet_ids' => $this->dropletIds,
87-
'redirect_http_to_https' => $this->redirectHttpToHttps,
88-
'http_idle_timeout_seconds' => $this->httpIdleTimeoutSeconds,
89-
];
133+
}, $forwardingRules);
134+
}
135+
136+
if (isset($payload['health_check']) && $payload['health_check'] instanceof HealthCheck) {
137+
$payload['health_check'] = $payload['health_check']->toArray();
138+
}
139+
140+
if (isset($payload['sticky_sessions']) && $payload['sticky_sessions'] instanceof StickySession) {
141+
$payload['sticky_sessions'] = $payload['sticky_sessions']->toArray();
142+
}
143+
144+
if (isset($payload['tag']) && '' !== $payload['tag']) {
145+
unset($payload['droplet_ids']);
146+
} else {
147+
unset($payload['tag']);
148+
}
149+
150+
$data = [];
151+
152+
foreach ([
153+
'name',
154+
'region',
155+
'algorithm',
156+
'size_unit',
157+
'size',
158+
'forwarding_rules',
159+
'health_check',
160+
'sticky_sessions',
161+
'tag',
162+
'droplet_ids',
163+
'redirect_http_to_https',
164+
'enable_proxy_protocol',
165+
'enable_backend_keepalive',
166+
'http_idle_timeout_seconds',
167+
'vpc_uuid',
168+
'disable_lets_encrypt_dns_records',
169+
'project_id',
170+
'firewall',
171+
'network',
172+
'network_stack',
173+
'type',
174+
'domains',
175+
'glb_settings',
176+
'target_load_balancer_ids',
177+
'tls_cipher_policy',
178+
] as $property) {
179+
if (\array_key_exists($property, $payload)) {
180+
$data[$property] = $payload[$property];
181+
}
182+
}
183+
184+
return $data;
185+
}
186+
187+
private static function normalizeArray(array|\stdClass $value): array
188+
{
189+
if ($value instanceof \stdClass) {
190+
$value = \get_object_vars($value);
191+
}
192+
193+
foreach ($value as $key => $subValue) {
194+
if ($subValue instanceof \stdClass || \is_array($subValue)) {
195+
$value[$key] = self::normalizeArray($subValue);
196+
}
197+
}
198+
199+
return $value;
90200
}
91201
}

src/Entity/StickySession.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ class StickySession extends AbstractEntity
2323

2424
public string $cookieName;
2525

26-
public string $cookieTtlSeconds;
26+
public int $cookieTtlSeconds;
2727
}

0 commit comments

Comments
 (0)