-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Antoine Lelaisant
committed
Sep 27, 2022
0 parents
commit 3b94697
Showing
11 changed files
with
619 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.