From 79733f87f7b4857891a7cd3c66a5829b0dae35b7 Mon Sep 17 00:00:00 2001 From: Antoine Lelaisant Date: Thu, 20 Oct 2022 18:13:53 +0200 Subject: [PATCH] fix: make EnumType, CollectionType and ObjectType implementing JsonSchemaInterface --- src/CollectionSchema.php | 27 ++++++++++++++++++++++++++- src/EnumSchema.php | 26 +++++++++++++++++++++++--- src/ObjectSchema.php | 2 +- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/CollectionSchema.php b/src/CollectionSchema.php index 596ede5..84ee67f 100644 --- a/src/CollectionSchema.php +++ b/src/CollectionSchema.php @@ -11,7 +11,7 @@ * * @extends JsonSchema */ -abstract class CollectionSchema extends JsonSchema +abstract class CollectionSchema implements JsonSchemaInterface { /** * @param JsonSchema $itemSchema @@ -30,6 +30,11 @@ public function getTitle(): string return sprintf('Collection<%s>', $this->itemSchema->getTitle()); } + public function getDescription(): string + { + return sprintf('Collection of %s', $this->itemSchema->getDescription()); + } + protected function getUniqueItems(): ?bool { return null; @@ -71,4 +76,24 @@ public function getSchema(): array return $schema; } + + /** + * {@inheritdoc} + */ + public function jsonSerialize(): array + { + $schema = $this->getSchema(); + + /** + * @var array&array{title: string, description: string, examples: array} + */ + return array_merge( + $schema, + [ + 'title' => $this->getTitle(), + 'description' => $this->getDescription(), + 'examples' => [...$this->getExamples()], + ], + ); + } } diff --git a/src/EnumSchema.php b/src/EnumSchema.php index abedb36..67a5449 100644 --- a/src/EnumSchema.php +++ b/src/EnumSchema.php @@ -8,8 +8,13 @@ * @template E * @extends JsonSchema */ -abstract class EnumSchema extends JsonSchema +abstract class EnumSchema implements JsonSchemaInterface { + /** + * @return iterable + */ + abstract protected function getEnum(): iterable; + public function getExamples(): iterable { return $this->getEnum(); @@ -23,7 +28,22 @@ public function getSchema(): array } /** - * @return iterable + * {@inheritdoc} */ - abstract protected function getEnum(): iterable; + public function jsonSerialize(): array + { + $schema = $this->getSchema(); + + /** + * @var array&array{title: string, description: string, examples: array} + */ + return array_merge( + $schema, + [ + 'title' => $this->getTitle(), + 'description' => $this->getDescription(), + 'examples' => [...$this->getExamples()], + ], + ); + } } diff --git a/src/ObjectSchema.php b/src/ObjectSchema.php index 026bbdf..852c11e 100644 --- a/src/ObjectSchema.php +++ b/src/ObjectSchema.php @@ -8,7 +8,7 @@ * @template T of array * @extends JsonSchema */ -abstract class ObjectSchema extends JsonSchema +abstract class ObjectSchema implements JsonSchemaInterface { /** * @var array>