@@ -18,11 +18,12 @@ class OGM
18
18
*/
19
19
public function map (array $ object ): mixed
20
20
{
21
+
21
22
if (!isset ($ object ['$type ' ])) {
22
- if (isset ( $ object [ 'elementId ' ] , $ object[ 'labels ' ] , $ object[ 'properties ' ] )) {
23
- return $ this ->mapNode ($ object ); // Handle as a Node
23
+ if (array_key_exists ( 'elementId ' , $ object) && array_key_exists ( 'labels ' , $ object) && array_key_exists ( 'properties ' , $ object )) {
24
+ return $ this ->mapNode ($ object );
24
25
}
25
- throw new \InvalidArgumentException ('Unknown object type: ' . json_encode ($ object ));
26
+ throw new \InvalidArgumentException ('Unknown object type: ' . json_encode ($ object, JSON_THROW_ON_ERROR ));
26
27
}
27
28
28
29
// if (!isset($object['_value'])) {
@@ -39,21 +40,30 @@ public function map(array $object): mixed
39
40
'Point ' => $ this ->parseWKT ($ object ['_value ' ]),
40
41
'Relationship ' => $ this ->mapRelationship ($ object ['_value ' ]),
41
42
'Path ' => $ this ->mapPath ($ object ['_value ' ]),
42
- default => throw new \InvalidArgumentException ('Unknown type: ' . $ object ['$type ' ] . ' in object: ' . json_encode ($ object )),
43
+ default => throw new \InvalidArgumentException ('Unknown type: ' . $ object ['$type ' ] . ' in object: ' . json_encode ($ object, JSON_THROW_ON_ERROR )),
43
44
};
44
45
}
45
46
46
47
public static function parseWKT (string $ wkt ): Point
47
48
{
48
- $ sridPart = substr ($ wkt , 0 , strpos ($ wkt , '; ' ));
49
+ $ sridPos = strpos ($ wkt , '; ' );
50
+ if ($ sridPos === false ) {
51
+ throw new \InvalidArgumentException ("Invalid WKT format: missing ';' " );
52
+ }
53
+ $ sridPart = substr ($ wkt , 0 , $ sridPos );
49
54
$ srid = (int )str_replace ('SRID= ' , '' , $ sridPart );
50
55
51
- $ pointPart = substr ($ wkt , strpos ($ wkt , 'POINT ' ) + 6 );
56
+ $ pointPos = strpos ($ wkt , 'POINT ' );
57
+ if ($ pointPos === false ) {
58
+ throw new \InvalidArgumentException ("Invalid WKT format: missing 'POINT' " );
59
+ }
60
+ $ pointPart = substr ($ wkt , $ pointPos + 6 );
61
+
52
62
$ pointPart = str_replace ('Z ' , '' , $ pointPart );
53
63
$ pointPart = trim ($ pointPart , ' () ' );
54
64
$ coordinates = explode (' ' , $ pointPart );
55
65
56
- [$ x , $ y , $ z ] = array_pad (array_map ('floatval ' , $ coordinates ), 3 , null );
66
+ [$ x , $ y , $ z ] = array_pad (array_map ('floatval ' , $ coordinates ), 3 , 0.0 );
57
67
58
68
return new Point ($ x , $ y , $ z , $ srid );
59
69
}
0 commit comments