Adds additional typehints to LotGD\Core\Events\*

This commit is contained in:
Vassyli
2020-12-24 12:20:56 +01:00
parent 363c4678d3
commit 709b96ea52
6 changed files with 54 additions and 31 deletions
+12 -1
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace LotGD\Core\Events;
use JetBrains\PhpStorm\ArrayShape;
use LotGD\Core\Models\Character;
/**
@@ -10,7 +11,17 @@ use LotGD\Core\Models\Character;
*/
class CharacterEventData extends EventContextData
{
protected static $argumentConfig = [
#[ArrayShape([
"character" => [
"type" => Character::class,
"required" => "bool",
],
"value" => [
"type" => "mixed",
"required" => "bool",
],
])]
protected static array $argumentConfig = [
"character" => ["type" => Character::class, "required" => true],
"value" => ["type" => "mixed", "required" => false],
];
+3 -10
View File
@@ -9,10 +9,6 @@ namespace LotGD\Core\Events;
*/
class EventContext
{
private $matchingPattern;
private $event;
private $data;
/**
* EventContext constructor.
* @param string $event The published event
@@ -20,13 +16,10 @@ class EventContext
* @param EventContextData $data
*/
public function __construct(
string $event,
string $matchingPattern,
EventContextData $data
private string $event,
private string $matchingPattern,
private EventContextData $data
) {
$this->event = $event;
$this->matchingPattern = $matchingPattern;
$this->data = $data;
}
/**
+12 -15
View File
@@ -13,7 +13,14 @@ use LotGD\Core\Exceptions\ArgumentException;
*/
class EventContextData
{
private $data;
protected static ?array $argumentConfig = null;
/**
* protected constructor..
* @see self::create
* @param array $data
*/
protected function __construct(private array $data) {}
/**
* Creates a new instance of a data container.
@@ -33,10 +40,10 @@ class EventContextData
/**
* Checks a field configuration given in self::$argumentConfig.
* @param $data
* @param array $data
* @throws ArgumentException
*/
public static function checkConfiguration($data)
public static function checkConfiguration(array $data)
{
$configuration = static::$argumentConfig;
$types = [
@@ -80,16 +87,6 @@ class EventContextData
}
}
/**
* protected constructor..
* @see self::create
* @param array $data
*/
protected function __construct(array $data)
{
$this->data = $data;
}
/**
* Returns true if container has a certain field.
* @param string $field
@@ -116,10 +113,10 @@ class EventContextData
/**
* Sets a field to a new value and returns a new data container.
* @param string $field
* @param $value
* @param mixed $value
* @return EventContextData
*/
public function set(string $field, $value): self
public function set(string $field, mixed $value): self
{
if ($this->has($field)) {
$data = $this->data;
+11 -2
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace LotGD\Core\Events;
use JetBrains\PhpStorm\ArrayShape;
use LotGD\Core\Exceptions\ArgumentException;
use LotGD\Core\Models\Scene;
use LotGD\Core\Models\Viewpoint;
@@ -24,8 +25,16 @@ class NavigateToSceneData extends EventContextData
* @param array $data Must contain fields referrer, viewpoint, scene, parameters and redirect; none more.
* @throws ArgumentException
*/
protected function __construct(array $data)
{
protected function __construct(
#[ArrayShape([
"referrer" => Scene::class . "|null",
"viewpoint" => Viewpoint::class,
"scene" => Scene::class,
"parameters" => "array",
"reddirect" => Scene::class . "|null",
])]
array $data,
) {
$mustHaveForm = ["referrer", "viewpoint", "scene", "parameters", "redirect"];
$doesHaveForm = \array_keys($data);
\sort($mustHaveForm);
+8 -2
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace LotGD\Core\Events;
use JetBrains\PhpStorm\ArrayShape;
use LotGD\Core\Exceptions\ArgumentException;
use LotGD\Core\Models\Character;
use LotGD\Core\Models\Scene;
@@ -21,8 +22,13 @@ class NewViewpointData extends EventContextData
* @param array $data
* @throws ArgumentException In case $data contains invalid data.
*/
protected function __construct(array $data)
{
protected function __construct(
#[ArrayShape([
"character" => Character::class,
"scene" => Scene::class . "|null",
])]
array $data,
) {
if (\array_keys($data) !== ["character", "scene"]) {
throw new ArgumentException("A NewViewpoint event must have only character and scene.");
}
+8 -1
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace LotGD\Core\Events;
use JetBrains\PhpStorm\ArrayShape;
use LotGD\Core\Models\Viewpoint;
/**
@@ -10,7 +11,13 @@ use LotGD\Core\Models\Viewpoint;
*/
class ViewpointDecorationEventData extends EventContextData
{
protected static $argumentConfig = [
#[ArrayShape([
"viewpoint" => [
"type" => Viewpoint::class,
"required" => "bool",
],
])]
protected static array $argumentConfig = [
"viewpoint" => ["type" => Viewpoint::class, "required" => true],
];
}