Phase 0: Fundament für Inhaus
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s

- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3)
- Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung
- Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü
- Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person
- Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“
- Benutzerverwaltung im Admin, Zugang vorläufig über is_admin
- vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/
- PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung
- VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-25 20:35:30 +00:00
parent 7197eaebf2
commit 9c0f8de791
15771 changed files with 1564103 additions and 5 deletions

View file

@ -0,0 +1,633 @@
<?php
namespace Illuminate\JsonSchema;
use InvalidArgumentException;
class Deserializer
{
/**
* The maximum number of schema fragments that may be expanded.
*/
protected const MAX_NODES = 20000;
/**
* The root schema being deserialized (used to resolve local $refs).
*
* @var array<string, mixed>
*/
protected array $root;
/**
* The number of schema fragments expanded so far.
*/
protected int $nodes = 0;
/**
* The cache of resolved local "$ref" targets, keyed by reference.
*
* @var array<string, array<string, mixed>>
*/
protected array $refCache = [];
/**
* Create a new deserializer instance.
*
* @param array<string, mixed> $root
*/
protected function __construct(array $root)
{
$this->root = $root;
}
/**
* Deserialize the Laravel-supported JSON Schema subset into a type.
*
* @param array<string, mixed> $schema
*
* @throws \InvalidArgumentException
*/
public static function deserialize(array $schema): Types\Type
{
return (new static($schema))->build($schema);
}
/**
* Build a type from the given schema fragment.
*
* @param array<string, mixed> $schema
* @param array<int, string> $refs
*
* @throws \InvalidArgumentException
*/
protected function build(array $schema, array $refs = []): Types\Type
{
if (++$this->nodes > static::MAX_NODES) {
throw new InvalidArgumentException(
'The JSON Schema is too large to deserialize; it expands beyond ['.static::MAX_NODES.'] fragments.'
);
}
[$schema, $refs] = $this->resolveRef($schema, $refs);
if (($type = $this->buildAnyOfComposition($schema, $refs)) !== null) {
$this->applyCommon($type, $schema);
return $type;
}
[$schema, $nullableFromUnion, $refs] = $this->normalizeUnions($schema, $refs);
[$name, $nullableFromType] = $this->resolveType($schema);
if (is_array($name)) {
$this->ensureUnionConstraintsAreSupported($schema);
$type = new Types\UnionType($name);
} else {
$type = match ($name) {
'object' => $this->buildObject($schema, $refs),
'array' => $this->buildArray($schema, $refs),
'string' => $this->buildString($schema),
'integer' => $this->buildInteger($schema),
'number' => $this->buildNumber($schema),
'boolean' => new Types\BooleanType,
default => throw new InvalidArgumentException("Unsupported JSON Schema type [{$name}]."),
};
}
$this->applyCommon($type, $schema);
if ($nullableFromUnion || $nullableFromType) {
$type->nullable();
}
return $type;
}
/**
* Build an anyOf composition unless it is the existing nullable single-schema form.
*
* @param array<string, mixed> $schema
* @param array<int, string> $refs
*
* @throws \InvalidArgumentException
*/
protected function buildAnyOfComposition(array $schema, array $refs = []): ?Types\AnyOfType
{
if (! isset($schema['anyOf']) || ! is_array($schema['anyOf'])) {
return null;
}
$nullable = false;
$branches = [];
foreach ($schema['anyOf'] as $branch) {
if (! is_array($branch)) {
throw new InvalidArgumentException('Unable to represent the schema for an anyOf branch; boolean schemas are not supported.');
}
[$branch, $branchRefs] = $this->resolveRef($branch, $refs);
if ($this->isNullBranch($branch)) {
$nullable = true;
} else {
$branches[] = [$branch, $branchRefs];
}
}
if ($nullable && count($branches) === 1) {
return null;
}
$type = new Types\AnyOfType(array_map(
fn (array $branch) => $this->build($branch[0], $branch[1]),
$branches,
));
if ($nullable) {
$type->nullable();
}
return $type;
}
/**
* Build an object type from the given schema fragment.
*
* @param array<string, mixed> $schema
* @param array<int, string> $refs
*
* @throws \InvalidArgumentException
*/
protected function buildObject(array $schema, array $refs = []): Types\ObjectType
{
$properties = [];
if (isset($schema['properties']) && is_array($schema['properties'])) {
$required = is_array($schema['required'] ?? null)
? array_flip(array_map('strval', $schema['required']))
: [];
foreach ($schema['properties'] as $key => $definition) {
if (! is_array($definition)) {
throw new InvalidArgumentException(
"Unable to represent the schema for property [{$key}]; boolean schemas are not supported."
);
}
$property = $this->build($definition, $refs);
if (isset($required[(string) $key])) {
$property->required();
}
$properties[$key] = $property;
}
}
$type = new Types\ObjectType($properties);
if (($schema['additionalProperties'] ?? null) === false) {
$type->withoutAdditionalProperties();
}
return $type;
}
/**
* Build an array type from the given schema fragment.
*
* @param array<string, mixed> $schema
* @param array<int, string> $refs
*
* @throws \InvalidArgumentException
*/
protected function buildArray(array $schema, array $refs = []): Types\ArrayType
{
$type = new Types\ArrayType;
if (isset($schema['items']) && $schema['items'] !== []) {
if (! is_array($schema['items']) || array_is_list($schema['items'])) {
throw new InvalidArgumentException('Tuple and boolean JSON Schema "items" are not supported.');
}
$type->items($this->build($schema['items'], $refs));
}
if (isset($schema['minItems'])) {
$type->min((int) $schema['minItems']);
}
if (isset($schema['maxItems'])) {
$type->max((int) $schema['maxItems']);
}
if (isset($schema['uniqueItems'])) {
$type->unique((bool) $schema['uniqueItems']);
}
return $type;
}
/**
* Build a string type from the given schema fragment.
*
* @param array<string, mixed> $schema
*/
protected function buildString(array $schema): Types\StringType
{
$type = new Types\StringType;
if (isset($schema['minLength'])) {
$type->min((int) $schema['minLength']);
}
if (isset($schema['maxLength'])) {
$type->max((int) $schema['maxLength']);
}
if (isset($schema['pattern'])) {
$type->pattern((string) $schema['pattern']);
}
if (isset($schema['format'])) {
$type->format((string) $schema['format']);
}
return $type;
}
/**
* Build an integer type from the given schema fragment.
*
* @param array<string, mixed> $schema
*/
protected function buildInteger(array $schema): Types\IntegerType
{
return $this->applyNumericBounds(new Types\IntegerType, $schema, $this->toInteger(...));
}
/**
* Build a number type from the given schema fragment.
*
* @param array<string, mixed> $schema
*/
protected function buildNumber(array $schema): Types\NumberType
{
return $this->applyNumericBounds(new Types\NumberType, $schema);
}
/**
* Apply the numeric bound keywords to the given integer or number type.
*
* @template TType of Types\IntegerType|Types\NumberType
*
* @param TType $type
* @param array<string, mixed> $schema
* @param (callable(int|float): (int|float))|null $cast
* @return TType
*
* @throws \InvalidArgumentException
*/
protected function applyNumericBounds(Types\IntegerType|Types\NumberType $type, array $schema, ?callable $cast = null)
{
$cast ??= static fn (int|float $value) => $value;
foreach (['minimum' => 'min', 'maximum' => 'max', 'multipleOf' => 'multipleOf'] as $keyword => $method) {
if (! isset($schema[$keyword])) {
continue;
}
if (($value = $this->toNumber($schema[$keyword])) === null) {
throw new InvalidArgumentException("The JSON Schema [{$keyword}] constraint must be a number.");
}
$type->{$method}($cast($value));
}
return $type;
}
/**
* Apply the keywords shared by every type to the given instance.
*
* @param array<string, mixed> $schema
*
* @throws \InvalidArgumentException
*/
protected function applyCommon(Types\Type $type, array $schema): void
{
if (isset($schema['title'])) {
$type->title((string) $schema['title']);
}
if (isset($schema['description'])) {
$type->description((string) $schema['description']);
}
if (isset($schema['enum']) && is_array($schema['enum'])) {
$type->enum($schema['enum']);
}
if (array_key_exists('default', $schema)) {
if ($schema['default'] === null) {
throw new InvalidArgumentException('A null JSON Schema [default] is not supported.');
}
// The "default" setter is typed per concrete type, so assign it directly...
(fn () => $this->default = $schema['default'])->call($type);
}
}
/**
* Resolve the base type name and whether the schema is nullable.
*
* @param array<string, mixed> $schema
* @return array{0: string|array<int, string>, 1: bool}
*
* @throws \InvalidArgumentException
*/
protected function resolveType(array $schema): array
{
$type = $schema['type'] ?? null;
$nullable = false;
if (is_array($type)) {
$nullable = in_array('null', $type, true);
$names = array_values(array_unique(array_map('strval', array_filter(
$type,
static fn ($value) => $value !== 'null',
))));
if (count($names) > 1) {
return [$names, $nullable];
}
$type = $names[0] ?? null;
}
$type ??= $this->inferType($schema);
if (! is_string($type)) {
throw new InvalidArgumentException('Unable to determine the JSON Schema type for the given schema.');
}
return [$type, $nullable];
}
/**
* Infer the type name when "type" is absent but the shape is unambiguous.
*
* @param array<string, mixed> $schema
*/
protected function inferType(array $schema): ?string
{
return match (true) {
isset($schema['properties']), isset($schema['additionalProperties']), isset($schema['required']) => 'object',
isset($schema['items']), isset($schema['minItems']), isset($schema['maxItems']), isset($schema['uniqueItems']) => 'array',
isset($schema['enum']) && is_array($schema['enum']) => $this->inferEnumType($schema['enum']),
isset($schema['minLength']), isset($schema['maxLength']), isset($schema['pattern']), isset($schema['format']) => 'string',
isset($schema['minimum']), isset($schema['maximum']), isset($schema['multipleOf']) => 'number',
default => null,
};
}
/**
* Infer the scalar type shared by a homogeneous enum of scalars.
*
* @param array<int, mixed> $enum
*/
protected function inferEnumType(array $enum): ?string
{
$resolved = null;
foreach ($enum as $value) {
$current = match (true) {
is_bool($value) => 'boolean',
is_int($value) => 'integer',
is_float($value) => 'number',
is_string($value) => 'string',
default => null,
};
if ($current === null) {
return null;
}
if ($resolved === null || $resolved === $current) {
$resolved = $current;
continue;
}
// A mix of integers and floats is still numeric; anything else is ambiguous...
if (in_array($resolved, ['integer', 'number'], true) && in_array($current, ['integer', 'number'], true)) {
$resolved = 'number';
continue;
}
return null;
}
return $resolved;
}
/**
* Ensure a multi-type union carries no type-specific constraint keywords.
*
* @param array<string, mixed> $schema
*
* @throws \InvalidArgumentException
*/
protected function ensureUnionConstraintsAreSupported(array $schema): void
{
$keywords = [
'minLength', 'maxLength', 'pattern', 'format',
'minimum', 'maximum', 'multipleOf',
'items', 'minItems', 'maxItems', 'uniqueItems',
'properties', 'required', 'additionalProperties',
];
$unsupported = array_values(array_intersect($keywords, array_keys($schema)));
if ($unsupported !== []) {
throw new InvalidArgumentException(
'Type-specific keywords ['.implode(', ', $unsupported).'] are not supported on a multi-type JSON Schema union.'
);
}
}
/**
* Collapse "anyOf" / "oneOf" null branches into a single effective schema.
*
* @param array<string, mixed> $schema
* @param array<int, string> $refs
* @return array{0: array<string, mixed>, 1: bool, 2: array<int, string>}
*
* @throws \InvalidArgumentException
*/
protected function normalizeUnions(array $schema, array $refs = []): array
{
foreach (['anyOf', 'oneOf'] as $key) {
if (! isset($schema[$key]) || ! is_array($schema[$key])) {
continue;
}
$nullable = false;
$branches = [];
foreach ($schema[$key] as $branch) {
if (! is_array($branch)) {
continue;
}
[$branch, $branchRefs] = $this->resolveRef($branch, $refs);
if ($this->isNullBranch($branch)) {
$nullable = true;
} else {
$branches[] = [$branch, $branchRefs];
}
}
if (! $nullable || count($branches) !== 1) {
throw new InvalidArgumentException(
"Only a nullable \"{$key}\" (a single schema plus a \"null\" branch) is supported."
);
}
[$branch, $branchRefs] = $branches[0];
$siblings = $schema;
unset($siblings[$key]);
foreach ($siblings as $siblingKey => $value) {
if (array_key_exists($siblingKey, $branch) && $branch[$siblingKey] !== $value) {
throw new InvalidArgumentException(
"Conflicting [{$siblingKey}] between a \"{$key}\" branch and its sibling keys."
);
}
}
return [array_merge($siblings, $branch), true, $branchRefs];
}
return [$schema, false, $refs];
}
/**
* Determine if the given schema branch describes only the "null" type.
*
* @param array<string, mixed> $branch
*/
protected function isNullBranch(array $branch): bool
{
$type = $branch['type'] ?? null;
return $type === 'null' || $type === ['null'];
}
/**
* Resolve a local "$ref" against the root schema, merging sibling keys.
*
* @param array<string, mixed> $schema
* @param array<int, string> $refs
* @return array{0: array<string, mixed>, 1: array<int, string>}
*
* @throws \InvalidArgumentException
*/
protected function resolveRef(array $schema, array $refs = []): array
{
if (! isset($schema['$ref']) || ! is_string($schema['$ref'])) {
return [$schema, $refs];
}
$ref = $schema['$ref'];
if (in_array($ref, $refs, true)) {
throw new InvalidArgumentException("Circular JSON Schema \$ref [{$ref}] detected.");
}
$refs[] = $ref;
$resolved = $this->lookupRef($ref);
$siblings = $schema;
unset($siblings['$ref']);
return $this->resolveRef(array_merge($resolved, $siblings), $refs);
}
/**
* Look up a local JSON pointer reference within the root schema.
*
* @return array<string, mixed>
*
* @throws \InvalidArgumentException
*/
protected function lookupRef(string $ref): array
{
if (isset($this->refCache[$ref])) {
return $this->refCache[$ref];
}
if ($ref === '#') {
return $this->refCache[$ref] = $this->root;
}
if (! str_starts_with($ref, '#/')) {
throw new InvalidArgumentException("Unable to resolve non-local JSON Schema \$ref [{$ref}].");
}
$target = $this->root;
foreach (explode('/', substr($ref, 2)) as $segment) {
$segment = str_replace(['~1', '~0'], ['/', '~'], rawurldecode($segment));
if (! is_array($target) || ! array_key_exists($segment, $target)) {
throw new InvalidArgumentException("Unable to resolve JSON Schema \$ref [{$ref}].");
}
$target = $target[$segment];
}
if (! is_array($target)) {
throw new InvalidArgumentException("The JSON Schema \$ref [{$ref}] does not point to a schema.");
}
return $this->refCache[$ref] = $target;
}
/**
* Normalize the given value to an integer or float, or null when not numeric.
*/
protected function toNumber(mixed $value): int|float|null
{
if (is_int($value) || is_float($value)) {
return $value;
}
if (is_string($value) && is_numeric($value)) {
return $value + 0;
}
return null;
}
/**
* Cast the given number to an integer, rejecting non-integer values.
*
* @throws \InvalidArgumentException
*/
protected function toInteger(int|float $value): int
{
if (is_float($value) && floor($value) !== $value) {
throw new InvalidArgumentException("The JSON Schema integer constraint [{$value}] must be an integer.");
}
return (int) $value;
}
}

