1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Database\Drivers\MongoDB; |
4
|
|
|
|
5
|
|
|
use MongoDB\BSON\Binary; |
6
|
|
|
use MongoDB\BSON\Decimal128 as Decimal; |
7
|
|
|
use MongoDB\BSON\Javascript; |
8
|
|
|
use MongoDB\BSON\MaxKey; |
9
|
|
|
use MongoDB\BSON\MinKey; |
10
|
|
|
use MongoDB\BSON\ObjectId; |
11
|
|
|
use MongoDB\BSON\Regex; |
12
|
|
|
use MongoDB\BSON\Timestamp; |
13
|
|
|
use MongoDB\BSON\UTCDateTime; |
14
|
|
|
|
15
|
|
|
class Type |
16
|
|
|
{ |
17
|
|
|
protected static $types = [ |
18
|
|
|
'binary', |
19
|
|
|
'decimal', |
20
|
|
|
'javascript', |
21
|
|
|
'maxKey', |
22
|
|
|
'minKey', |
23
|
|
|
'objectId', |
24
|
|
|
'regex', |
25
|
|
|
'timestamp', |
26
|
|
|
'dateTime', |
27
|
|
|
'string', |
28
|
|
|
'arrayType', |
29
|
|
|
'object', |
30
|
|
|
'boolean', |
31
|
|
|
'double', |
32
|
|
|
'null', |
33
|
|
|
'integer', |
34
|
|
|
'longInteger', |
35
|
|
|
'relationship', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get binary. |
40
|
|
|
* |
41
|
|
|
* @return \MongoDB\BSON\Binary |
42
|
|
|
*/ |
43
|
|
|
public static function binary(string $value, int $type = Binary::TYPE_GENERIC) |
44
|
|
|
{ |
45
|
|
|
return new Binary($value, $type); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get decimal. |
50
|
|
|
* |
51
|
|
|
* @return \MongoDB\BSON\Decimal128 |
52
|
|
|
*/ |
53
|
|
|
public static function decimal(string $value) |
54
|
|
|
{ |
55
|
|
|
return new Decimal($value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get javascript. |
60
|
|
|
* |
61
|
|
|
* @return \MongoDB\BSON\Javascript |
62
|
|
|
*/ |
63
|
|
|
public static function javascript(string $code, array $scope = []) |
64
|
|
|
{ |
65
|
|
|
return new Javascript($code, $scope); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get max key. |
70
|
|
|
* |
71
|
|
|
* @return \MongoDB\BSON\MaxKey |
72
|
|
|
*/ |
73
|
|
|
public static function maxKey() |
74
|
|
|
{ |
75
|
|
|
return new MaxKey(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get min key. |
80
|
|
|
* |
81
|
|
|
* @return \MongoDB\BSON\MinKey |
82
|
|
|
*/ |
83
|
|
|
public static function minKey() |
84
|
|
|
{ |
85
|
|
|
return new MinKey(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get ObjectId. |
90
|
|
|
* |
91
|
|
|
* @return \MongoDB\BSON\ObjectId |
92
|
|
|
*/ |
93
|
|
|
public static function objectId(string $value) |
94
|
|
|
{ |
95
|
|
|
return new ObjectId($value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get regular expression. |
100
|
|
|
* |
101
|
|
|
* @return \MongoDB\BSON\Regex |
102
|
|
|
*/ |
103
|
|
|
public static function regex(string $pattern, string $flags = '') |
104
|
|
|
{ |
105
|
|
|
return new Regex($pattern, $flags); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get timestamp. |
110
|
|
|
* |
111
|
|
|
* @return \MongoDB\BSON\Timestamp |
112
|
|
|
*/ |
113
|
|
|
public static function timestamp(int $increment, int $timestamp) |
114
|
|
|
{ |
115
|
|
|
return new Timestamp($increment, $timestamp); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get datetime. |
120
|
|
|
* |
121
|
|
|
* @param int|null $milliseconds |
122
|
|
|
* |
123
|
|
|
* @return \MongoDB\BSON\UTCDateTime |
124
|
|
|
*/ |
125
|
|
|
public static function dateTime($milliseconds = null) |
126
|
|
|
{ |
127
|
|
|
if (! is_int($milliseconds) || ! is_float($milliseconds) || ! is_string($milliseconds) || ! $milliseconds instanceof \DateTimeInterface) { |
|
|
|
|
128
|
|
|
throw new \Exception($milliseconds.' integer or float or string or instance of DateTimeInterface'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return new UTCDateTime($milliseconds); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get string. |
136
|
|
|
* |
137
|
|
|
* @param string $value |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public static function string($value) |
142
|
|
|
{ |
143
|
|
|
return (string) $value; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get array. |
148
|
|
|
* |
149
|
|
|
* @param array $value |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public static function arrayType($value) |
154
|
|
|
{ |
155
|
|
|
return (array) $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get object. |
160
|
|
|
* |
161
|
|
|
* @param object $value |
162
|
|
|
* |
163
|
|
|
* @return object |
164
|
|
|
*/ |
165
|
|
|
public static function object($value) |
166
|
|
|
{ |
167
|
|
|
return (object) $value; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get boolean. |
172
|
|
|
* |
173
|
|
|
* @param bool $value |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public static function boolean($value) |
178
|
|
|
{ |
179
|
|
|
return (bool) $value; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get double. |
184
|
|
|
* |
185
|
|
|
* @param float $value |
186
|
|
|
* |
187
|
|
|
* @return float |
188
|
|
|
*/ |
189
|
|
|
public static function double($value) |
190
|
|
|
{ |
191
|
|
|
return (float) $value; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get null. |
196
|
|
|
* |
197
|
|
|
* @return null |
198
|
|
|
*/ |
199
|
|
|
public static function null() |
200
|
|
|
{ |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get integer. |
205
|
|
|
* |
206
|
|
|
* @param int $value |
207
|
|
|
* |
208
|
|
|
* @return int |
209
|
|
|
*/ |
210
|
|
|
public static function integer($value) |
211
|
|
|
{ |
212
|
|
|
return (int) $value; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get long integer. |
217
|
|
|
* |
218
|
|
|
* @param int $value |
219
|
|
|
* |
220
|
|
|
* @return int |
221
|
|
|
*/ |
222
|
|
|
public static function longInteger($value) |
223
|
|
|
{ |
224
|
|
|
return (int) $value; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get types. |
229
|
|
|
* |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
public static function getTypes() |
233
|
|
|
{ |
234
|
|
|
return (array) self::$types; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|