Skip to content

Commit 3b94697

Browse files
author
Antoine Lelaisant
committed
feat: initial commit
0 parents  commit 3b94697

File tree

11 files changed

+619
-0
lines changed

11 files changed

+619
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "knplabs/json-schema",
3+
"description": "PHP json-schema implementation",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Knplabs\\JsonSchema\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "KnpLabs",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"require": {
18+
"php": ">=8.0"
19+
}
20+
}

src/Collection.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KnpLabs\JsonSchema;
6+
7+
use Exception;
8+
9+
final class Collection
10+
{
11+
/**
12+
* @param iterable<JsonSchema<mixed>> $schemas
13+
*/
14+
public function __construct(private iterable $schemas)
15+
{
16+
}
17+
18+
/**
19+
* @template J of JsonSchema
20+
*
21+
* @param class-string<J> $schemaClassName
22+
*
23+
* @return J
24+
*/
25+
public function get(string $schemaClassName): JsonSchema
26+
{
27+
foreach ($this->schemas as $schema) {
28+
if (is_a($schema, $schemaClassName)) {
29+
return $schema;
30+
}
31+
}
32+
33+
throw new Exception("Schema {$schemaClassName} not found.");
34+
}
35+
}

src/CollectionSchema.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KnpLabs\JsonSchema;
6+
7+
/**
8+
* @template I
9+
*
10+
* @phpstan-type CollectionSchemaData array<I>
11+
*
12+
* @extends JsonSchema<CollectionSchemaData>
13+
*/
14+
abstract class CollectionSchema extends JsonSchema
15+
{
16+
/**
17+
* @param JsonSchema<I> $itemSchema
18+
*/
19+
public function __construct(private JsonSchema $itemSchema)
20+
{
21+
}
22+
23+
public function getExamples(): iterable
24+
{
25+
yield [...$this->itemSchema->getExamples()];
26+
}
27+
28+
public function getTitle(): string
29+
{
30+
return sprintf('Collection<%s>', $this->itemSchema->getTitle());
31+
}
32+
33+
protected function getUniqueItems(): ?bool
34+
{
35+
return null;
36+
}
37+
38+
protected function getMinLength(): ?int
39+
{
40+
return null;
41+
}
42+
43+
protected function getMaxLength(): ?int
44+
{
45+
return null;
46+
}
47+
48+
protected function getRange(): ?int
49+
{
50+
return null;
51+
}
52+
53+
protected function getSchema(): array
54+
{
55+
$schema = [
56+
'type' => 'array',
57+
'items' => $this->itemSchema->jsonSerialize(),
58+
];
59+
60+
if (null !== $uniqueItems = $this->getUniqueItems()) {
61+
$schema['uniqueItems'] = $uniqueItems;
62+
}
63+
64+
if (null !== $minLength = $this->getMinLength()) {
65+
$schema['minLength'] = $minLength;
66+
}
67+
68+
if (null !== $maxLength = $this->getMaxLength()) {
69+
$schema['maxLength'] = $maxLength;
70+
}
71+
72+
return $schema;
73+
}
74+
}

src/EnumSchema.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KnpLabs\JsonSchema;
6+
7+
/**
8+
* @template E
9+
* @extends JsonSchema<E>
10+
*/
11+
abstract class EnumSchema extends JsonSchema
12+
{
13+
public function getExamples(): iterable
14+
{
15+
return $this->getEnum();
16+
}
17+
18+
protected function getSchema(): array
19+
{
20+
return [
21+
'enum' => [...$this->getEnum()],
22+
];
23+
}
24+
25+
/**
26+
* @return iterable<int, E>
27+
*/
28+
abstract protected function getEnum(): iterable;
29+
}

0 commit comments

Comments
 (0)