Commit 023246f9 authored by Simon Welsh's avatar Simon Welsh

Update objects to use the new coversion methods

parent 690c36cd
...@@ -26,105 +26,40 @@ class Droplet extends Object { ...@@ -26,105 +26,40 @@ class Droplet extends Object {
public ?Kernel $kernel; public ?Kernel $kernel;
public function __construct(ConstMapAccess<string, mixed> $data) { public function __construct(ConstMapAccess<string, mixed> $data) {
// id $this->id = nullthrows(static::_toInt($data, 'id'));
if ($data->containsKey('id')) { $this->name = nullthrows(static::_toString($data, 'name'));
$this->id = static::_toInt($data->at('id'), 'id'); $this->memory = nullthrows(static::_toInt($data, 'memory'));
} else { $this->vcpus = nullthrows(static::_toInt($data, 'vcpus'));
throw new IAE('An id value is required'); $this->disk = nullthrows(static::_toInt($data, 'disk'));
} $this->locked = nullthrows(static::_toBool($data, 'locked'));
// name $this->created_at = nullthrows(static::_toDateTime($data,
if ($data->containsKey('name')) { 'created_at'));
$this->name = static::_toString($data->at('name'), 'name');
} else {
throw new IAE('A name value is required');
}
// memory
if ($data->containsKey('memory')) {
$this->memory = static::_toInt($data->at('memory'), 'memory');
} else {
throw new IAE('A memory value is required');
}
// vcpus
if ($data->containsKey('vcpus')) {
$this->vcpus = static::_toInt($data->at('vcpus'), 'vcpus');
} else {
throw new IAE('A vcpus value is required');
}
// disk
if ($data->containsKey('disk')) {
$this->disk = static::_toInt($data->at('disk'), 'disk');
} else {
throw new IAE('A disk value is required');
}
// locked
if ($data->containsKey('locked')) {
$this->locked = static::_toBool($data->at('locked'), 'locked');
} else {
throw new IAE('A locked value is required');
}
// created_at
if ($data->containsKey('created_at')) {
$this->created_at = static::_toDateTime($data->at('created_at'),
'created_at');
} else {
throw new IAE('A created_at value is required');
}
// Status
if ($data->containsKey('status')) { if ($data->containsKey('status')) {
$this->status = Status::assert($data->at('status')); $this->status = Status::assert($data->at('status'));
} else { } else {
throw new IAE('A status value is required'); throw new IAE('A status value is required');
} }
// backup_ids $this->backup_ids = nullthrows(static::_toSet(
if ($data->containsKey('backup_ids')) { $data,
$this->backup_ids = static::_toSet(
$data->at('backup_ids'),
'backup_ids', 'backup_ids',
class_meth(static::class, '_toInt'), class_meth(static::class, '_toInt'),
); ));
} else { $this->snapshot_ids = nullthrows(static::_toSet(
throw new IAE('A backup_ids value is required'); $data,
}
// snapshots_ids
if ($data->containsKey('snapshot_ids')) {
$this->snapshot_ids = static::_toSet(
$data->at('snapshot_ids'),
'snapshot_ids', 'snapshot_ids',
class_meth(static::class, '_toInt'), class_meth(static::class, '_toInt'),
); ));
} else { $this->features = nullthrows(static::_toSet(
throw new IAE('A snapshot_ids value is required'); $data,
}
// features
if ($data->containsKey('features')) {
$this->features = static::_toSet(
$data->at('features'),
'features', 'features',
class_meth(static::class, '_toString'), class_meth(static::class, '_toString'),
); ));
} else { $this->region = nullthrows(static::_toRegion($data, 'region'));
throw new IAE('A features value is required'); $this->image = nullthrows(static::_toImage($data, 'image'));
} if ($data->containsKey('size_slug')) {
// region $this->size = nullthrows(static::_toString($data, 'size_slug'));
if ($data->containsKey('region')) {
$this->region = static::_toRegion($data->at('region'), 'region');
} else {
throw new IAE('A region value is required');
}
// image
if ($data->containsKey('image')) {
$this->image = static::_toImage($data->at('image'), 'image');
} else {
throw new IAE('An image value is required');
}
// size
if ($data->containsKey('size')) {
$this->size = static::_toString($data->at('size'), 'size');
} else if ($data->containsKey('size_slug')) {
$this->size = static::_toString($data->at('size_slug'),
'size_slug');
} else { } else {
throw new IAE('A size value is required'); $this->size = nullthrows(static::_toString($data, 'size'));
} }
if (!$data->containsKey('networks')) { if (!$data->containsKey('networks')) {
throw new IAE('A networks object is required'); throw new IAE('A networks object is required');
...@@ -138,30 +73,17 @@ class Droplet extends Object { ...@@ -138,30 +73,17 @@ class Droplet extends Object {
} else { } else {
throw new IAE('Expected a map for key networks'); throw new IAE('Expected a map for key networks');
} }
// ipv4 $this->ipv4 = nullthrows(static::_toVector(
if ($networks->containsKey('v4')) { $networks,
$this->ipv4 = static::_toVector(
$networks->at('v4'),
'v4', 'v4',
class_meth(static::class, '_toNetwork'), class_meth(static::class, '_toNetwork'),
); ));
} else { $this->ipv6 = nullthrows(static::_toVector(
throw new IAE('An v4 value is required'); $networks,
}
// ipv6
if ($networks->containsKey('v6')) {
$this->ipv6 = static::_toVector(
$networks->at('v6'),
'v6', 'v6',
class_meth(static::class, '_toNetwork'), class_meth(static::class, '_toNetwork'),
); ));
} else { $this->kernel = static::_toKernel($data, 'kernel', false);
throw new IAE('An v6 value is required');
}
// kernel
if ($data->containsKey('kernel')) {
$this->kernel = static::_toKernel($data->at('kernel'), 'kernel');
}
} }
public static function get_all_droplets( public static function get_all_droplets(
......
...@@ -15,51 +15,18 @@ class Image extends Object { ...@@ -15,51 +15,18 @@ class Image extends Object {
public int $min_disk_size; public int $min_disk_size;
public function __construct(ConstMapAccess<string, mixed> $data) { public function __construct(ConstMapAccess<string, mixed> $data) {
// id $this->id = nullthrows(static::_toInt($data, 'id'));
if ($data->containsKey('id')) { $this->name = nullthrows(static::_toString($data, 'name'));
$this->id = static::_toInt($data->at('id'), 'id'); $this->distribution = nullthrows(static::_toString($data,
} else { 'distribution'));
throw new IAE('An id value is required'); $this->slug = static::_toString($data, 'slug', false);
} $this->public = nullthrows(static::_toBool($data, 'public'));
// name $this->regions = nullthrows(static::_toSet(
if ($data->containsKey('name')) { $data,
$this->name = static::_toString($data->at('name'), 'name');
} else {
throw new IAE('A name value is required');
}
// distribution
if ($data->containsKey('distribution')) {
$this->distribution = static::_toString($data->at('distribution'),
'distribution');
} else {
throw new IAE('A distribution value is required');
}
// slug
if ($data->containsKey('slug')) {
$this->slug = static::_toString($data->at('slug'), 'slug');
}
// public
if ($data->containsKey('public')) {
$this->public = static::_toBool($data->at('public'), 'public');
} else {
throw new IAE('A public value is required');
}
// regions
if ($data->containsKey('regions')) {
$this->regions = static::_toSet(
$data->at('regions'),
'regions', 'regions',
class_meth(static::class, '_toString'), class_meth(static::class, '_toString'),
); ));
} else { $this->min_disk_size = nullthrows(static::_toInt($data,
throw new IAE('A regions value is required'); 'min_disk_size'));
}
// min_disk_size
if ($data->containsKey('min_disk_size')) {
$this->min_disk_size = static::_toInt($data->at('min_disk_size'),
'min_disk_size');
} else {
throw new IAE('A min_disk_size value is required');
}
} }
} }
...@@ -246,11 +246,10 @@ abstract class Object { ...@@ -246,11 +246,10 @@ abstract class Object {
} else { } else {
throw new IAE('Expected network data for key ' . $key); throw new IAE('Expected network data for key ' . $key);
} }
$ip_address = nullthrows(static::_toString($data->at('ip_address'), $ip_address = nullthrows(static::_toString($data, 'ip_address'));
'ip_address')); $netmask = nullthrows(static::_toString($data, 'netmask'));
$netmask = nullthrows(static::_toString($data->at('netmask'), 'netmask')); $gateway = nullthrows(static::_toString($data, 'gateway'));
$gateway = nullthrows(static::_toString($data->at('gateway'), 'gateway')); $type = nullthrows(static::_toString($data, 'type'));
$type = nullthrows(static::_toString($data->at('type'), 'type'));
return shape( return shape(
'ip_address' => $ip_address, 'ip_address' => $ip_address,
'netmask' => $netmask, 'netmask' => $netmask,
...@@ -279,9 +278,9 @@ abstract class Object { ...@@ -279,9 +278,9 @@ abstract class Object {
} else { } else {
throw new IAE('Expected kernel data for key ' . $key); throw new IAE('Expected kernel data for key ' . $key);
} }
$id = nullthrows(static::_toInt($data->at('id'), 'id')); $id = nullthrows(static::_toInt($data, 'id'));
$name = nullthrows(static::_toString($data->at('name'), 'name')); $name = nullthrows(static::_toString($data, 'name'));
$version = nullthrows(static::_toString($data->at('version'), 'version')); $version = nullthrows(static::_toString($data, 'version'));
return shape( return shape(
'id' => $id, 'id' => $id,
'name' => $name, 'name' => $name,
......
...@@ -13,43 +13,21 @@ class Region extends Object { ...@@ -13,43 +13,21 @@ class Region extends Object {
public Set<string> $features; public Set<string> $features;
public function __construct(ConstMapAccess<string, mixed> $data) { public function __construct(ConstMapAccess<string, mixed> $data) {
// slug $this->slug = nullthrows(static::_toString($data, 'slug'));
if ($data->containsKey('slug')) { $this->name = nullthrows(static::_toString($data, 'name'));
$this->slug = static::_toString($data->at('slug'), 'slug'); $this->sizes = nullthrows(static::_toSet(
} else { $data,
throw new IAE('A slug value is required');
}
// name
if ($data->containsKey('name')) {
$this->name = static::_toString($data->at('name'), 'name');
} else {
throw new IAE('A name value is required');
}
// sizes
if ($data->containsKey('sizes')) {
$this->sizes = static::_toSet(
$data->at('sizes'),
'sizes', 'sizes',
class_meth(static::class, '_toString'), class_meth(static::class, '_toString'),
); ));
} else { $this->available = nullthrows(static::_toBool(
throw new IAE('A sizes value is required'); $data,
} 'available',
// available ));
if ($data->containsKey('available')) { $this->features = nullthrows(static::_toSet(
$this->available = static::_toBool($data->at('available'), 'available'); $data,
} else {
throw new IAE('A available value is required');
}
// features
if ($data->containsKey('features')) {
$this->features = static::_toSet(
$data->at('features'),
'features', 'features',
class_meth(static::class, '_toString'), class_meth(static::class, '_toString'),
); ));
} else {
throw new IAE('A features value is required');
}
} }
} }
...@@ -4,7 +4,10 @@ namespace _91Carriage\Droplets; ...@@ -4,7 +4,10 @@ namespace _91Carriage\Droplets;
function nullthrows<T>(?T $x, ?string $message = null): T { function nullthrows<T>(?T $x, ?string $message = null): T {
if ($x === null) { if ($x === null) {
throw new \Exception($message === null ? $message : 'Unexpected null'); if ($message === null) {
$message = 'Unexpected null';
}
throw new \Exception($message);
} }
return $x; return $x;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment