-
-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathLoadBalancer.php
More file actions
201 lines (152 loc) · 5.23 KB
/
Copy pathLoadBalancer.php
File metadata and controls
201 lines (152 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
declare(strict_types=1);
/*
* This file is part of the DigitalOcean API library.
*
* (c) Antoine Kirk <contact@sbin.dk>
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DigitalOceanV2\Entity;
/**
* @author Jacob Holmes <jwh315@cox.net>
*/
final class LoadBalancer extends AbstractEntity
{
public string $id;
public string $name;
public string $ip;
public string $ipv6;
public string $projectId;
public int $sizeUnit;
public string $size;
public string $algorithm;
public string $status;
public string $createdAt;
/**
* @var ForwardingRule[]
*/
public array $forwardingRules;
public HealthCheck $healthCheck;
public StickySession $stickySessions;
public Region $region;
public string $tag;
public array $dropletIds;
public bool $redirectHttpToHttps;
public bool $enableProxyProtocol;
public bool $enableBackendKeepalive;
/**
* @var int<30, 600>
*/
public int $httpIdleTimeoutSeconds;
public string $vpcUuid;
public bool $disableLetsEncryptDnsRecords;
public array $firewall;
public string $network;
public string $networkStack;
public string $type;
public array $domains;
public array $glbSettings;
public array $targetLoadBalancerIds;
public string $tlsCipherPolicy;
public function build(array $parameters): void
{
foreach ($parameters as $property => $value) {
$property = static::convertToCamelCase($property);
if ('forwardingRules' === $property) {
$this->forwardingRules = \array_map(fn ($v) => new ForwardingRule($v), $value);
continue;
} elseif ('healthCheck' === $property) {
$this->healthCheck = new HealthCheck($value);
continue;
} elseif ('stickySessions' === $property) {
$this->stickySessions = new StickySession($value);
continue;
} elseif ('region' === $property) {
$this->region = new Region($value);
continue;
} elseif (
\in_array($property, ['firewall', 'domains', 'glbSettings', 'targetLoadBalancerIds'], true) &&
($value instanceof \stdClass || \is_array($value))
) {
$value = self::normalizeArray($value);
}
if (\property_exists($this, $property)) {
$this->$property = $value;
}
}
}
public function toArray(): array
{
$payload = parent::toArray();
unset($payload['id'], $payload['ip'], $payload['ipv6'], $payload['status'], $payload['created_at']);
if (isset($payload['region']) && $payload['region'] instanceof Region) {
$payload['region'] = $payload['region']->slug;
}
if (isset($payload['forwarding_rules']) && \is_array($payload['forwarding_rules'])) {
/** @var ForwardingRule[] $forwardingRules */
$forwardingRules = $payload['forwarding_rules'];
$payload['forwarding_rules'] = \array_map(function (ForwardingRule $rule): array {
return $rule->toArray();
}, $forwardingRules);
}
if (isset($payload['health_check']) && $payload['health_check'] instanceof HealthCheck) {
$payload['health_check'] = $payload['health_check']->toArray();
}
if (isset($payload['sticky_sessions']) && $payload['sticky_sessions'] instanceof StickySession) {
$payload['sticky_sessions'] = $payload['sticky_sessions']->toArray();
}
if (isset($payload['tag']) && '' !== $payload['tag']) {
unset($payload['droplet_ids']);
} else {
unset($payload['tag']);
}
$data = [];
foreach ([
'name',
'region',
'algorithm',
'size_unit',
'size',
'forwarding_rules',
'health_check',
'sticky_sessions',
'tag',
'droplet_ids',
'redirect_http_to_https',
'enable_proxy_protocol',
'enable_backend_keepalive',
'http_idle_timeout_seconds',
'vpc_uuid',
'disable_lets_encrypt_dns_records',
'project_id',
'firewall',
'network',
'network_stack',
'type',
'domains',
'glb_settings',
'target_load_balancer_ids',
'tls_cipher_policy',
] as $property) {
if (\array_key_exists($property, $payload)) {
$data[$property] = $payload[$property];
}
}
return $data;
}
private static function normalizeArray(array|\stdClass $value): array
{
if ($value instanceof \stdClass) {
$value = \get_object_vars($value);
}
foreach ($value as $key => $subValue) {
if ($subValue instanceof \stdClass || \is_array($subValue)) {
$value[$key] = self::normalizeArray($subValue);
}
}
return $value;
}
}