Skip to content

Commit 113fa46

Browse files
committed
Make setAdditionalData methods and parsing functions handle nullable fields
1 parent 31c6e0e commit 113fa46

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

src/Models/Prices/Price.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public static function parse($input): ?self
3636
return null;
3737
}
3838

39-
return new self($input->net, $input->gross);
39+
return new self($input->net ?? '0', $input->gross ?? '0');
4040
}
4141
}

src/Models/Prices/Prices.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public function all(?RequestOpts $requestOpts = null): ?self
9292
*/
9393
public function setAdditionalData($input)
9494
{
95-
$this->currency = $input->currency;
96-
$this->vat_rate = $input->vat_rate;
97-
$this->image = Price::parse($input->image->price_per_gb_month);
98-
$this->floating_ip = Price::parse($input->floating_ip->price_monthly);
99-
$this->traffic = Price::parse($input->traffic->price_per_tb);
100-
$this->server_backup = $input->server_backup->percentage;
101-
$this->volume = Price::parse($input->volume->price_per_gb_month);
95+
$this->currency = $input->currency ?? null;
96+
$this->vat_rate = $input->vat_rate ?? null;
97+
$this->image = property_exists($input, 'image') ? Price::parse($input->image->price_per_gb_month) : null;
98+
$this->floating_ip = property_exists($input, 'floating_ip') ? Price::parse($input->floating_ip->price_monthly) : null;
99+
$this->traffic = property_exists($input, 'traffic') ? Price::parse($input->traffic->price_per_tb) : null;
100+
$this->server_backup = property_exists($input, 'server_backup') ? ($input->server_backup->percentage ?? null) : null;
101+
$this->volume = property_exists($input, 'volume') ? Price::parse($input->volume->price_per_gb_month) : null;
102102
if (property_exists($input, 'server_types')) {
103103
$this->server_types = collect($input->server_types)->map(function ($serverType) {
104104
return ServerType::parse($serverType);

src/Models/Prices/ServerTypePrice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public static function parse($input): ?self
4343
return null;
4444
}
4545

46-
return new self($input->location, Price::parse($input->price_hourly), Price::parse($input->price_monthly));
46+
return new self($input->location ?? '', Price::parse($input->price_hourly ?? null), Price::parse($input->price_monthly ?? null));
4747
}
4848
}

src/Models/Servers/Server.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,37 +212,37 @@ public function __construct(int $serverId, ?GuzzleClient $httpClient = null)
212212
*/
213213
public function setAdditionalData($data)
214214
{
215-
$this->name = $data->name;
216-
$this->status = $data->status ?: null;
217-
$this->public_net = $data->public_net ?: null;
218-
$this->publicNet = $data->public_net ?: null;
219-
$this->private_net = property_exists($data, 'private_net') ? $data->private_net : [];
220-
$this->privateNet = property_exists($data, 'private_net') ? $data->private_net : [];
221-
$this->server_type = $data->server_type ? ServerType::parse($data->server_type) : null;
222-
$this->serverType = $data->server_type ? ServerType::parse($data->server_type) : null;
223-
$this->datacenter = $data->datacenter ? Datacenter::parse($data->datacenter) : null;
215+
$this->name = $data->name ?? null;
216+
$this->status = $data->status ?? null;
217+
$this->public_net = $data->public_net ?? null;
218+
$this->publicNet = $data->public_net ?? null;
219+
$this->private_net = $data->private_net ?? [];
220+
$this->privateNet = $data->private_net ?? [];
221+
$this->server_type = property_exists($data, 'server_type') && $data->server_type ? ServerType::parse($data->server_type) : null;
222+
$this->serverType = property_exists($data, 'server_type') && $data->server_type ? ServerType::parse($data->server_type) : null;
223+
$this->datacenter = property_exists($data, 'datacenter') && $data->datacenter ? Datacenter::parse($data->datacenter) : null;
224224
$this->location = property_exists($data, 'location') && $data->location ? Location::parse($data->location) : null;
225-
$this->created = $data->created;
226-
$this->image = $data->image ? Image::parse($data->image) : null;
227-
$this->iso = $data->iso ? ISO::parse($data->iso) : null;
225+
$this->created = $data->created ?? null;
226+
$this->image = property_exists($data, 'image') && $data->image ? Image::parse($data->image) : null;
227+
$this->iso = property_exists($data, 'iso') && $data->iso ? ISO::parse($data->iso) : null;
228228
$this->rescue_enabled = $data->rescue_enabled ?? null;
229229
$this->rescueEnabled = $data->rescue_enabled ?? null;
230230
$this->locked = $data->locked ?? null;
231-
$this->backup_window = $data->backup_window ?: null;
232-
$this->backupWindow = $data->backup_window ?: null;
233-
$this->outgoing_traffic = $data->outgoing_traffic ?: null;
234-
$this->outgoingTraffic = $data->outgoing_traffic ?: null;
235-
$this->ingoing_traffic = $data->ingoing_traffic ?: null;
236-
$this->ingoingTraffic = $data->ingoing_traffic ?: null;
237-
$this->included_traffic = $data->included_traffic ?: null;
238-
$this->includedTraffic = $data->included_traffic ?: null;
239-
$this->volumes = property_exists($data, 'volumes') ? $data->volumes : [];
240-
$this->protection = $data->protection ? Protection::parse($data->protection) : null;
241-
$this->labels = get_object_vars($data->labels);
242-
$this->primary_disk_size = $data->primary_disk_size ?: null;
243-
$this->primaryDiskSize = $data->primary_disk_size ?: null;
244-
$this->placement_group = property_exists($data, 'placement_group') ? $data->placement_group : null;
245-
$this->load_balancers = property_exists($data, 'load_balancers') ? $data->load_balancers : [];
231+
$this->backup_window = $data->backup_window ?? null;
232+
$this->backupWindow = $data->backup_window ?? null;
233+
$this->outgoing_traffic = $data->outgoing_traffic ?? null;
234+
$this->outgoingTraffic = $data->outgoing_traffic ?? null;
235+
$this->ingoing_traffic = $data->ingoing_traffic ?? null;
236+
$this->ingoingTraffic = $data->ingoing_traffic ?? null;
237+
$this->included_traffic = $data->included_traffic ?? null;
238+
$this->includedTraffic = $data->included_traffic ?? null;
239+
$this->volumes = $data->volumes ?? [];
240+
$this->protection = property_exists($data, 'protection') && $data->protection ? Protection::parse($data->protection) : null;
241+
$this->labels = property_exists($data, 'labels') && $data->labels ? get_object_vars($data->labels) : [];
242+
$this->primary_disk_size = $data->primary_disk_size ?? null;
243+
$this->primaryDiskSize = $data->primary_disk_size ?? null;
244+
$this->placement_group = $data->placement_group ?? null;
245+
$this->load_balancers = $data->load_balancers ?? [];
246246

247247
return $this;
248248
}

0 commit comments

Comments
 (0)