13
13
14
14
namespace Laudis \Neo4j ;
15
15
16
+ use Bolt \structures \DateTime ;
17
+ use Bolt \structures \Duration ;
16
18
use Bolt \structures \IStructure ;
17
19
use function count ;
20
+ use function date_default_timezone_get ;
21
+ use DateInterval ;
22
+ use DateTimeInterface ;
23
+ use DateTimeZone ;
18
24
use function gettype ;
19
25
use InvalidArgumentException ;
20
26
use function is_array ;
@@ -67,13 +73,14 @@ public static function asMap(iterable $iterable): CypherMap
67
73
/**
68
74
* @param mixed $value
69
75
*
70
- * @return iterable|scalar|stdClass|null| IStructure
76
+ * @return iterable|scalar|stdClass|IStructure|null
71
77
*/
72
78
public static function asParameter ($ value , bool $ boltDriver = false )
73
79
{
74
80
return self ::cypherMapToStdClass ($ value ) ??
75
81
self ::emptySequenceToArray ($ value ) ??
76
82
self ::convertBoltConvertibles ($ value , $ boltDriver ) ??
83
+ self ::convertTemporalTypes ($ value , $ boltDriver ) ??
77
84
self ::filledIterableToArray ($ value ) ??
78
85
self ::stringAbleToString ($ value ) ??
79
86
self ::filterInvalidType ($ value );
@@ -195,6 +202,9 @@ private static function iterableToArray(iterable $value): array
195
202
return $ tbr ;
196
203
}
197
204
205
+ /**
206
+ * @param mixed $value
207
+ */
198
208
private static function convertBoltConvertibles ($ value , bool $ boltDriver ): ?IStructure
199
209
{
200
210
if ($ boltDriver && $ value instanceof BoltConvertibleInterface) {
@@ -203,4 +213,40 @@ private static function convertBoltConvertibles($value, bool $boltDriver): ?IStr
203
213
204
214
return null ;
205
215
}
216
+
217
+ /**
218
+ * @param mixed $value
219
+ */
220
+ private static function convertTemporalTypes ($ value , bool $ boltDriver ): ?IStructure
221
+ {
222
+ if ($ boltDriver ) {
223
+ if ($ value instanceof DateTimeInterface) {
224
+ $ tz = $ value ->getTimezone ();
225
+ /** @var DateTimeInterface $gm */
226
+ $ gm = gmdate ('now ' );
227
+ if ($ tz ) {
228
+ $ tz = $ tz ->getOffset ($ gm );
229
+ } else {
230
+ $ tz = (new DateTimeZone (date_default_timezone_get ()))->getOffset ($ gm );
231
+ }
232
+
233
+ return new DateTime (
234
+ $ value ->getTimestamp (),
235
+ ((int ) $ value ->format ('u ' )) * 1000 ,
236
+ $ tz
237
+ );
238
+ }
239
+
240
+ if ($ value instanceof DateInterval) {
241
+ return new Duration (
242
+ $ value ->y * 12 + $ value ->m ,
243
+ $ value ->d ,
244
+ $ value ->h * 60 * 60 * $ value ->i * 60 + $ value ->s * 60 ,
245
+ (int ) ($ value ->f * 1000 )
246
+ );
247
+ }
248
+ }
249
+
250
+ return null ;
251
+ }
206
252
}
0 commit comments