Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Lelaisant committed Sep 27, 2022
0 parents commit 3b94697
Show file tree
Hide file tree
Showing 11 changed files with 619 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "knplabs/json-schema",
"description": "PHP json-schema implementation",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Knplabs\\JsonSchema\\": "src/"
}
},
"authors": [
{
"name": "KnpLabs",
"email": "hello@knplabs.com"
}
],
"require": {
"php": ">=8.0"
}
}
35 changes: 35 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace KnpLabs\JsonSchema;

use Exception;

final class Collection
{
/**
* @param iterable<JsonSchema<mixed>> $schemas
*/
public function __construct(private iterable $schemas)
{
}

/**
* @template J of JsonSchema
*
* @param class-string<J> $schemaClassName
*
* @return J
*/
public function get(string $schemaClassName): JsonSchema
{
foreach ($this->schemas as $schema) {
if (is_a($schema, $schemaClassName)) {
return $schema;
}
}

throw new Exception("Schema {$schemaClassName} not found.");
}
}
74 changes: 74 additions & 0 deletions src/CollectionSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace KnpLabs\JsonSchema;

/**
* @template I
*
* @phpstan-type CollectionSchemaData array<I>
*
* @extends JsonSchema<CollectionSchemaData>
*/
abstract class CollectionSchema extends JsonSchema
{
/**
* @param JsonSchema<I> $itemSchema
*/
public function __construct(private JsonSchema $itemSchema)
{
}

public function getExamples(): iterable
{
yield [...$this->itemSchema->getExamples()];
}

public function getTitle(): string
{
return sprintf('Collection<%s>', $this->itemSchema->getTitle());
}

protected function getUniqueItems(): ?bool
{
return null;
}

protected function getMinLength(): ?int
{
return null;
}

protected function getMaxLength(): ?int
{
return null;
}

protected function getRange(): ?int
{
return null;
}

protected function getSchema(): array
{
$schema = [
'type' => 'array',
'items' => $this->itemSchema->jsonSerialize(),
];

if (null !== $uniqueItems = $this->getUniqueItems()) {
$schema['uniqueItems'] = $uniqueItems;
}

if (null !== $minLength = $this->getMinLength()) {
$schema['minLength'] = $minLength;
}

if (null !== $maxLength = $this->getMaxLength()) {
$schema['maxLength'] = $maxLength;
}

return $schema;
}
}
29 changes: 29 additions & 0 deletions src/EnumSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace KnpLabs\JsonSchema;

/**
* @template E
* @extends JsonSchema<E>
*/
abstract class EnumSchema extends JsonSchema
{
public function getExamples(): iterable
{
return $this->getEnum();
}

protected function getSchema(): array
{
return [
'enum' => [...$this->getEnum()],
];
}

/**
* @return iterable<int, E>
*/
abstract protected function getEnum(): iterable;
}
Loading

0 comments on commit 3b94697

Please sign in to comment.