Skip to content

Commit e0f5a1f

Browse files
Fix more entity hydration bugs
1 parent d1c7f0d commit e0f5a1f

15 files changed

+125
-208
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ CHANGE LOG
22
==========
33

44

5+
## 5.0.4 (UPCOMING)
6+
7+
* Fixed a bunch of entity hydration bugs
8+
9+
510
## 5.0.3 (11/03/2025)
611

712
* Fixed hydration of the Network class with IPv6 addresses

src/Entity/Account.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ final class Account extends AbstractEntity
4141
public function build(array $parameters): void
4242
{
4343
foreach ($parameters as $property => $value) {
44-
switch ($property) {
45-
case 'team':
46-
if (\is_object($value)) {
47-
$this->team = new Team($value);
48-
}
49-
unset($parameters[$property]);
50-
51-
break;
44+
$property = static::convertToCamelCase($property);
45+
46+
if ('team' === $property) {
47+
$this->team = new Team($value);
48+
} elseif (\property_exists($this, $property)) {
49+
$this->$property = $value;
5250
}
5351
}
54-
55-
parent::build($parameters);
5652
}
5753
}

src/Entity/Action.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ final class Action extends AbstractEntity
4040

4141
public function build(array $parameters): void
4242
{
43-
parent::build($parameters);
44-
4543
foreach ($parameters as $property => $value) {
44+
$property = static::convertToCamelCase($property);
45+
4646
if ('region' === $property) {
4747
$this->region = new Region($value);
48+
} elseif (\property_exists($this, $property)) {
49+
$this->$property = $value;
4850
}
4951
}
5052
}

src/Entity/DatabaseCluster.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,19 @@ final class DatabaseCluster extends AbstractEntity
6464

6565
public function build(array $parameters): void
6666
{
67-
parent::build($parameters);
68-
6967
foreach ($parameters as $property => $value) {
68+
$property = static::convertToCamelCase($property);
69+
7070
if ('connection' === $property) {
7171
$this->connection = new DatabaseConnection($value);
72-
}
73-
74-
if ('private_connection' === $property) {
72+
} elseif ('privateConnection' === $property) {
7573
$this->privateConnection = new DatabaseConnection($value);
76-
}
77-
78-
if ('users' === $property && \is_array($value)) {
79-
$this->users = [];
80-
foreach ($value as $user) {
81-
if (\is_object($user)) {
82-
$this->users[] = new DatabaseUser($user);
83-
}
84-
}
85-
}
86-
87-
if ('maintenance_window' === $property) {
74+
} elseif ('users' === $property) {
75+
$this->users = array_map(fn ($v) => new DatabaseUser($v), $value);
76+
} elseif ('maintenanceWindow' === $property) {
8877
$this->maintenanceWindow = new DatabaseMaintenanceWindow($value);
78+
} elseif (\property_exists($this, $property)) {
79+
$this->$property = $value;
8980
}
9081
}
9182
}

src/Entity/DatabasePool.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ final class DatabasePool extends AbstractEntity
3535

3636
public function build(array $parameters): void
3737
{
38-
parent::build($parameters);
39-
4038
foreach ($parameters as $property => $value) {
39+
$property = static::convertToCamelCase($property);
40+
4141
if ('connection' === $property) {
4242
$this->connection = new DatabaseConnection($value);
43-
}
44-
45-
if ('private_connection' === $property) {
43+
} elseif ('privateConnection' === $property) {
4644
$this->privateConnection = new DatabaseConnection($value);
45+
} elseif (\property_exists($this, $property)) {
46+
$this->$property = $value;
4747
}
4848
}
4949
}

src/Entity/DatabaseReplica.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ final class DatabaseReplica extends AbstractEntity
4040

4141
public function build(array $parameters): void
4242
{
43-
parent::build($parameters);
44-
4543
foreach ($parameters as $property => $value) {
44+
$property = static::convertToCamelCase($property);
45+
4646
if ('connection' === $property) {
4747
$this->connection = new DatabaseConnection($value);
48-
}
49-
50-
if ('private_connection' === $property) {
48+
} elseif ('privateConnection' === $property) {
5149
$this->privateConnection = new DatabaseConnection($value);
50+
} elseif (\property_exists($this, $property)) {
51+
$this->$property = $value;
5252
}
5353
}
5454
}

src/Entity/DatabaseUser.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ final class DatabaseUser extends AbstractEntity
2929

