mirror of
https://github.com/scummvm/scummvm-web.git
synced 2026-11-18 21:35:12 +00:00
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
This commit is contained in:
1 parent
d455741806
commit
4d78399ea2
15 files changed
+396
-376
No files matched your search
@@ -8,13 +8,16 @@ namespace ScummVM\Objects;
|
||||
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)
|
||||
{
|
||||
if (array_key_exists('id', $data)) {
|
||||
$this->id = $data['id'];
|
||||
} elseif (array_key_exists('support', $data)) { // Compatibility
|
||||
$this->id = $data['game_id'];
|
||||
try {
|
||||
$this->id = $this->assignFromArray('id', $data);
|
||||
} catch (\ErrorException $ex) {
|
||||
throw new \ErrorException(\sprintf(self::NO_ID,\get_class($this)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,19 +29,24 @@ abstract class DataObject
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return strval($this->id);
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to safely retrieve an object
|
||||
* from an array.
|
||||
*/
|
||||
protected function assignFromArray($key, $array)
|
||||
protected function assignFromArray($key, $array, $required = false)
|
||||
{
|
||||
if (array_key_exists($key, $array)) {
|
||||
return $array[$key];
|
||||
} else {
|
||||
return $key;
|
||||
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];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user