2
2
3
3
namespace Neo4j \QueryAPI ;
4
4
5
+ use DateTimeZone ;
5
6
use Neo4j \QueryAPI \Objects \Point ;
6
7
use Neo4j \QueryAPI \Objects \Node ;
7
8
use Neo4j \QueryAPI \Objects \Relationship ;
8
9
use Neo4j \QueryAPI \Objects \Path ;
9
10
use 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 ;
10
18
11
19
final class OGM
12
20
{
@@ -21,14 +29,24 @@ public function map(array $data): mixed
21
29
}
22
30
23
31
return match ($ data ['$type ' ]) {
24
- 'Integer ' , 'Float ' , 'String ' , ' Boolean ' , ' Duration ' , ' OffsetDateTime ' => $ data ['_value ' ],
32
+ 'Integer ' , 'Float ' , 'Boolean ' => $ data ['_value ' ],
25
33
'Array ' , 'List ' => is_array ($ data ['_value ' ]) ? array_map ([$ this , 'map ' ], $ data ['_value ' ]) : [],
26
34
'Null ' => null ,
27
35
'Node ' => $ this ->mapNode ($ data ['_value ' ]),
28
36
'Map ' => is_array ($ data ['_value ' ]) ? $ this ->mapProperties ($ data ['_value ' ]) : [],
29
37
'Point ' => $ this ->parsePoint ($ data ['_value ' ]),
30
38
'Relationship ' => $ this ->mapRelationship ($ data ['_value ' ]),
31
39
'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 ' ],
32
50
default => throw new InvalidArgumentException ('Unknown type: ' . json_encode ($ data , JSON_THROW_ON_ERROR )),
33
51
};
34
52
}
@@ -37,10 +55,10 @@ public function map(array $data): mixed
37
55
private function parsePoint (string $ value ): Point
38
56
{
39
57
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 ;
44
62
45
63
return new Point ($ x , $ y , $ z , $ srid );
46
64
}
@@ -129,5 +147,46 @@ private function mapProperties(array $properties): array
129
147
return $ mappedProperties ;
130
148
}
131
149
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
+ }
132
171
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
+ }
133
192
}
0 commit comments