Skip to content

Commit

Permalink
fix: make EnumType, CollectionType and ObjectType implementing JsonSc…
Browse files Browse the repository at this point in the history
…hemaInterface
  • Loading branch information
Antoine Lelaisant committed Oct 20, 2022
1 parent 8de165d commit 79733f8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
27 changes: 26 additions & 1 deletion src/CollectionSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @extends JsonSchema<CollectionSchemaData>
*/
abstract class CollectionSchema extends JsonSchema
abstract class CollectionSchema implements JsonSchemaInterface
{
/**
* @param JsonSchema<I> $itemSchema
Expand All @@ -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;
Expand Down Expand Up @@ -71,4 +76,24 @@ public function getSchema(): array

return $schema;
}

/**
* {@inheritdoc}
*/
public function jsonSerialize(): array
{
$schema = $this->getSchema();

/**
* @var array<string, mixed>&array{title: string, description: string, examples: array<T>}
*/
return array_merge(
$schema,
[
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'examples' => [...$this->getExamples()],
],
);
}
}
26 changes: 23 additions & 3 deletions src/EnumSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
* @template E
* @extends JsonSchema<E>
*/
abstract class EnumSchema extends JsonSchema
abstract class EnumSchema implements JsonSchemaInterface
{
/**
* @return iterable<int, E>
*/
abstract protected function getEnum(): iterable;

public function getExamples(): iterable
{
return $this->getEnum();
Expand All @@ -23,7 +28,22 @@ public function getSchema(): array
}

/**
* @return iterable<int, E>
* {@inheritdoc}
*/
abstract protected function getEnum(): iterable;
public function jsonSerialize(): array
{
$schema = $this->getSchema();

/**
* @var array<string, mixed>&array{title: string, description: string, examples: array<T>}
*/
return array_merge(
$schema,
[
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'examples' => [...$this->getExamples()],
],
);
}
}
2 changes: 1 addition & 1 deletion src/ObjectSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @template T of array<string, mixed>
* @extends JsonSchema<T>
*/
abstract class ObjectSchema extends JsonSchema
abstract class ObjectSchema implements JsonSchemaInterface
{
/**
* @var array<string, JsonSchema<mixed>>
Expand Down

0 comments on commit 79733f8

Please sign in to comment.