@@ -36,10 +36,9 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
36
36
}
37
37
38
38
if ($ value instanceof ExpressionContract) {
39
- $ wkt = $ this ->extractWktFromExpression ($ value , $ model ->getConnection ());
40
- $ srid = $ this ->extractSridFromExpression ($ value , $ model ->getConnection ());
39
+ $ expressionValues = $ this ->extractFromExpression ($ value , $ model ->getConnection ());
41
40
42
- return $ this ->className ::fromWkt ($ wkt , $ srid );
41
+ return $ this ->className ::fromWkt ($ expressionValues [ ' wkt ' ] , $ expressionValues [ ' srid ' ] );
43
42
}
44
43
45
44
return $ this ->className ::fromWkb ($ value );
@@ -87,23 +86,16 @@ private function isCorrectGeometryType(mixed $value): bool
87
86
return $ value instanceof $ this ->className && get_class ($ value ) === $ this ->className ;
88
87
}
89
88
90
- private function extractWktFromExpression (ExpressionContract $ expression , Connection $ connection ): string
91
- {
92
- $ grammar = $ connection ->getQueryGrammar ();
93
- $ expressionValue = $ expression ->getValue ($ grammar );
94
-
95
- preg_match ('/ST_GeomFromText\( \'(.+) \', .+(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
96
-
97
- return $ match [1 ];
98
- }
99
-
100
- private function extractSridFromExpression (ExpressionContract $ expression , Connection $ connection ): int
89
+ /**
90
+ * @return array{wkt: string, srid: int}
91
+ */
92
+ private function extractFromExpression (ExpressionContract $ expression , Connection $ connection ): array
101
93
{
102
94
$ grammar = $ connection ->getQueryGrammar ();
103
95
$ expressionValue = $ expression ->getValue ($ grammar );
104
96
105
- preg_match (' /ST_GeomFromText\( \' .+ \' , (.+)(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
97
+ preg_match (" /ST_GeomFromText\(\s*'([^']+)'\s*(?:,\s*(\d+))?\s*(?:,\s*'([^']+)')?\s*\)/ " , (string ) $ expressionValue , $ matches );
106
98
107
- return (int ) $ match [ 1 ];
99
+ return [ ' wkt ' => $ matches [ 1 ], ' srid ' => (int ) ( $ matches [ 2 ] ?? 0 ) ];
108
100
}
109
101
}
0 commit comments