Files
scummvm-web/include/Objects/DataObject.php
T
Mataniko 4d78399ea2 WEB: Add error handling about missing required fields
In the new data model, some field operate as keys for other parts of the model.
This now performs some validations and will throw an error if fields are missing.

For example: a Game must have an Engine, but does not require datafiles
2020-09-27 22:08:07 -04:00

52 lines
1.3 KiB
PHP

<?php
namespace ScummVM\Objects;
/**
* The DataObject class is inherited by all other new data objects
* and houses all common functions.
*/
abstract class DataObject
{
protected $id;
const NO_ID = 'Data object %s is missing an ID field';
const BAD_KEY = 'Field %s is required and cannot be empty for %s';
const NO_KEY = "Key is required and can not be blank";
public function __construct($data)
{
try {
$this->id = $this->assignFromArray('id', $data);
} catch (\ErrorException $ex) {
throw new \ErrorException(\sprintf(self::NO_ID,\get_class($this)));
}
}
/* Get the ID. */
public function getId()
{
return $this->id;
}
public function __toString()
{
return $this->id;
}
/**
* Helper method to safely retrieve an object
* from an array.
*/
protected function assignFromArray($key, $array, $required = false)
{
if ($required) {
if ($key === '') {
throw new \ErrorException(\sprintf(self::NO_KEY, $key, \get_class($this)));
}
if (!isset($array[$key]) || $array[$key] === '') {
throw new \ErrorException(\sprintf(self::BAD_KEY, $key, \get_class($this)));
}
}
return $array[$key];
}
}