View file

@ -0,0 +1,39 @@
<?php
namespace Illuminate\JsonSchema;
use Closure;
use Illuminate\JsonSchema\Types\Type;
/**
* @method static Types\ObjectType object(Closure|array<string, Types\Type> $properties = [])
* @method static Types\AnyOfType anyOf(Closure|array<int, Types\Type> $schemas)
* @method static Types\IntegerType integer()
* @method static Types\NumberType number()
* @method static Types\StringType string()
* @method static Types\BooleanType boolean()
* @method static Types\ArrayType array()
* @method static Types\UnionType union(array<int, string> $types)
*/
class JsonSchema
{
/**
* Build a type from a raw array of the Laravel-supported JSON Schema subset.
*
* @param array<string, mixed> $schema
*
* @throws \InvalidArgumentException
*/
public static function fromArray(array $schema): Type
{
return Deserializer::deserialize($schema);
}
/**
* Dynamically pass static methods to the schema instance.
*/
public static function __callStatic(string $name, mixed $arguments): Type
{
return (new JsonSchemaTypeFactory)->$name(...$arguments);
}
}

View file

@ -0,0 +1,87 @@
<?php
namespace Illuminate\JsonSchema;
use Closure;
use Illuminate\Contracts\JsonSchema\JsonSchema as JsonSchemaContract;
class JsonSchemaTypeFactory extends JsonSchema implements JsonSchemaContract
{
/**
* Create a new object schema instance.
*
* @param (Closure(JsonSchemaTypeFactory): array<string, Types\Type>)|array<string, Types\Type> $properties
*/
public function object(Closure|array $properties = []): Types\ObjectType
{
if ($properties instanceof Closure) {
$properties = $properties($this);
}
return new Types\ObjectType($properties);
}
/**
* Create a new array property instance.
*/
public function array(): Types\ArrayType
{
return new Types\ArrayType;
}
/**
* Create a new string property instance.
*/
public function string(): Types\StringType
{
return new Types\StringType;
}
/**
* Create a new integer property instance.
*/
public function integer(): Types\IntegerType
{
return new Types\IntegerType;
}
/**
* Create a new number property instance.
*/
public function number(): Types\NumberType
{
return new Types\NumberType;
}
/**
* Create a new boolean property instance.
*/
public function boolean(): Types\BooleanType
{
return new Types\BooleanType;
}
/**
* Create a new multi-type union instance.
*
* @param array<int, string> $types
*/
public function union(array $types): Types\UnionType
{
return new Types\UnionType($types);
}
/**
* Create a new anyOf schema instance.
*
* @param (Closure(JsonSchemaTypeFactory): array<int, Types\Type>)|array<int, Types\Type> $schemas
*/
public function anyOf(Closure|array $schemas): Types\AnyOfType
{
if ($schemas instanceof Closure) {
$schemas = $schemas($this);
}
return new Types\AnyOfType($schemas);
}
}

