glastree_on_gitea
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?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\IntegerType integer()
|
||||
* @method static Types\NumberType number()
|
||||
* @method static Types\StringType string()
|
||||
* @method static Types\BooleanType boolean()
|
||||
* @method static Types\ArrayType array()
|
||||
*/
|
||||
class JsonSchema
|
||||
{
|
||||
/**
|
||||
* Dynamically pass static methods to the schema instance.
|
||||
*/
|
||||
public static function __callStatic(string $name, mixed $arguments): Type
|
||||
{
|
||||
return (new JsonSchemaTypeFactory)->$name(...$arguments);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
@@ -0,0 +1,100 @@
|
||||
<?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);
|
||||
|
||||
$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',
|
||||
default => throw new RuntimeException('Unsupported ['.get_class($type).'] type.'),
|
||||
};
|
||||
|
||||
$nullable = static::isNullable($type);
|
||||
|
||||
if ($nullable) {
|
||||
$attributes['type'] = [$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_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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?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
|
||||
{
|
||||
if ($unique) {
|
||||
$this->uniqueItems = true;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type's default value.
|
||||
*
|
||||
* @param array<int, mixed> $value
|
||||
*/
|
||||
public function default(array $value): static
|
||||
{
|
||||
$this->default = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
<?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
|
||||
{
|
||||
if ($required) {
|
||||
$this->required = true;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the type is optional.
|
||||
*/
|
||||
public function nullable(bool $nullable = true): static
|
||||
{
|
||||
if ($nullable) {
|
||||
$this->nullable = true;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user