Commit 746f205f authored by Simon Welsh's avatar Simon Welsh

Change conversion methods to take the entire map object

parent bbe6ca34
......@@ -3,11 +3,23 @@
namespace _91Carriage\Droplets;
use DateTime;
use ConstMapAccess;
use InvalidArgumentException as IAE;
use Stringish;
abstract class Object {
public static function _toString(mixed $value, string $key): string {
public static function _toString(
ConstMapAccess<string, mixed> $data,
string $key,
bool $required = true,
): ?string {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$value = $data->at($key);
if (is_string($value)) {
return $value;
}
......@@ -20,7 +32,18 @@ abstract class Object {
throw new IAE('Expected a string for key ' . $key);
}
public static function _toInt(mixed $value, string $key): int {
public static function _toInt(
ConstMapAccess<string, mixed> $data,
string $key,
bool $required = true,
): ?int {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$value = $data->at($key);
if (is_int($value)) {
return $value;
}
......@@ -34,7 +57,18 @@ abstract class Object {
return intval($value);
}
public static function _toBool(mixed $value, string $key): bool {
public static function _toBool(
ConstMapAccess<string, mixed> $data,
string $key,
bool $required = true,
): ?bool {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$value = $data->at($key);
if (is_bool($value)) {
return $value;
}
......@@ -76,7 +110,18 @@ abstract class Object {
throw new IAE('Expected a boolean for key ' . $key);
}
public static function _toDateTime(mixed $value, string $key): DateTime {
public static function _toDateTime(
ConstMapAccess<string, mixed> $data,
string $key,
bool $required = true,
): ?DateTime {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$value = $data->at($key);
if ($value instanceof DateTime) {
return $value;
}
......@@ -89,31 +134,64 @@ abstract class Object {
throw new IAE('Expected a datetime for key ' . $key);
}
public static function _toSet<T>(mixed $values, string $key,
(function(mixed, string):T) $handler): Set<T> {
public static function _toSet<T>(
ConstMapAccess<string, mixed> $data,
string $key,
(function(mixed, string, bool):T) $handler,
bool $required = true
): ?Set<T> {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$values = $data->at($key);
if (!$values instanceof Traversable) {
throw new IAE('Expected a traversable for key ' . $key);
}
$set = Set {};
foreach ($values as $value) {
$set[] = $handler($value, $key);
$set[] = nullthrows($handler(Map { $key => $value }, $key, true));
}
return $set;
}
public static function _toVector<T>(mixed $values, string $key,
(function(mixed, string):T) $handler): Vector<T> {
public static function _toVector<T>(
ConstMapAccess<string, mixed> $data,
string $key,
(function(mixed, string, bool):T) $handler,
bool $required = true
): ?Vector<T> {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$values = $data->at($key);
if (!$values instanceof Traversable) {
throw new IAE('Expected a traversable for key ' . $key);
}
$vector = Vector {};
foreach ($values as $value) {
$vector[] = $handler($value, $key);
$vector[] = nullthrows($handler(Map { $key => $value }, $key, true));
}
return $vector;
}
public static function _toRegion(mixed $values, string $key): Region {
public static function _toRegion(
ConstMapAccess<string, mixed> $data,
string $key,
bool $required = true,
): ?Region {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$values = $data->at($key);
if (is_object($values)) {
$values = get_object_vars($values);
}
......@@ -125,7 +203,18 @@ abstract class Object {
return new Region($data);
}
public static function _toImage(mixed $values, string $key): Image {
public static function _toImage(
ConstMapAccess<string, mixed> $data,
string $key,
bool $required = true,
): ?Image {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$values = $data->at($key);
if (is_object($values)) {
$values = get_object_vars($values);
}
......@@ -137,7 +226,18 @@ abstract class Object {
return new Image($data);
}
public static function _toNetwork(mixed $values, string $key): Network {
public static function _toNetwork(
ConstMapAccess<string, mixed> $data,
string $key,
bool $required = true,
): ?Network {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$values = $data->at($key);
if (is_object($values)) {
$values = get_object_vars($values);
}
......@@ -146,27 +246,11 @@ abstract class Object {
} else {
throw new IAE('Expected network data for key ' . $key);
}
if ($data->contains('ip_address')) {
$ip_address = static::_toString($data->at('ip_address'),
'ip_address');
} else {
throw new IAE('An ip_address is required');
}
if ($data->contains('netmask')) {
$netmask = static::_toString($data->at('netmask'), 'netmask');
} else {
throw new IAE('An netmask is required');
}
if ($data->contains('gateway')) {
$gateway = static::_toString($data->at('gateway'), 'gateway');
} else {
throw new IAE('An gateway is required');
}
if ($data->contains('type')) {
$type = static::_toString($data->at('type'), 'type');
} else {
throw new IAE('An type is required');
}
$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'));
return shape(
'ip_address' => $ip_address,
'netmask' => $netmask,
......@@ -175,7 +259,18 @@ abstract class Object {
);
}
public static function _toKernel(mixed $values, string $key): Kernel {
public static function _toKernel(
ConstMapAccess<string, mixed> $data,
string $key,
bool $required = true,
): ?Kernel {
if (!$data->containsKey($key)) {
if ($required) {
throw new IAE('A value for ' . $key . ' is required');
}
return null;
}
$values = $data->at($key);
if (is_object($values)) {
$values = get_object_vars($values);
}
......@@ -184,21 +279,9 @@ abstract class Object {
} else {
throw new IAE('Expected kernel data for key ' . $key);
}
if ($data->contains('id')) {
$id = static::_toInt($data->at('id'), 'id');
} else {
throw new IAE('An id is required');
}
if ($data->contains('name')) {
$name = static::_toString($data->at('name'), 'name');
} else {
throw new IAE('An name is required');
}
if ($data->contains('version')) {
$version = static::_toString($data->at('version'), 'version');
} else {
throw new IAE('An version is required');
}
$id = nullthrows(static::_toInt($data->at('id'), 'id'));
$name = nullthrows(static::_toString($data->at('name'), 'name'));
$version = nullthrows(static::_toString($data->at('version'), 'version'));
return shape(
'id' => $id,
'name' => $name,
......
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