22
33namespace Neo4j \QueryAPI ;
44
5+ use DateTimeZone ;
56use Neo4j \QueryAPI \Objects \Point ;
67use Neo4j \QueryAPI \Objects \Node ;
78use Neo4j \QueryAPI \Objects \Relationship ;
89use Neo4j \QueryAPI \Objects \Path ;
910use InvalidArgumentException ;
11+ use Neo4j \QueryAPI \Objects \Temporal \Date ;
12+ use Neo4j \QueryAPI \Objects \Temporal \DateTime ;
13+ use Neo4j \QueryAPI \Objects \Temporal \DateTimeZoneId ;
14+ use Neo4j \QueryAPI \Objects \Temporal \Duration ;
15+ use Neo4j \QueryAPI \Objects \Temporal \LocalDateTime ;
16+ use Neo4j \QueryAPI \Objects \Temporal \LocalTime ;
17+ use Neo4j \QueryAPI \Objects \Temporal \Time ;
1018
1119final class OGM
1220{
@@ -21,14 +29,24 @@ public function map(array $data): mixed
2129 }
2230
2331 return match ($ data ['$type ' ]) {
24- 'Integer ' , 'Float ' , 'String ' , ' Boolean ' , ' Duration ' , ' OffsetDateTime ' => $ data ['_value ' ],
32+ 'Integer ' , 'Float ' , 'Boolean ' => $ data ['_value ' ],
2533 'Array ' , 'List ' => is_array ($ data ['_value ' ]) ? array_map ([$ this , 'map ' ], $ data ['_value ' ]) : [],
2634 'Null ' => null ,
2735 'Node ' => $ this ->mapNode ($ data ['_value ' ]),
2836 'Map ' => is_array ($ data ['_value ' ]) ? $ this ->mapProperties ($ data ['_value ' ]) : [],
2937 'Point ' => $ this ->parsePoint ($ data ['_value ' ]),
3038 'Relationship ' => $ this ->mapRelationship ($ data ['_value ' ]),
3139 'Path ' => $ this ->mapPath ($ data ['_value ' ]),
40+ 'Date ' => $ this ->mapDate ($ data ['_value ' ]),
41+ 'OffsetDateTime ' => $ this ->mapDateTime ($ data ['_value ' ]),
42+ 'Time ' => $ this ->mapTime ($ data ['_value ' ]),
43+ 'LocalTime ' => $ this ->mapLocalTime ($ data ['_value ' ]),
44+ 'LocalDateTime ' => $ this ->mapLocalDateTime ($ data ['_value ' ]),
45+ 'Duration ' =>$ this ->mapDuration ($ data ['_value ' ]),
46+
47+ 'String ' => $ this ->isValidTimeZone ($ data ['_value ' ])
48+ ? new DateTimeZoneId ($ data ['_value ' ]) // Convert timezone strings to `DateTimeZoneId`
49+ : $ data ['_value ' ],
3250 default => throw new InvalidArgumentException ('Unknown type: ' . json_encode ($ data , JSON_THROW_ON_ERROR )),
3351 };
3452 }
@@ -37,10 +55,10 @@ public function map(array $data): mixed
3755 private function parsePoint (string $ value ): Point
3856 {
3957 if (preg_match ('/SRID=(\d+);POINT(?: Z)? \(([-\d.]+) ([-\d.]+)(?: ([-\d.]+))?\)/ ' , $ value , $ matches )) {
40- $ srid = (int ) $ matches [1 ];
41- $ x = (float ) $ matches [2 ];
42- $ y = (float ) $ matches [3 ];
43- $ z = isset ($ matches [4 ]) ? (float ) $ matches [4 ] : null ;
58+ $ srid = (int )$ matches [1 ];
59+ $ x = (float )$ matches [2 ];
60+ $ y = (float )$ matches [3 ];
61+ $ z = isset ($ matches [4 ]) ? (float )$ matches [4 ] : null ;
4462
4563 return new Point ($ x , $ y , $ z , $ srid );
4664 }
@@ -129,5 +147,46 @@ private function mapProperties(array $properties): array
129147 return $ mappedProperties ;
130148 }
131149
150+ private function mapDate (string $ value )
151+ {
152+ $ totalDaysSinceEpoch = (new \DateTime ($ value ))->diff (new \DateTime ('@0 ' ))->days ;
153+
154+ return new Date ($ totalDaysSinceEpoch );
155+ }
156+
157+ private function mapDateTime (string $ value )
158+ {
159+ return new DateTime ($ value );
160+ }
161+
162+ private function mapDateTimeZoneId (string $ value )
163+ {
164+ return new DateTimeZoneId ($ value );
165+ }
166+
167+ private function isValidTimeZone (string $ value ): bool
168+ {
169+ return in_array ($ value , timezone_identifiers_list (), true );
170+ }
132171
172+ private function mapTime (mixed $ _value )
173+ {
174+ return new Time ($ _value );
175+
176+ }
177+
178+ private function mapLocalTime (mixed $ _value )
179+ {
180+ return new LocalTime ($ _value );
181+ }
182+
183+ private function mapLocalDateTime (mixed $ _value )
184+ {
185+ return new LocalDateTime ($ _value );
186+ }
187+
188+ private function mapDuration (mixed $ _value )
189+ {
190+ return new Duration ($ _value );
191+ }
133192}
0 commit comments