-
Notifications
You must be signed in to change notification settings - Fork 4
/
expressions.php
163 lines (147 loc) · 5.48 KB
/
expressions.php
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
<?php
declare(strict_types=1);
// Target namespace for the generated files, allows to use ::class notation without use statements
namespace MongoDB\Builder\Expression;
use MongoDB\BSON;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Type;
use MongoDB\CodeGenerator\Definition\PhpObject;
use MongoDB\Model\BSONArray;
use stdClass;
use function ucfirst;
$bsonTypes = [
// BSON types
// @see https://www.mongodb.com/docs/manual/reference/bson-types/
// Ignore deprecated types and min/max keys which are not actual types
'double' => ['int', BSON\Int64::class, 'float'],
'string' => ['string'],
'object' => ['array', stdClass::class, BSON\Document::class, BSON\Serializable::class],
'array' => ['array', BSONArray::class, BSON\PackedArray::class],
'binData' => ['string', BSON\Binary::class],
'objectId' => [BSON\ObjectId::class],
'bool' => ['bool'],
'date' => [BSON\UTCDateTime::class],
'null' => ['null'],
'regex' => [BSON\Regex::class],
'javascript' => ['string', BSON\Javascript::class],
'int' => ['int'],
'timestamp' => ['int', BSON\Timestamp::class],
'long' => ['int', BSON\Int64::class],
'decimal' => ['int', BSON\Int64::class, 'float', BSON\Decimal128::class],
];
// "any" accepts all the BSON types. No generic "object" or "mixed"
$bsonTypes['any'] = ['bool', 'int', 'float', 'string', 'array', 'null', stdClass::class, BSON\Type::class];
// "number" accepts all the numeric types
$bsonTypes['number'] = ['int', 'float', BSON\Int64::class, BSON\Decimal128::class];
$expressions = [];
$resolvesToInterfaces = [];
foreach ($bsonTypes as $name => $acceptedTypes) {
$expressions[$name] = ['acceptedTypes' => $acceptedTypes];
$resolvesTo = 'resolvesTo' . ucfirst($name);
$resolvesToInterface = __NAMESPACE__ . '\\' . ucfirst($resolvesTo);
$expressions[$resolvesTo] = [
'generate' => PhpObject::PhpInterface,
'implements' => [Type\ExpressionInterface::class],
'returnType' => $resolvesToInterface,
'acceptedTypes' => $acceptedTypes,
];
$fieldPathName = $name . 'FieldPath';
if ($name === 'any') {
$fieldPathName = 'fieldPath';
} else {
$resolvesToInterfaces[] = $resolvesToInterface;
}
$expressions[$fieldPathName] = [
'generate' => PhpObject::PhpClass,
'implements' => [Type\FieldPathInterface::class, $resolvesToInterface],
'acceptedTypes' => ['string'],
];
}
$expressions['resolvesToLong']['implements'] = [ResolvesToInt::class];
$expressions['resolvesToInt']['implements'] = [ResolvesToNumber::class];
$expressions['resolvesToDecimal']['implements'] = [ResolvesToDouble::class];
$expressions['resolvesToDouble']['implements'] = [ResolvesToNumber::class];
$expressions['resolvesToAny']['implements'] = $resolvesToInterfaces;
return $expressions + [
'expression' => [
'returnType' => Type\ExpressionInterface::class,
'acceptedTypes' => [Type\ExpressionInterface::class, ...$bsonTypes['any']],
],
'fieldQuery' => [
'returnType' => Type\FieldQueryInterface::class,
'acceptedTypes' => [Type\FieldQueryInterface::class, ...$bsonTypes['any']],
],
'query' => [
'returnType' => Type\QueryInterface::class,
'acceptedTypes' => [Type\QueryInterface::class, 'array'],
],
'projection' => [
'returnType' => Type\ProjectionInterface::class,
'acceptedTypes' => [Type\ProjectionInterface::class, ...$bsonTypes['object']],
],
'accumulator' => [
'returnType' => Type\AccumulatorInterface::class,
'acceptedTypes' => [Type\AccumulatorInterface::class, ...$bsonTypes['object']],
],
'window' => [
'returnType' => Type\WindowInterface::class,
'acceptedTypes' => [Type\WindowInterface::class, ...$bsonTypes['object']],
],
'stage' => [
'returnType' => Type\StageInterface::class,
'acceptedTypes' => [Type\StageInterface::class, ...$bsonTypes['object']],
],
'pipeline' => [
'acceptedTypes' => [Pipeline::class, ...$bsonTypes['array']],
],
'variable' => [
'generate' => PhpObject::PhpClass,
'implements' => [ResolvesToAny::class],
'acceptedTypes' => ['string'],
],
'geometry' => [
'returnType' => Type\GeometryInterface::class,
'acceptedTypes' => [Type\GeometryInterface::class, ...$bsonTypes['object']],
],
// @todo add enum values
'Granularity' => [
'acceptedTypes' => [...$bsonTypes['string']],
],
'FullDocument' => [
'acceptedTypes' => [...$bsonTypes['string']],
],
'FullDocumentBeforeChange' => [
'acceptedTypes' => [...$bsonTypes['string']],
],
'AccumulatorPercentile' => [
'acceptedTypes' => [...$bsonTypes['string']],
],
'WhenMatched' => [
'acceptedTypes' => [...$bsonTypes['string']],
],
'WhenNotMatched' => [
'acceptedTypes' => [...$bsonTypes['string']],
],
// @todo create specific model classes factories
'OutCollection' => [
'acceptedTypes' => [...$bsonTypes['object']],
],
'CollStats' => [
'acceptedTypes' => [...$bsonTypes['object']],
],
'Range' => [
'acceptedTypes' => [...$bsonTypes['object']],
],
'FillOut' => [
'acceptedTypes' => [...$bsonTypes['object']],
],
'SortSpec' => [
'acceptedTypes' => [...$bsonTypes['object']],
],
'Window' => [
'acceptedTypes' => [...$bsonTypes['object']],
],
'GeoPoint' => [
'acceptedTypes' => [...$bsonTypes['object']],
],
];