Файл: vendor/laravel/framework/src/Illuminate/JsonSchema/Serializer.php
Строк: 75
<?php
namespace IlluminateJsonSchema;
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(TypesType $type): array
{
/** @var array<string, mixed> $attributes */
$attributes = (fn () => get_object_vars($type))->call($type);
$attributes['type'] = match (get_class($type)) {
TypesArrayType::class => 'array',
TypesBooleanType::class => 'boolean',
TypesIntegerType::class => 'integer',
TypesNumberType::class => 'number',
TypesObjectType::class => 'object',
TypesStringType::class => 'string',
TypesUnionType::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 TypesObjectType) {
if (count($attributes['properties']) === 0) {
unset($attributes['properties']);
} else {
$required = array_map(
'strval',
array_keys(array_filter(
$attributes['properties'],
static fn (TypesType $property) => static::isRequired($property),
))
);
if ($required !== []) {
$attributes['required'] = $required;
}
$attributes['properties'] = array_map(
static fn (TypesType $property) => static::serialize($property),
$attributes['properties'],
);
}
}
if ($type instanceof TypesArrayType) {
if (isset($attributes['items']) && $attributes['items'] instanceof TypesType) {
$attributes['items'] = static::serialize($attributes['items']);
}
}
return $attributes;
}
/**
* Determine if the given type is required.
*/
protected static function isRequired(TypesType $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(TypesType $type): bool
{
$attributes = (fn () => get_object_vars($type))->call($type);
return isset($attributes['nullable']) && $attributes['nullable'] === true;
}
}