Skip to content

Commit 8a0e084

Browse files
author
Antoine Lelaisant
committed
fix: json-schema-interface
1 parent d250dcb commit 8a0e084

File tree

3 files changed

+64
-74
lines changed

3 files changed

+64
-74
lines changed

src/JsonSchema.php

+42-73
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,50 @@
66

77
/**
88
* @template T of mixed
9+
*
10+
* @implements JsonSchemaInterface<T>
911
*/
10-
abstract class JsonSchema implements JsonSchemaInterface
12+
class JsonSchema implements JsonSchemaInterface
1113
{
14+
private function __construct(
15+
protected readonly string $title,
16+
protected readonly string $description,
17+
protected readonly iterable $examples,
18+
protected readonly array $schema
19+
) {
20+
}
21+
22+
public function getTitle(): string
23+
{
24+
return $this->title;
25+
}
26+
27+
public function getDescription(): string
28+
{
29+
return $this->description;
30+
}
31+
32+
public function getExamples(): iterable
33+
{
34+
yield from $this->examples;
35+
}
36+
37+
public function getSchema(): array
38+
{
39+
return $this->schema;
40+
}
41+
1242
/**
13-
* @param JsonSchema<E> $schema
1443
* @template E of mixed
44+
* @param JsonSchemaInterface<E> $schema
1545
*
1646
* @return JsonSchema<null|E>
1747
*/
18-
public static function nullable(self $schema): self
48+
public static function nullable(JsonSchemaInterface $schema): self
1949
{
2050
return self::create(
21-
'',
22-
'',
51+
sprintf('Nullable<%s>', $schema->getTitle()),
52+
$schema->getDescription(),
2353
[...$schema->getExamples(), null],
2454
['oneOf' => [self::null(), $schema->jsonSerialize()]],
2555
);
@@ -36,64 +66,23 @@ public static function create(
3666
string $title,
3767
string $description,
3868
iterable $examples,
39-
$schema
69+
array $schema
4070
): self {
41-
return new class($title, $description, $examples, $schema) extends JsonSchema {
42-
/**
43-
* @var iterable<int, E>
44-
*/
45-
private iterable $examples;
46-
47-
/**
48-
* @param iterable<int, E> $examples
49-
* @param array<string, mixed> $schema
50-
*/
51-
public function __construct(
52-
private string $title,
53-
private string $description,
54-
iterable $examples,
55-
private $schema
56-
) {
57-
$this->examples = [...$examples];
58-
}
59-
60-
public function getTitle(): string
61-
{
62-
return $this->title;
63-
}
64-
65-
public function getDescription(): string
66-
{
67-
return $this->description;
68-
}
69-
70-
/**
71-
* @return iterable<int, E>
72-
*/
73-
public function getExamples(): iterable
74-
{
75-
yield from $this->examples;
76-
}
77-
78-
public function getSchema(): array
79-
{
80-
return $this->schema;
81-
}
82-
};
71+
return new self($title, $description, $examples, $schema);
8372
}
8473

8574
/**
8675
* @template I
8776
*
88-
* @param JsonSchema<I> $jsonSchema
77+
* @param JsonSchemaInterface<I> $jsonSchema
8978
*
9079
* @return JsonSchema<array<int, I>>
9180
*/
92-
public static function collection(self $jsonSchema): self
81+
public static function collection(JsonSchemaInterface $jsonSchema): self
9382
{
9483
return self::create(
9584
sprintf('Collection<%s>', $jsonSchema->getTitle()),
96-
'',
85+
$jsonSchema->getDescription(),
9786
[[...$jsonSchema->getExamples()]],
9887
[
9988
'type' => 'array',
@@ -103,9 +92,9 @@ public static function collection(self $jsonSchema): self
10392
}
10493

10594
/**
106-
* @return array<string, mixed>&array{title: string, description: string, examples: array<T>}
95+
* {@inheritdoc}
10796
*/
108-
public function jsonSerialize(): mixed
97+
public function jsonSerialize(): array
10998
{
11099
$schema = $this->getSchema();
111100

@@ -122,21 +111,6 @@ public function jsonSerialize(): mixed
122111
);
123112
}
124113

125-
/**
126-
* @return iterable<int, T>
127-
*/
128-
abstract public function getExamples(): iterable;
129-
130-
public function getTitle(): string
131-
{
132-
return '';
133-
}
134-
135-
public function getDescription(): string
136-
{
137-
return '';
138-
}
139-
140114
/**
141115
* @param scalar $value
142116
*
@@ -250,9 +224,4 @@ protected static function oneOf(...$schemas): array
250224
'oneOf' => $schemas,
251225
];
252226
}
253-
254-
/**
255-
* @return array<string, mixed>
256-
*/
257-
abstract protected function getSchema(): array;
258227
}

src/JsonSchemaInterface.php

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@
66

77
use JsonSerializable;
88

9+
/**
10+
* @template T of mixed
11+
*/
912
interface JsonSchemaInterface extends JsonSerializable
1013
{
14+
/**
15+
* @return array<string, mixed>&array{title: string, description: string, examples: array<T>}
16+
*/
17+
public function jsonSerialize(): array;
18+
19+
public function getTitle(): string;
20+
21+
public function getDescription(): string;
22+
23+
/**
24+
* @return iterable<int, T>
25+
*/
26+
public function getExamples(): iterable;
27+
28+
/**
29+
* @return array<string, mixed>
30+
*/
31+
public function getSchema(): array;
1132
}

src/Scalar/UuidSchema.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getDescription(): string
3434
/**
3535
* {@inheritDoc}
3636
*/
37-
protected function getSchema(): array
37+
public function getSchema(): array
3838
{
3939
return array_merge(
4040
self::string(),

0 commit comments

Comments
 (0)