From e9882291ffae99ba8039d27f5e922bf22fab1218 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 7 Jan 2022 10:56:53 +0900 Subject: [PATCH] Validate type --- src/CreateDescriptor.php | 7 +++++++ src/Exception/InvalidTypeException.php | 11 +++++++++++ tests/CreateDescriptorTest.php | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/Exception/InvalidTypeException.php create mode 100644 tests/CreateDescriptorTest.php diff --git a/src/CreateDescriptor.php b/src/CreateDescriptor.php index 69d4a692..1c2586c6 100644 --- a/src/CreateDescriptor.php +++ b/src/CreateDescriptor.php @@ -6,6 +6,7 @@ use Koriym\AppStateDiagram\Exception\DescriptorIsNotArrayException; use Koriym\AppStateDiagram\Exception\InvalidDescriptorException; +use Koriym\AppStateDiagram\Exception\InvalidTypeException; use stdClass; use function assert; @@ -18,6 +19,8 @@ final class CreateDescriptor { + private const VALID_TYPES = ['semantic', 'safe', 'unsafe', 'idempotent']; + /** * @param array $descriptorsArray * @@ -118,5 +121,9 @@ private function validateDescriptor(stdClass $descriptor): void if ($hasNoId) { throw new InvalidDescriptorException((string) json_encode($descriptor)); } + + if (isset($descriptor->type) && ! in_array($descriptor->type, self::VALID_TYPES)) { + throw new InvalidTypeException((string) $descriptor->type); + } } } diff --git a/src/Exception/InvalidTypeException.php b/src/Exception/InvalidTypeException.php new file mode 100644 index 00000000..aebe83cf --- /dev/null +++ b/src/Exception/InvalidTypeException.php @@ -0,0 +1,11 @@ +expectException(InvalidTypeException::class); + $stdClass = new stdClass(); + $stdClass->id = 'About'; + $stdClass->type = '_invalid_'; + $instances = ['About' => $stdClass]; + (new CreateDescriptor())($instances); + } +}