Skip to content

Commit

Permalink
fix: json-schema-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Lelaisant committed Oct 3, 2022
1 parent d250dcb commit 8a0e084
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 74 deletions.
115 changes: 42 additions & 73 deletions src/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,50 @@

/**
* @template T of mixed
*
* @implements JsonSchemaInterface<T>
*/
abstract class JsonSchema implements JsonSchemaInterface
class JsonSchema implements JsonSchemaInterface
{
private function __construct(
protected readonly string $title,
protected readonly string $description,
protected readonly iterable $examples,
protected readonly array $schema
) {
}

public function getTitle(): string
{
return $this->title;
}

public function getDescription(): string
{
return $this->description;
}

public function getExamples(): iterable
{
yield from $this->examples;
}

public function getSchema(): array
{
return $this->schema;
}

/**
* @param JsonSchema<E> $schema
* @template E of mixed
* @param JsonSchemaInterface<E> $schema
*
* @return JsonSchema<null|E>
*/
public static function nullable(self $schema): self
public static function nullable(JsonSchemaInterface $schema): self
{
return self::create(
'',
'',
sprintf('Nullable<%s>', $schema->getTitle()),
$schema->getDescription(),
[...$schema->getExamples(), null],
['oneOf' => [self::null(), $schema->jsonSerialize()]],
);
Expand All @@ -36,64 +66,23 @@ public static function create(
string $title,
string $description,
iterable $examples,
$schema
array $schema
): self {
return new class($title, $description, $examples, $schema) extends JsonSchema {
/**
* @var iterable<int, E>
*/
private iterable $examples;

/**
* @param iterable<int, E> $examples
* @param array<string, mixed> $schema
*/
public function __construct(
private string $title,
private string $description,
iterable $examples,
private $schema
) {
$this->examples = [...$examples];
}

public function getTitle(): string
{
return $this->title;
}

public function getDescription(): string
{
return $this->description;
}

/**
* @return iterable<int, E>
*/
public function getExamples(): iterable
{
yield from $this->examples;
}

public function getSchema(): array
{
return $this->schema;
}
};
return new self($title, $description, $examples, $schema);
}

/**
* @template I
*
* @param JsonSchema<I> $jsonSchema
* @param JsonSchemaInterface<I> $jsonSchema
*
* @return JsonSchema<array<int, I>>
*/
public static function collection(self $jsonSchema): self
public static function collection(JsonSchemaInterface $jsonSchema): self
{
return self::create(
sprintf('Collection<%s>', $jsonSchema->getTitle()),
'',
$jsonSchema->getDescription(),
[[...$jsonSchema->getExamples()]],
[
'type' => 'array',
Expand All @@ -103,9 +92,9 @@ public static function collection(self $jsonSchema): self
}

/**
* @return array<string, mixed>&array{title: string, description: string, examples: array<T>}
* {@inheritdoc}
*/
public function jsonSerialize(): mixed
public function jsonSerialize(): array
{
$schema = $this->getSchema();

Expand All @@ -122,21 +111,6 @@ public function jsonSerialize(): mixed
);
}

/**
* @return iterable<int, T>
*/
abstract public function getExamples(): iterable;

public function getTitle(): string
{
return '';
}

public function getDescription(): string
{
return '';
}

/**
* @param scalar $value
*
Expand Down Expand Up @@ -250,9 +224,4 @@ protected static function oneOf(...$schemas): array
'oneOf' => $schemas,
];
}

/**
* @return array<string, mixed>
*/
abstract protected function getSchema(): array;
}
21 changes: 21 additions & 0 deletions src/JsonSchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@

use JsonSerializable;

/**
* @template T of mixed
*/
interface JsonSchemaInterface extends JsonSerializable
{
/**
* @return array<string, mixed>&array{title: string, description: string, examples: array<T>}
*/
public function jsonSerialize(): array;

public function getTitle(): string;

public function getDescription(): string;

/**
* @return iterable<int, T>
*/
public function getExamples(): iterable;

/**
* @return array<string, mixed>
*/
public function getSchema(): array;
}
2 changes: 1 addition & 1 deletion src/Scalar/UuidSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getDescription(): string
/**
* {@inheritDoc}
*/
protected function getSchema(): array
public function getSchema(): array
{
return array_merge(
self::string(),
Expand Down

0 comments on commit 8a0e084

Please sign in to comment.