55namespace MatanYadaev \EloquentSpatial ;
66
77use Illuminate \Contracts \Database \Eloquent \CastsAttributes ;
8+ use Illuminate \Contracts \Database \Query \Expression as ExpressionContract ;
9+ use Illuminate \Database \Connection ;
810use Illuminate \Database \Eloquent \Model ;
9- use Illuminate \Database \Query \Expression ;
1011use InvalidArgumentException ;
1112use MatanYadaev \EloquentSpatial \Objects \Geometry ;
1213
@@ -26,7 +27,7 @@ public function __construct(string $className)
2627 /**
2728 * @param Model $model
2829 * @param string $key
29- * @param string|Expression |null $value
30+ * @param string|ExpressionContract |null $value
3031 * @param array<string, mixed> $attributes
3132 * @return Geometry|null
3233 */
@@ -36,9 +37,9 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
3637 return null ;
3738 }
3839
39- if ($ value instanceof Expression ) {
40- $ wkt = $ this ->extractWktFromExpression ($ value );
41- $ srid = $ this ->extractSridFromExpression ($ value );
40+ if ($ value instanceof ExpressionContract ) {
41+ $ wkt = $ this ->extractWktFromExpression ($ value, $ model -> getConnection () );
42+ $ srid = $ this ->extractSridFromExpression ($ value, $ model -> getConnection () );
4243
4344 return $ this ->className ::fromWkt ($ wkt , $ srid );
4445 }
@@ -51,18 +52,18 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
5152 * @param string $key
5253 * @param Geometry|mixed|null $value
5354 * @param array<string, mixed> $attributes
54- * @return Expression |null
55+ * @return ExpressionContract |null
5556 *
5657 * @throws InvalidArgumentException
5758 */
58- public function set ($ model , string $ key , $ value , array $ attributes ): Expression |null
59+ public function set ($ model , string $ key , $ value , array $ attributes ): ExpressionContract |null
5960 {
6061 if (! $ value ) {
6162 return null ;
6263 }
6364
6465 if (is_array ($ value )) {
65- $ value = Geometry::fromArray ($ value );
66+ $ value = Geometry::fromArray ($ value );
6667 }
6768
6869 if (! ($ value instanceof $ this ->className )) {
@@ -75,16 +76,22 @@ public function set($model, string $key, $value, array $attributes): Expression|
7576 return $ value ->toSqlExpression ($ model ->getConnection ());
7677 }
7778
78- private function extractWktFromExpression (Expression $ expression ): string
79+ private function extractWktFromExpression (ExpressionContract $ expression, Connection $ connection ): string
7980 {
80- preg_match ('/ST_GeomFromText\( \'(.+) \', .+(, .+)?\)/ ' , (string ) $ expression , $ match );
81+ $ grammar = $ connection ->getQueryGrammar ();
82+ $ expressionValue = $ expression ->getValue ($ grammar );
83+
84+ preg_match ('/ST_GeomFromText\( \'(.+) \', .+(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
8185
8286 return $ match [1 ];
8387 }
8488
85- private function extractSridFromExpression (Expression $ expression ): int
89+ private function extractSridFromExpression (ExpressionContract $ expression, Connection $ connection ): int
8690 {
87- preg_match ('/ST_GeomFromText\( \'.+ \', (.+)(, .+)?\)/ ' , (string ) $ expression , $ match );
91+ $ grammar = $ connection ->getQueryGrammar ();
92+ $ expressionValue = $ expression ->getValue ($ grammar );
93+
94+ preg_match ('/ST_GeomFromText\( \'.+ \', (.+)(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
8895
8996 return (int ) $ match [1 ];
9097 }
0 commit comments