-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParser.php
More file actions
139 lines (119 loc) · 3.67 KB
/
Parser.php
File metadata and controls
139 lines (119 loc) · 3.67 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
<?php
declare(strict_types=1);
namespace Chubbyphp\Parsing;
use Chubbyphp\Parsing\Schema\ArraySchema;
use Chubbyphp\Parsing\Schema\AssocSchema;
use Chubbyphp\Parsing\Schema\BackedEnumSchema;
use Chubbyphp\Parsing\Schema\BoolSchema;
use Chubbyphp\Parsing\Schema\ConstSchema;
use Chubbyphp\Parsing\Schema\DateTimeSchema;
use Chubbyphp\Parsing\Schema\DiscriminatedUnionSchema;
use Chubbyphp\Parsing\Schema\FloatSchema;
use Chubbyphp\Parsing\Schema\IntSchema;
use Chubbyphp\Parsing\Schema\LazySchema;
use Chubbyphp\Parsing\Schema\LiteralSchema;
use Chubbyphp\Parsing\Schema\ObjectSchema;
use Chubbyphp\Parsing\Schema\ObjectSchemaInterface;
use Chubbyphp\Parsing\Schema\RecordSchema;
use Chubbyphp\Parsing\Schema\RespectValidationSchema;
use Chubbyphp\Parsing\Schema\SchemaInterface;
use Chubbyphp\Parsing\Schema\StringSchema;
use Chubbyphp\Parsing\Schema\TupleSchema;
use Chubbyphp\Parsing\Schema\UnionSchema;
use Respect\Validation\Validatable;
final class Parser implements ParserInterface
{
public function array(SchemaInterface $itemSchema): ArraySchema
{
return new ArraySchema($itemSchema);
}
/**
* @param array<string, SchemaInterface> $fieldNameToSchema
*/
public function assoc(array $fieldNameToSchema): AssocSchema
{
return new AssocSchema($fieldNameToSchema);
}
/**
* @param class-string<\BackedEnum> $backedEnumClass
*/
public function backedEnum(string $backedEnumClass): BackedEnumSchema
{
return new BackedEnumSchema($backedEnumClass);
}
public function bool(): BoolSchema
{
return new BoolSchema();
}
public function dateTime(): DateTimeSchema
{
return new DateTimeSchema();
}
public function const(bool|float|int|string $const): ConstSchema
{
return new ConstSchema($const);
}
/**
* @param array<ObjectSchemaInterface> $objectSchemas
*/
public function discriminatedUnion(array $objectSchemas, string $discriminatorFieldName): DiscriminatedUnionSchema
{
return new DiscriminatedUnionSchema($objectSchemas, $discriminatorFieldName);
}
public function float(): FloatSchema
{
return new FloatSchema();
}
public function int(): IntSchema
{
return new IntSchema();
}
/**
* @param \Closure(): SchemaInterface $lazy
*/
public function lazy(\Closure $lazy): SchemaInterface
{
return new LazySchema($lazy);
}
/**
* @deprecated Use const($literal) instead
*/
public function literal(bool|float|int|string $literal): LiteralSchema
{
return new LiteralSchema($literal);
}
/**
* @param array<string, SchemaInterface> $fieldNameToSchema
* @param class-string $classname
*/
public function object(array $fieldNameToSchema, string $classname = \stdClass::class, bool $construct = false): ObjectSchema
{
return new ObjectSchema($fieldNameToSchema, $classname, $construct);
}
public function record(SchemaInterface $fieldSchema): RecordSchema
{
return new RecordSchema($fieldSchema);
}
public function string(): StringSchema
{
return new StringSchema();
}
/**
* @param array<SchemaInterface> $schemas
*/
public function tuple(array $schemas): TupleSchema
{
return new TupleSchema($schemas);
}
/**
* @param array<SchemaInterface> $schemas
*/
public function union(array $schemas): UnionSchema
{
return new UnionSchema($schemas);
}
public function respectValidation(Validatable $validatable): RespectValidationSchema
{
return new RespectValidationSchema($validatable);
}
}