Skip to content

Commit 35e788c

Browse files
committed
added date, localtime and time
1 parent 8becf9f commit 35e788c

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/Formatter/Specialised/JoltHttpOGMTranslator.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use function array_key_first;
1717
use Closure;
18+
use DateTimeImmutable;
1819
use function is_array;
1920
use Laudis\Neo4j\Contracts\ConnectionInterface;
2021
use Laudis\Neo4j\Contracts\PointInterface;
@@ -24,15 +25,21 @@
2425
use Laudis\Neo4j\Types\CartesianPoint;
2526
use Laudis\Neo4j\Types\CypherList;
2627
use Laudis\Neo4j\Types\CypherMap;
28+
use Laudis\Neo4j\Types\Date;
29+
use Laudis\Neo4j\Types\LocalTime;
2730
use Laudis\Neo4j\Types\Node;
2831
use Laudis\Neo4j\Types\Path;
2932
use Laudis\Neo4j\Types\Relationship;
33+
use Laudis\Neo4j\Types\Time;
3034
use Laudis\Neo4j\Types\UnboundRelationship;
3135
use Laudis\Neo4j\Types\WGS843DPoint;
3236
use Laudis\Neo4j\Types\WGS84Point;
37+
use function preg_match;
3338
use Psr\Http\Message\RequestInterface;
3439
use Psr\Http\Message\ResponseInterface;
3540
use stdClass;
41+
use function str_pad;
42+
use const STR_PAD_RIGHT;
3643
use function strtolower;
3744
use UnexpectedValueException;
3845

@@ -120,10 +127,32 @@ private function translateJoltType(?stdClass $value)
120127

121128
/**
122129
* @return OGMTypes
130+
*
131+
* @psalm-suppress ImpureMethodCall
132+
* @psalm-suppress PossiblyFalseReference
123133
*/
124134
private function translateDateTime(string $datetime)
125135
{
126-
// TODO; They're in ISO format so shouldn't be too hard
136+
if (preg_match('/^\d+-\d{2}-\d{2}$/', $datetime)) {
137+
$date = DateTimeImmutable::createFromFormat('Y-m-d', $datetime);
138+
139+
return new Date((int) $date->diff(new DateTimeImmutable('@0'))->format('%a'));
140+
}
141+
142+
if (preg_match('/^(\d{2}):(\d{2}):(\d{2})((\.)(\d+))?$/', $datetime, $matches)) {
143+
$nanoseconds = $this->nanosecondsFromMatches($matches);
144+
145+
return new LocalTime($nanoseconds);
146+
}
147+
148+
if (preg_match('/^(\d{2}):(\d{2}):(\d{2})((\.)(\d+))?(?<zone>[\w\W])+$/', $datetime, $matches)) {
149+
$nanoseconds = $this->nanosecondsFromMatches($matches);
150+
151+
$offset = $this->offsetFromMatches($matches);
152+
153+
return new Time($nanoseconds, $offset);
154+
}
155+
127156
throw new UnexpectedValueException('Date/time values have not been implemented yet');
128157
}
129158

@@ -307,4 +336,27 @@ private function translateBinary(): Closure
307336
{
308337
throw new UnexpectedValueException('Binary data has not been implemented');
309338
}
339+
340+
private function nanosecondsFromMatches(array $matches): int
341+
{
342+
/** @var array{0: string, 1: string, 2: string, 3: string, 4?: array{0: string, 1: string}} $matches */
343+
$seconds = ((int) $matches[1]) * 60 * 60 + ((int) $matches[2]) * 60 + ((int) $matches[3]);
344+
$nanoseconds = $matches[4][1] ?? '0';
345+
$nanoseconds = str_pad($nanoseconds, 9, '0', STR_PAD_RIGHT);
346+
347+
return $seconds * 1000 * 1000 * 1000 + (int) $nanoseconds;
348+
}
349+
350+
private function offsetFromMatches(array $matches): int
351+
{
352+
/** @var array{zone: string} $matches */
353+
$zone = $matches['zone'];
354+
355+
if (preg_match('/(\d{2}):(\d{2})/', $zone, $matches)) {
356+
/** @var array{0: string, 1: string, 2: string} $matches */
357+
return ((int) $matches[1]) * 60 + (int) $matches[2];
358+
}
359+
360+
return 0;
361+
}
310362
}

0 commit comments

Comments
 (0)