3030
public function build(array $parameters): void
3131
{
32-
parent::build($parameters);
33-
3432
foreach ($parameters as $property => $value) {
35-
if ('mysql_settings' === $property) {
33+
$property = static::convertToCamelCase($property);
34+
35+
if ('mysqlSettings' === $property) {
3636
$this->mysqlSettings = new DatabaseMysqlSettings($value);
37+
} elseif (\property_exists($this, $property)) {
38+
$this->$property = $value;
3739
}
3840
}
3941
}

src/Entity/Droplet.php

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -91,76 +91,44 @@ final class Droplet extends AbstractEntity
9191
public function build(array $parameters): void
9292
{
9393
foreach ($parameters as $property => $value) {
94-
switch ($property) {
95-
case 'networks':
96-
if (\is_object($value)) {
97-
if (\property_exists($value, 'v4')) {
98-
foreach ($value->v4 as $subValue) {
99-
$subValue->version = 4;
100-
$this->networks[] = new Network($subValue);
101-
}
102-
}
103-
104-
if (\property_exists($value, 'v6')) {
105-
foreach ($value->v6 as $subValue) {
106-
/** @var object{ip_address: string, netmask: int, gateway: string, type: string} $subValue */
107-
$this->networks[] = new Network((object) [
108-
'ip_address' => $subValue->ip_address,
109-
'netmask' => null,
110-
'gateway' => $subValue->gateway,
111-
'type' => $subValue->type,
112-
'cidr' => \sprintf('%s/%d', $subValue->ip_address, $subValue->netmask),
113-
'version' => 6,
114-
]);
115-
}
116-
}
117-
}
118-
unset($parameters[$property]);
119-
120-
break;
121-
122-
case 'kernel':
123-
if (\is_object($value)) {
124-
$this->kernel = new Kernel($value);
125-
}
126-
unset($parameters[$property]);
127-
128-
break;
129-
130-
case 'size':
131-
if (\is_object($value)) {
132-
$this->size = new Size($value);
133-
}
134-
unset($parameters[$property]);
94+
$property = static::convertToCamelCase($property);
13595

136-
break;
137-
138-
case 'region':
139-
if (\is_object($value)) {
140-
$this->region = new Region($value);
96+
if ('networks' === $property) {
97+
if (\property_exists($value, 'v4')) {
98+
foreach ($value->v4 as $subValue) {
99+
$subValue->version = 4;
100+
$this->networks[] = new Network($subValue);
141101
}
142-
unset($parameters[$property]);
143-
144-
break;
145-
146-
case 'image':
147-
if (\is_object($value)) {
148-
$this->image = new Image($value);
102+
}
103+
104+
if (\property_exists($value, 'v6')) {
105+
foreach ($value->v6 as $subValue) {
106+
/** @var object{ip_address: string, netmask: int, gateway: string, type: string} $subValue */
107+
$this->networks[] = new Network((object) [
108+
'ip_address' => $subValue->ip_address,
109+
'netmask' => null,
110+
'gateway' => $subValue->gateway,
111+
'type' => $subValue->type,
112+
'cidr' => \sprintf('%s/%d', $subValue->ip_address, $subValue->netmask),
113+
'version' => 6,
114+
]);
149115
}
150-
unset($parameters[$property]);
151-
152-
break;
153-
154-
case 'next_backup_window':
155-
$this->nextBackupWindow = new NextBackupWindow($value);
156-
unset($parameters[$property]);
157-
158-
break;
116+
}
117+
} elseif ('kernel' === $property) {
118+
$this->kernel = new Kernel($value);
119+
} elseif ('size' === $property) {
120+
$this->size = new Size($value);
121+
} elseif ('region' === $property) {
122+
$this->region = new Region($value);
123+
} elseif ('image' === $property) {
124+
$this->image = new Image($value);
125+
} elseif ('nextBackupWindow' === $property) {
126+
$this->nextBackupWindow = new NextBackupWindow($value);
127+
} elseif (\property_exists($this, $property)) {
128+
$this->$property = $value;
159129
}
160130
}
161131

162-
parent::build($parameters);
163-
164132
$this->backupsEnabled = \in_array('backups', $this->features, true);
165133
$this->virtIOEnabled = \in_array('virtio', $this->features, true);
166134
$this->privateNetworkingEnabled = \in_array('private_networking', $this->features, true);

src/Entity/Firewall.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,16 @@ final class Firewall extends AbstractEntity
4141
public function build(array $parameters): void
4242
{
4343
foreach ($parameters as $property => $value) {
44-
switch ($property) {
45-
case 'inbound_rules':
46-
if (\is_array($value)) {
47-
$this->inboundRules = [];
48-
foreach ($value as $key => $rule) {
49-
if (\is_object($rule)) {
50-
$this->inboundRules[$key] = new FirewallRuleInbound($rule);
51-
}
52-
}
53-
}
54-
unset($parameters[$property]);
55-
56-
break;
57-
58-
case 'outbound_rules':
59-
if (\is_array($value)) {
60-
$this->outboundRules = [];
61-
foreach ($value as $key => $rule) {
62-
if (\is_object($rule)) {
63-
$this->outboundRules[$key] = new FirewallRuleOutbound($rule);
64-
}
65-
}
66-
}
67-
unset($parameters[$property]);
68-
69-
break;
44+
$property = static::convertToCamelCase($property);
45+
46+
if ('inbound_rules' === $property) {
47+
$this->inboundRules = array_map(fn ($v) => new FirewallRuleInbound($v), $value);
48+
} elseif ('outbound_rules' === $property) {
49+
$this->outboundRules = array_map(fn ($v) => new FirewallRuleOutbound($v), $value);
50+
} elseif (\property_exists($this, $property)) {
51+
$this->$property = $value;
7052
}
7153
}
72-
73-
parent::build($parameters);
7454
}
7555

7656
public function setCreatedAt(string $createdAt): void

src/Entity/FirewallRuleInbound.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ final class FirewallRuleInbound extends FirewallRule
2525
public function build(array $parameters): void
2626
{
2727
foreach ($parameters as $property => $value) {
28-
switch ($property) {
29-
case 'sources':
30-
if (\is_object($value)) {
31-
$this->sources = new FirewallLocations($value);
32-
}
33-
unset($parameters[$property]);
34-
35-
break;
28+
$property = static::convertToCamelCase($property);
29+
30+
if ('sources' === $property) {
31+
$this->sources = new FirewallLocations($value);
32+
} elseif (\property_exists($this, $property)) {
33+
$this->$property = $value;
3634
}
3735
}
38-
39-
parent::build($parameters);
4036
}
4137

4238
public function toArray(): array

0 commit comments

Comments
 (0)