diff --git a/src/CollectionSchema.php b/src/CollectionSchema.php index 84ee67f..f6340bc 100644 --- a/src/CollectionSchema.php +++ b/src/CollectionSchema.php @@ -7,16 +7,16 @@ /** * @template I * - * @phpstan-type CollectionSchemaData array + * @type CollectionSchemaData array * - * @extends JsonSchema + * @implements JsonSchemaInterface */ abstract class CollectionSchema implements JsonSchemaInterface { /** - * @param JsonSchema $itemSchema + * @psalm-param JsonSchemaInterface $itemSchema */ - public function __construct(private JsonSchema $itemSchema) + public function __construct(private JsonSchemaInterface $itemSchema) { } diff --git a/src/EnumSchema.php b/src/EnumSchema.php index 67a5449..a310aff 100644 --- a/src/EnumSchema.php +++ b/src/EnumSchema.php @@ -6,7 +6,8 @@ /** * @template E - * @extends JsonSchema + * + * @implements JsonSchemaInterface */ abstract class EnumSchema implements JsonSchemaInterface { diff --git a/src/JsonSchema.php b/src/JsonSchema.php index 550b7a1..e4361d5 100644 --- a/src/JsonSchema.php +++ b/src/JsonSchema.php @@ -4,6 +4,8 @@ namespace Knp\JsonSchema; +use Knp\JsonSchema\Validator\Errors; + /** * @template T of mixed * @@ -116,7 +118,7 @@ public function jsonSerialize(): array * * @return array */ - protected static function constant($value): array + public static function constant($value): array { return [ 'const' => $value, @@ -126,7 +128,7 @@ protected static function constant($value): array /** * @return array */ - protected static function null(): array + public static function null(): array { return [ 'type' => 'null', @@ -136,7 +138,7 @@ protected static function null(): array /** * @return array */ - protected static function text(): array + public static function text(): array { return [ 'type' => 'string', @@ -147,7 +149,7 @@ protected static function text(): array /** * @return array */ - protected static function boolean(): array + public static function boolean(): array { return [ 'type' => 'boolean', @@ -157,7 +159,7 @@ protected static function boolean(): array /** * @return array */ - protected static function string(?string $format = null): array + public static function string(?string $format = null): array { $result = [ ...self::text(), @@ -174,7 +176,7 @@ protected static function string(?string $format = null): array /** * @return array */ - protected static function integer(): array + public static function integer(): array { return [ 'type' => 'integer', @@ -184,7 +186,7 @@ protected static function integer(): array /** * @return array */ - protected static function number(): array + public static function number(): array { return [ 'type' => 'number', @@ -194,7 +196,7 @@ protected static function number(): array /** * @return array */ - protected static function date(): array + public static function date(): array { return [ 'type' => 'string', @@ -205,7 +207,7 @@ protected static function date(): array /** * @return array */ - protected static function positiveInteger(): array + public static function positiveInteger(): array { return [ ...self::integer(), @@ -218,7 +220,7 @@ protected static function positiveInteger(): array * * @return array{oneOf: array>} */ - protected static function oneOf(...$schemas): array + public static function oneOf(...$schemas): array { return [ 'oneOf' => $schemas, diff --git a/src/JsonSchemaInterface.php b/src/JsonSchemaInterface.php index 87805de..039dc34 100644 --- a/src/JsonSchemaInterface.php +++ b/src/JsonSchemaInterface.php @@ -5,9 +5,10 @@ namespace Knp\JsonSchema; use JsonSerializable; +use Knp\JsonSchema\Validator\Errors; /** - * @template T of mixed + * @template T */ interface JsonSchemaInterface extends JsonSerializable { diff --git a/src/ObjectSchema.php b/src/ObjectSchema.php index d419391..979b76c 100644 --- a/src/ObjectSchema.php +++ b/src/ObjectSchema.php @@ -5,13 +5,14 @@ namespace Knp\JsonSchema; /** - * @template T of array - * @extends JsonSchema + * @template T + * + * @implements JsonSchemaInterface */ abstract class ObjectSchema implements JsonSchemaInterface { /** - * @var array> + * @var array> */ private array $properties = []; @@ -44,11 +45,11 @@ protected function hasAdditionalProperties(): bool } /** - * @template S + * @psalm-template S * - * @param JsonSchema $schema + * @psalm-param JsonSchemaInterface $schema */ - protected function addProperty(string $name, JsonSchema $schema, bool $required = true): void + protected function addProperty(string $name, JsonSchemaInterface $schema, bool $required = true): void { $this->properties[$name] = $schema; diff --git a/src/Scalar/UuidSchema.php b/src/Scalar/UuidSchema.php index 5aeceb0..27c02c8 100644 --- a/src/Scalar/UuidSchema.php +++ b/src/Scalar/UuidSchema.php @@ -5,11 +5,12 @@ namespace Knp\JsonSchema\Scalar; use Knp\JsonSchema\JsonSchema; +use Knp\JsonSchema\JsonSchemaInterface; /** - * @extends JsonSchema + * @implements JsonSchemaInterface */ -final class UuidSchema extends JsonSchema +final class UuidSchema implements JsonSchemaInterface { private const PATTERN = '^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$'; @@ -37,7 +38,7 @@ public function getDescription(): string public function getSchema(): array { return array_merge( - self::string(), + JsonSchema::string(), [ 'pattern' => self::PATTERN, 'minLength' => 36, @@ -45,4 +46,24 @@ public function getSchema(): array ] ); } + + /** + * {@inheritdoc} + */ + public function jsonSerialize(): array + { + $schema = $this->getSchema(); + + /** + * @var array&array{title: string, description: string, examples: array} + */ + return array_merge( + $schema, + [ + 'title' => $this->getTitle(), + 'description' => $this->getDescription(), + 'examples' => [...$this->getExamples()], + ], + ); + } } diff --git a/src/Validator.php b/src/Validator.php index 8d98156..6f364df 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -9,10 +9,10 @@ interface Validator { /** - * @template T + * @template T of array * - * @param T $data - * @param class-string>|JsonSchema $schema + * @param array $data + * @param JsonSchemaInterface $schema */ - public function validate($data, $schema): ?Errors; + public function validate(array $data, JsonSchemaInterface $schema): ?Errors; }