Skip to content

Commit 61c0b00

Browse files
author
Antoine Lelaisant
committed
fix: JsonSchemaInterface implementations
1 parent 0b16fc1 commit 61c0b00

7 files changed

+55
-29
lines changed

src/CollectionSchema.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
/**
88
* @template I
99
*
10-
* @phpstan-type CollectionSchemaData array<I>
10+
* @type CollectionSchemaData array<I>
1111
*
12-
* @extends JsonSchema<CollectionSchemaData>
12+
* @implements JsonSchemaInterface<CollectionSchemaData>
1313
*/
1414
abstract class CollectionSchema implements JsonSchemaInterface
1515
{
1616
/**
17-
* @param JsonSchema<I> $itemSchema
17+
* @psalm-param JsonSchemaInterface<I> $itemSchema
1818
*/
19-
public function __construct(private JsonSchema $itemSchema)
19+
public function __construct(private JsonSchemaInterface $itemSchema)
2020
{
2121
}
2222

src/EnumSchema.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
/**
88
* @template E
9-
* @extends JsonSchema<E>
9+
*
10+
* @implements JsonSchemaInterface<E>
1011
*/
1112
abstract class EnumSchema implements JsonSchemaInterface
1213
{

src/JsonSchema.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Knp\JsonSchema;
66

7+
use Knp\JsonSchema\Validator\Errors;
8+
79
/**
810
* @template T of mixed
911
*
@@ -116,7 +118,7 @@ public function jsonSerialize(): array
116118
*
117119
* @return array<string, mixed>
118120
*/
119-
protected static function constant($value): array
121+
public static function constant($value): array
120122
{
121123
return [
122124
'const' => $value,
@@ -126,7 +128,7 @@ protected static function constant($value): array
126128
/**
127129
* @return array<string, mixed>
128130
*/
129-
protected static function null(): array
131+
public static function null(): array
130132
{
131133
return [
132134
'type' => 'null',
@@ -136,7 +138,7 @@ protected static function null(): array
136138
/**
137139
* @return array<string, mixed>
138140
*/
139-
protected static function text(): array
141+
public static function text(): array
140142
{
141143
return [
142144
'type' => 'string',
@@ -147,7 +149,7 @@ protected static function text(): array
147149
/**
148150
* @return array<string, mixed>
149151
*/
150-
protected static function boolean(): array
152+
public static function boolean(): array
151153
{
152154
return [
153155
'type' => 'boolean',
@@ -157,7 +159,7 @@ protected static function boolean(): array
157159
/**
158160
* @return array<string, mixed>
159161
*/
160-
protected static function string(?string $format = null): array
162+
public static function string(?string $format = null): array
161163
{
162164
$result = [
163165
...self::text(),
@@ -174,7 +176,7 @@ protected static function string(?string $format = null): array
174176
/**
175177
* @return array<string, mixed>
176178
*/
177-
protected static function integer(): array
179+
public static function integer(): array
178180
{
179181
return [
180182
'type' => 'integer',
@@ -184,7 +186,7 @@ protected static function integer(): array
184186
/**
185187
* @return array<string, mixed>
186188
*/
187-
protected static function number(): array
189+
public static function number(): array
188190
{
189191
return [
190192
'type' => 'number',
@@ -194,7 +196,7 @@ protected static function number(): array
194196
/**
195197
* @return array<string, mixed>
196198
*/
197-
protected static function date(): array
199+
public static function date(): array
198200
{
199201
return [
200202
'type' => 'string',
@@ -205,7 +207,7 @@ protected static function date(): array
205207
/**
206208
* @return array<string, mixed>
207209
*/
208-
protected static function positiveInteger(): array
210+
public static function positiveInteger(): array
209211
{
210212
return [
211213
...self::integer(),
@@ -218,7 +220,7 @@ protected static function positiveInteger(): array
218220
*
219221
* @return array{oneOf: array<array<string, mixed>>}
220222
*/
221-
protected static function oneOf(...$schemas): array
223+
public static function oneOf(...$schemas): array
222224
{
223225
return [
224226
'oneOf' => $schemas,

src/JsonSchemaInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace Knp\JsonSchema;
66

77
use JsonSerializable;
8+
use Knp\JsonSchema\Validator\Errors;
89

910
/**
10-
* @template T of mixed
11+
* @template T
1112
*/
1213
interface JsonSchemaInterface extends JsonSerializable
1314
{

src/ObjectSchema.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
namespace Knp\JsonSchema;
66

77
/**
8-
* @template T of array<string, mixed>
9-
* @extends JsonSchema<T>
8+
* @template T
9+
*
10+
* @implements JsonSchemaInterface<T>
1011
*/
1112
abstract class ObjectSchema implements JsonSchemaInterface
1213
{
1314
/**
14-
* @var array<string, JsonSchema<mixed>>
15+
* @var array<string, JsonSchemaInterface<mixed>>
1516
*/
1617
private array $properties = [];
1718

@@ -44,11 +45,11 @@ protected function hasAdditionalProperties(): bool
4445
}
4546

4647
/**
47-
* @template S
48+
* @psalm-template S
4849
*
49-
* @param JsonSchema<S> $schema
50+
* @psalm-param JsonSchemaInterface<S> $schema
5051
*/
51-
protected function addProperty(string $name, JsonSchema $schema, bool $required = true): void
52+
protected function addProperty(string $name, JsonSchemaInterface $schema, bool $required = true): void
5253
{
5354
$this->properties[$name] = $schema;
5455

src/Scalar/UuidSchema.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
namespace Knp\JsonSchema\Scalar;
66

77
use Knp\JsonSchema\JsonSchema;
8+
use Knp\JsonSchema\JsonSchemaInterface;
89

910
/**
10-
* @extends JsonSchema<string>
11+
* @implements JsonSchemaInterface<string>
1112
*/
12-
final class UuidSchema extends JsonSchema
13+
final class UuidSchema implements JsonSchemaInterface
1314
{
1415
private const PATTERN = '^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$';
1516

@@ -37,12 +38,32 @@ public function getDescription(): string
3738
public function getSchema(): array
3839
{
3940
return array_merge(
40-
self::string(),
41+
JsonSchema::string(),
4142
[
4243
'pattern' => self::PATTERN,
4344
'minLength' => 36,
4445
'maxLength' => 36,
4546
]
4647
);
4748
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function jsonSerialize(): array
54+
{
55+
$schema = $this->getSchema();
56+
57+
/**
58+
* @var array<string, mixed>&array{title: string, description: string, examples: array<T>}
59+
*/
60+
return array_merge(
61+
$schema,
62+
[
63+
'title' => $this->getTitle(),
64+
'description' => $this->getDescription(),
65+
'examples' => [...$this->getExamples()],
66+
],
67+
);
68+
}
4869
}

src/Validator.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
interface Validator
1010
{
1111
/**
12-
* @template T
12+
* @template T of array<array-key, mixed>
1313
*
14-
* @param T $data
15-
* @param class-string<JsonSchema<T>>|JsonSchema<T> $schema
14+
* @param array<array-key, mixed> $data
15+
* @param JsonSchemaInterface<T> $schema
1616
*/
17-
public function validate($data, $schema): ?Errors;
17+
public function validate(array $data, JsonSchemaInterface $schema): ?Errors;
1818
}

0 commit comments

Comments
 (0)