Redesign EventData creation
This commit is contained in:
@@ -12,30 +12,8 @@ use LotGD\Core\Models\Character;
|
||||
*/
|
||||
class CharacterEventData extends EventContextData
|
||||
{
|
||||
/**
|
||||
* CharacterEventData constructor.
|
||||
* @param array $data Must contain field character.
|
||||
* @throws ArgumentException
|
||||
*/
|
||||
protected function __construct(array $data)
|
||||
{
|
||||
$mustHaveForm = ["character"];
|
||||
$doesHaveForm = array_keys($data);
|
||||
sort($mustHaveForm);
|
||||
sort($doesHaveForm);
|
||||
|
||||
if ($doesHaveForm !== $mustHaveForm) {
|
||||
throw new ArgumentException("A new CharacterEventData event must have a character data field.");
|
||||
}
|
||||
|
||||
if ($data["character"] instanceof Character === false) {
|
||||
throw new ArgumentException(sprintf(
|
||||
"data[character] must be an instance of %s, %s given.",
|
||||
Character::class,
|
||||
get_class($data["character"])
|
||||
));
|
||||
}
|
||||
|
||||
parent::__construct($data);
|
||||
}
|
||||
protected static $argumentConfig = [
|
||||
"character" => ["type" => Character::class, "required" => true],
|
||||
"value" => ["type" => "mixed", "required" => false]
|
||||
];
|
||||
}
|
||||
@@ -25,9 +25,47 @@ class EventContextData
|
||||
*/
|
||||
public static function create(array $data): self
|
||||
{
|
||||
if (isset(static::$argumentConfig)) {
|
||||
static::checkConfiguration($data);
|
||||
}
|
||||
|
||||
return new static($data);
|
||||
}
|
||||
|
||||
public static function checkConfiguration($data)
|
||||
{
|
||||
$configuration = static::$argumentConfig;
|
||||
$types = [
|
||||
"mixed" => function ($x) {return true;},
|
||||
"int" => function ($x) {return is_int($x);},
|
||||
"float" => function ($x) {return is_float($x);},
|
||||
"numeric" => function($x) {return is_numeric($x);},
|
||||
"string" => function($x) {return is_string($x);},
|
||||
];
|
||||
|
||||
$keys = array_keys($data);
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($configuration[$key])) {
|
||||
throw new ArgumentException(sprintf("%s does not accept a field called %s", static::class, $key));
|
||||
}
|
||||
}
|
||||
foreach ($configuration as $key => $config) {
|
||||
if ($config["required"] === true and !isset($data[$key])) {
|
||||
throw new ArgumentException(sprintf("%s must have a field called %s.", static::class, $key));
|
||||
}
|
||||
|
||||
if (isset($types[$config["type"]])) {
|
||||
if ($types[$config["type"]]($data[$key]) === false) {
|
||||
throw new ArgumentException(sprintf("The field %s of %s must be of type %s.", $key, static::class, $config["type"]));
|
||||
}
|
||||
} else {
|
||||
if (!$data[$key] instanceof $config["type"]) {
|
||||
throw new ArgumentException(sprintf("The field %s of %s must be of type %s", $key, static::class, $config["type"]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* protected constructor..
|
||||
* @see self::create
|
||||
|
||||
@@ -12,30 +12,7 @@ use LotGD\Core\Models\Viewpoint;
|
||||
*/
|
||||
class ViewpointDecorationEventData extends EventContextData
|
||||
{
|
||||
/**
|
||||
* ViewpointDecorationEventData constructor.
|
||||
* @param array $data Must contain field viewpoint.
|
||||
* @throws ArgumentException
|
||||
*/
|
||||
protected function __construct(array $data)
|
||||
{
|
||||
$mustHaveForm = ["viewpoint"];
|
||||
$doesHaveForm = array_keys($data);
|
||||
sort($mustHaveForm);
|
||||
sort($doesHaveForm);
|
||||
|
||||
if ($doesHaveForm !== $mustHaveForm) {
|
||||
throw new ArgumentException("A new ViewpointDecoration event must have a viewpoint..");
|
||||
}
|
||||
|
||||
if ($data["viewpoint"] instanceof Viewpoint === false) {
|
||||
throw new ArgumentException(sprintf(
|
||||
"data[viewpoint] must be an instance of %s, %s given.",
|
||||
Viewpoint::class,
|
||||
get_class($data["viewpoint"])
|
||||
));
|
||||
}
|
||||
|
||||
parent::__construct($data);
|
||||
}
|
||||
protected static $argumentConfig = [
|
||||
"viewpoint" => ["type" => Viewpoint::class, "required" => true]
|
||||
];
|
||||
}
|
||||
@@ -1224,8 +1224,8 @@ class BattleTest extends CoreModelTestCase
|
||||
$battle = $this->provideBuffBattleParticipants(new Buff([
|
||||
"slot" => "test",
|
||||
"rounds" => 99,
|
||||
"goodguyAttackModifier" => 2,
|
||||
"goodguyDefenseModifier" => 2,
|
||||
"goodguyAttackModifier" => 10,
|
||||
"goodguyDefenseModifier" => 10,
|
||||
"activateAt" => Buff::ACTIVATE_ROUNDSTART,
|
||||
]), 3);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user