Skip to content

Commit 98f296a

Browse files
committed
added conversion to temporal types
1 parent f35e487 commit 98f296a

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/ParameterHelper.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313

1414
namespace Laudis\Neo4j;
1515

16+
use Bolt\structures\DateTime;
17+
use Bolt\structures\Duration;
1618
use Bolt\structures\IStructure;
1719
use function count;
20+
use function date_default_timezone_get;
21+
use DateInterval;
22+
use DateTimeInterface;
23+
use DateTimeZone;
1824
use function gettype;
1925
use InvalidArgumentException;
2026
use function is_array;
@@ -67,13 +73,14 @@ public static function asMap(iterable $iterable): CypherMap
6773
/**
6874
* @param mixed $value
6975
*
70-
* @return iterable|scalar|stdClass|null|IStructure
76+
* @return iterable|scalar|stdClass|IStructure|null
7177
*/
7278
public static function asParameter($value, bool $boltDriver = false)
7379
{
7480
return self::cypherMapToStdClass($value) ??
7581
self::emptySequenceToArray($value) ??
7682
self::convertBoltConvertibles($value, $boltDriver) ??
83+
self::convertTemporalTypes($value, $boltDriver) ??
7784
self::filledIterableToArray($value) ??
7885
self::stringAbleToString($value) ??
7986
self::filterInvalidType($value);
@@ -195,6 +202,9 @@ private static function iterableToArray(iterable $value): array
195202
return $tbr;
196203
}
197204

205+
/**
206+
* @param mixed $value
207+
*/
198208
private static function convertBoltConvertibles($value, bool $boltDriver): ?IStructure
199209
{
200210
if ($boltDriver && $value instanceof BoltConvertibleInterface) {
@@ -203,4 +213,40 @@ private static function convertBoltConvertibles($value, bool $boltDriver): ?IStr
203213

204214
return null;
205215
}
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+
}
206252
}

0 commit comments

Comments
 (0)