-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchema.php
More file actions
191 lines (166 loc) · 6.16 KB
/
Schema.php
File metadata and controls
191 lines (166 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
namespace Cortex\JsonSchema;
use Closure;
use BackedEnum;
use Cortex\JsonSchema\Enums\SchemaType;
use Cortex\JsonSchema\Types\NullSchema;
use Cortex\JsonSchema\Types\ArraySchema;
use Cortex\JsonSchema\Types\UnionSchema;
use Cortex\JsonSchema\Types\NumberSchema;
use Cortex\JsonSchema\Types\ObjectSchema;
use Cortex\JsonSchema\Types\StringSchema;
use Cortex\JsonSchema\Enums\SchemaVersion;
use Cortex\JsonSchema\Types\BooleanSchema;
use Cortex\JsonSchema\Types\IntegerSchema;
use Cortex\JsonSchema\Contracts\JsonSchema;
use Cortex\JsonSchema\Converters\EnumConverter;
use Cortex\JsonSchema\Converters\JsonConverter;
use Cortex\JsonSchema\Converters\ClassConverter;
use Cortex\JsonSchema\Exceptions\SchemaException;
use Cortex\JsonSchema\Converters\ClosureConverter;
class Schema
{
private static ?SchemaVersion $schemaVersion = null;
/**
* Set the default schema version for all new schemas.
*/
public static function setDefaultVersion(SchemaVersion $schemaVersion): void
{
self::$schemaVersion = $schemaVersion;
}
/**
* Get the current default schema version.
*/
public static function getDefaultVersion(): SchemaVersion
{
return self::$schemaVersion ?? SchemaVersion::default();
}
/**
* Reset the default version to the package default.
*/
public static function resetDefaultVersion(): void
{
self::$schemaVersion = null;
}
public static function string(?string $title = null, ?SchemaVersion $schemaVersion = null): StringSchema
{
return new StringSchema($title, $schemaVersion ?? self::getDefaultVersion());
}
public static function object(?string $title = null, ?SchemaVersion $schemaVersion = null): ObjectSchema
{
return new ObjectSchema($title, $schemaVersion ?? self::getDefaultVersion());
}
public static function array(?string $title = null, ?SchemaVersion $schemaVersion = null): ArraySchema
{
return new ArraySchema($title, $schemaVersion ?? self::getDefaultVersion());
}
public static function number(?string $title = null, ?SchemaVersion $schemaVersion = null): NumberSchema
{
return new NumberSchema($title, $schemaVersion ?? self::getDefaultVersion());
}
public static function integer(?string $title = null, ?SchemaVersion $schemaVersion = null): IntegerSchema
{
return new IntegerSchema($title, $schemaVersion ?? self::getDefaultVersion());
}
public static function boolean(?string $title = null, ?SchemaVersion $schemaVersion = null): BooleanSchema
{
return new BooleanSchema($title, $schemaVersion ?? self::getDefaultVersion());
}
public static function null(?string $title = null, ?SchemaVersion $schemaVersion = null): NullSchema
{
return new NullSchema($title, $schemaVersion ?? self::getDefaultVersion());
}
/**
* @param array<int, \Cortex\JsonSchema\Enums\SchemaType> $types
*/
public static function union(array $types, ?string $title = null, ?SchemaVersion $schemaVersion = null): UnionSchema
{
return new UnionSchema($types, $title, $schemaVersion ?? self::getDefaultVersion());
}
public static function mixed(?string $title = null, ?SchemaVersion $schemaVersion = null): UnionSchema
{
return new UnionSchema(SchemaType::cases(), $title, $schemaVersion ?? self::getDefaultVersion());
}
/**
* Create a schema from a given closure.
*/
public static function fromClosure(
Closure $closure,
?SchemaVersion $schemaVersion = null,
bool $ignoreUnknownTypes = false,
): ObjectSchema {
$closureConverter = new ClosureConverter(
$closure,
$schemaVersion ?? self::getDefaultVersion(),
$ignoreUnknownTypes,
);
return $closureConverter->convert();
}
/**
* Create a schema from a given class.
*
* @param object|class-string $class
*/
public static function fromClass(
object|string $class,
bool $publicOnly = true,
?SchemaVersion $schemaVersion = null,
): ObjectSchema {
$classConverter = new ClassConverter(
$class,
$publicOnly,
$schemaVersion ?? self::getDefaultVersion(),
);
return $classConverter->convert();
}
/**
* Create a schema from a given enum.
*
* @param class-string<\BackedEnum> $enum
*/
public static function fromEnum(string $enum, ?SchemaVersion $schemaVersion = null): StringSchema|IntegerSchema
{
$enumConverter = new EnumConverter(
$enum,
$schemaVersion ?? self::getDefaultVersion(),
);
return $enumConverter->convert();
}
/**
* Create a schema from a given value.
*/
public static function from(
mixed $value,
?SchemaVersion $version = null,
bool $ignoreUnknownTypes = false,
): JsonSchema {
$schemaVersion = $version ?? self::getDefaultVersion();
return match (true) {
$value instanceof Closure => self::fromClosure($value, $schemaVersion, $ignoreUnknownTypes),
is_string($value) && enum_exists($value) && is_subclass_of($value, BackedEnum::class) => self::fromEnum(
$value,
$schemaVersion,
),
is_string($value) && class_exists($value) || is_object($value) => self::fromClass(
$value,
true,
$schemaVersion,
),
// @phpstan-ignore argument.type
is_array($value) || (is_string($value) && json_validate($value)) => self::fromJson($value, $schemaVersion),
default => throw new SchemaException(
'Unsupported value type. Only closures, enums, arrays and classes are supported.',
),
};
}
/**
* Create a schema from a JSON Schema definition.
*
* @param string|array<string, mixed> $json
*/
public static function fromJson(string|array $json, ?SchemaVersion $schemaVersion = null): JsonSchema
{
return (new JsonConverter($json, $schemaVersion ?? self::getDefaultVersion()))->convert();
}
}