Update objects to use the new coversion methods

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