@@ -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 );
@@ -76,23 +75,16 @@ public function set($model, string $key, $value, array $attributes): ?Expression
76
75
return $ value ->toSqlExpression ($ model ->getConnection ());
77
76
}
78
77
79
- private function extractWktFromExpression (ExpressionContract $ expression , Connection $ connection ): string
80
- {
81
- $ grammar = $ connection ->getQueryGrammar ();
82
- $ expressionValue = $ expression ->getValue ($ grammar );
83
-
84
- preg_match ('/ST_GeomFromText\( \'(.+) \', .+(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
85
-
86
- return $ match [1 ];
87
- }
88
-
89
- private function extractSridFromExpression (ExpressionContract $ expression , Connection $ connection ): int
78
+ /**
79
+ * @return array{wkt: string, srid: int}
80
+ */
81
+ private function extractFromExpression (ExpressionContract $ expression , Connection $ connection ): array
90
82
{
91
83
$ grammar = $ connection ->getQueryGrammar ();
92
84
$ expressionValue = $ expression ->getValue ($ grammar );
93
85
94
- preg_match (' /ST_GeomFromText\( \' .+ \' , (.+)(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
86
+ preg_match (" /ST_GeomFromText\(\s*'([^']+)'\s*(?:,\s*(\d+))?\s*(?:,\s*'([^']+)')?\s*\)/ " , (string ) $ expressionValue , $ matches );
95
87
96
- return (int ) $ match [ 1 ];
88
+ return [ ' wkt ' => $ matches [ 1 ], ' srid ' => (int ) ( $ matches [ 2 ] ?? 0 ) ];
97
89
}
98
90
}
0 commit comments