View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -0,0 +1,129 @@
<?php
namespace Illuminate\JsonSchema;
use RuntimeException;
class Serializer
{
/**
* The properties to ignore when serializing.
*
* @var array<int, string>
*/
protected static array $ignore = ['required', 'nullable'];
/**
* Serialize the given property to an array.
*
* @return array<string, mixed>
*
* @throws \RuntimeException
*/
public static function serialize(Types\Type $type): array
{
/** @var array<string, mixed> $attributes */
$attributes = (fn () => get_object_vars($type))->call($type);
if ($type instanceof Types\AnyOfType) {
$attributes['anyOf'] = array_map(
static fn (Types\Type $schema) => static::serialize($schema),
$attributes['schemas'],
);
unset($attributes['schemas']);
if (static::isNullable($type)) {
$attributes['anyOf'][] = ['type' => 'null'];
}
return array_filter($attributes, static function (mixed $value, string $key) {
if (in_array($key, static::$ignore, true)) {
return false;
}
return $value !== null;
}, ARRAY_FILTER_USE_BOTH);
}
$attributes['type'] = match (get_class($type)) {
Types\ArrayType::class => 'array',
Types\BooleanType::class => 'boolean',
Types\IntegerType::class => 'integer',
Types\NumberType::class => 'number',
Types\ObjectType::class => 'object',
Types\StringType::class => 'string',
Types\UnionType::class => $attributes['types'],
default => throw new RuntimeException('Unsupported ['.get_class($type).'] type.'),
};
unset($attributes['types']);
$nullable = static::isNullable($type);
if ($nullable) {
$attributes['type'] = is_array($attributes['type'])
? [...$attributes['type'], 'null']
: [$attributes['type'], 'null'];
}
$attributes = array_filter($attributes, static function (mixed $value, string $key) {
if (in_array($key, static::$ignore, true)) {
return false;
}
return $value !== null;
}, ARRAY_FILTER_USE_BOTH);
if ($type instanceof Types\ObjectType) {
if (count($attributes['properties']) === 0) {
unset($attributes['properties']);
} else {
$required = array_map(
'strval',
array_keys(array_filter(
$attributes['properties'],
static fn (Types\Type $property) => static::isRequired($property),
))
);
if ($required !== []) {
$attributes['required'] = $required;
}
$attributes['properties'] = array_map(
static fn (Types\Type $property) => static::serialize($property),
$attributes['properties'],
);
}
}
if ($type instanceof Types\ArrayType) {
if (isset($attributes['items']) && $attributes['items'] instanceof Types\Type) {
$attributes['items'] = static::serialize($attributes['items']);
}
}
return $attributes;
}
/**
* Determine if the given type is required.
*/
protected static function isRequired(Types\Type $type): bool
{
$attributes = (fn () => get_object_vars($type))->call($type);
return isset($attributes['required']) && $attributes['required'] === true;
}
/**
* Determine if the given type is nullable.
*/
protected static function isNullable(Types\Type $type): bool
{
$attributes = (fn () => get_object_vars($type))->call($type);
return isset($attributes['nullable']) && $attributes['nullable'] === true;
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Illuminate\JsonSchema\Types;
class AnyOfType extends Type
{
/**
* Create a new anyOf type instance.
*
* @param array<int, Type> $schemas
*/
public function __construct(protected array $schemas)
{
$this->schemas = array_values($schemas);
}
/**
* Get the anyOf schemas.
*
* @return array<int, Type>
*/
public function schemas(): array
{
return $this->schemas;
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace Illuminate\JsonSchema\Types;
class ArrayType extends Type
{
/**
* The minimum number of items (inclusive).
*/
protected ?int $minItems = null;
/**
* The maximum number of items (inclusive).
*/
protected ?int $maxItems = null;
/**
* The schema of the items contained in the array.
*/
protected ?Type $items = null;
/**
* Whether the array items must be unique.
*/
protected ?bool $uniqueItems = null;
/**
* Set the minimum number of items (inclusive).
*/
public function min(int $value): static
{
$this->minItems = $value;
return $this;
}
/**
* Set the maximum number of items (inclusive).
*/
public function max(int $value): static
{
$this->maxItems = $value;
return $this;
}
/**
* Set the schema for array items.
*/
public function items(Type $type): static
{
$this->items = $type;
return $this;
}
/**
* Indicate that the array items must be unique.
*/
public function unique(bool $unique = true): static
{
$this->uniqueItems = $unique ?: null;
return $this;
}
/**
* Set the type's default value.
*
* @param array<int, mixed> $value
*/
public function default(array $value): static
{
$this->default = $value;
return $this;
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace Illuminate\JsonSchema\Types;
class BooleanType extends Type
{
/**
* Set the type's default value.
*/
public function default(bool $value): static
{
$this->default = $value;
return $this;
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Illuminate\JsonSchema\Types;
class IntegerType extends Type
{
/**
* The minimum value (inclusive).
*/
protected ?int $minimum = null;
/**
* The maximum value (inclusive).
*/
protected ?int $maximum = null;
/**
* The number the value must be a multiple of.
*/
protected ?int $multipleOf = null;
/**
* Set the minimum value (inclusive).
*/
public function min(int $value): static
{
$this->minimum = $value;
return $this;
}
/**
* Set the maximum value (inclusive).
*/
public function max(int $value): static
{
$this->maximum = $value;
return $this;
}
/**
* Set the number the value must be a multiple of.
*/
public function multipleOf(int $value): static
{
$this->multipleOf = $value;
return $this;
}
/**
* Set the type's default value.
*/
public function default(int $value): static
{
$this->default = $value;
return $this;
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Illuminate\JsonSchema\Types;
class NumberType extends Type
{
/**
* The minimum value (inclusive).
*/
protected int|float|null $minimum = null;
/**
* The maximum value (inclusive).
*/
protected int|float|null $maximum = null;
/**
* The number the value must be a multiple of.
*/
protected int|float|null $multipleOf = null;
/**
* Set the minimum value (inclusive).
*/
public function min(int|float $value): static
{
$this->minimum = $value;
return $this;
}
/**
* Set the maximum value (inclusive).
*/
public function max(int|float $value): static
{
$this->maximum = $value;
return $this;
}
/**
* Set the number the value must be a multiple of.
*/
public function multipleOf(int|float $value): static
{
$this->multipleOf = $value;
return $this;
}
/**
* Set the type's default value.
*/
public function default(int|float $value): static
{
$this->default = $value;
return $this;
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace Illuminate\JsonSchema\Types;
class ObjectType extends Type
{
/**
* Whether additional properties are allowed.
*/
protected ?bool $additionalProperties = null;
/**
* Create a new object type instance.
*
* @param array<string, Type> $properties
*/
public function __construct(protected array $properties = [])
{
//
}
/**
* Disallow additional properties.
*/
public function withoutAdditionalProperties(): static
{
$this->additionalProperties = false;
return $this;
}
/**
* Set the type's default value.
*
* @param array<string, mixed> $value
*/
public function default(array $value): static
{
$this->default = $value;
return $this;
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace Illuminate\JsonSchema\Types;
class StringType extends Type
{
/**
* The minimum length (inclusive).
*/
protected ?int $minLength = null;
/**
* The maximum length (inclusive).
*/
protected ?int $maxLength = null;
/**
* A regular expression the value must match.
*/
protected ?string $pattern = null;
/**
* The format of the string.
*/
protected ?string $format = null;
/**
* Set the minimum length (inclusive).
*/
public function min(int $value): static
{
$this->minLength = $value;
return $this;
}
/**
* Set the maximum length (inclusive).
*/
public function max(int $value): static
{
$this->maxLength = $value;
return $this;
}
/**
* Set the pattern the value must satisfy.
*/
public function pattern(string $value): static
{
$this->pattern = $value;
return $this;
}
/**
* Set the format of the string.
*
* {@link https://json-schema.org/understanding-json-schema/reference/type#built-in-formats}
*/
public function format(string $value): static
{
$this->format = $value;
return $this;
}
/**
* Set the type's default value.
*/
public function default(string $value): static
{
$this->default = $value;
return $this;
}
}

View file

@ -0,0 +1,132 @@
<?php
namespace Illuminate\JsonSchema\Types;
use BackedEnum;
use Illuminate\JsonSchema\JsonSchema;
use Illuminate\JsonSchema\Serializer;
use InvalidArgumentException;
abstract class Type extends JsonSchema
{
/**
* Whether the type is required.
*/
protected ?bool $required = null;
/**
* The type's title.
*/
protected ?string $title = null;
/**
* The type's description.
*/
protected ?string $description = null;
/**
* The default value for the type.
*/
protected mixed $default = null;
/**
* The set of allowed values for the type.
*
* @var array<int, mixed>|null
*/
protected ?array $enum = null;
/**
* Indicates if the type is nullable.
*/
protected ?bool $nullable = null;
/**
* Indicate that the type is required.
*/
public function required(bool $required = true): static
{
$this->required = $required ?: null;
return $this;
}
/**
* Indicate that the type is optional.
*/
public function nullable(bool $nullable = true): static
{
$this->nullable = $nullable ?: null;
return $this;
}
/**
* Set the type's title.
*/
public function title(string $value): static
{
$this->title = $value;
return $this;
}
/**
* Set the type's description.
*/
public function description(string $value): static
{
$this->description = $value;
return $this;
}
/**
* Restrict the value to one of the provided enumerated values.
*
* @param class-string<\BackedEnum>|array<int, mixed> $values
*
* @throws \InvalidArgumentException
*/
public function enum(array|string $values): static
{
if (is_string($values)) {
if (! is_subclass_of($values, BackedEnum::class)) {
throw new InvalidArgumentException('The provided class must be a BackedEnum.');
}
$values = array_column($values::cases(), 'value');
}
// Keep order and allow complex values (arrays / objects) without forcing uniqueness...
$this->enum = array_values($values);
return $this;
}
/**
* Convert the type to an array.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
return Serializer::serialize($this);
}
/**
* Convert the type to its string representation.
*/
public function toString(): string
{
return json_encode($this->toArray(), JSON_PRETTY_PRINT) ?: '';
}
/**
* Convert the type to its string representation.
*/
public function __toString(): string
{
return $this->toString();
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Illuminate\JsonSchema\Types;
use InvalidArgumentException;
class UnionType extends Type
{
/**
* The JSON Schema primitive type names a union may be composed of.
*
* @var array<int, string>
*/
public const SUPPORTED = ['string', 'integer', 'number', 'boolean', 'object', 'array'];
/**
* The union's member type names.
*
* @var array<int, string>
*/
protected array $types;
/**
* Create a new union type instance.
*
* @param array<int, string> $types
*
* @throws \InvalidArgumentException
*/
public function __construct(array $types)
{
$names = array_map('strval', $types);
if (in_array('null', $names, true)) {
$this->nullable();
$names = array_filter($names, static fn (string $name) => $name !== 'null');
}
foreach ($names as $name) {
if (! in_array($name, self::SUPPORTED, true)) {
throw new InvalidArgumentException("Unsupported JSON Schema type [{$name}] in a multi-type union.");
}
}
$this->types = array_values(array_unique($names));
}
/**
* Get the union's member type names.
*
* @return array<int, string>
*/
public function types(): array
{
return $this->types;
}
}

View file

@ -0,0 +1,34 @@
{
"name": "illuminate/json-schema",
"description": "The Illuminate Json Schema package.",
"license": "MIT",
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"require": {
"php": "^8.3",
"illuminate/contracts": "^13.0"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Illuminate\\JsonSchema\\": ""
}
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "13.0.x-dev"
}
}
}