5
5
namespace MatanYadaev \EloquentSpatial ;
6
6
7
7
use Illuminate \Contracts \Database \Eloquent \CastsAttributes ;
8
+ use Illuminate \Contracts \Database \Query \Expression as ExpressionContract ;
9
+ use Illuminate \Database \Connection ;
8
10
use Illuminate \Database \Eloquent \Model ;
9
- use Illuminate \Database \Query \Expression ;
10
11
use InvalidArgumentException ;
11
12
use MatanYadaev \EloquentSpatial \Objects \Geometry ;
12
13
@@ -26,7 +27,7 @@ public function __construct(string $className)
26
27
/**
27
28
* @param Model $model
28
29
* @param string $key
29
- * @param string|Expression |null $value
30
+ * @param string|ExpressionContract |null $value
30
31
* @param array<string, mixed> $attributes
31
32
* @return Geometry|null
32
33
*/
@@ -36,9 +37,9 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
36
37
return null ;
37
38
}
38
39
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 () );
42
43
43
44
return $ this ->className ::fromWkt ($ wkt , $ srid );
44
45
}
@@ -51,18 +52,18 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
51
52
* @param string $key
52
53
* @param Geometry|mixed|null $value
53
54
* @param array<string, mixed> $attributes
54
- * @return Expression |null
55
+ * @return ExpressionContract |null
55
56
*
56
57
* @throws InvalidArgumentException
57
58
*/
58
- public function set ($ model , string $ key , $ value , array $ attributes ): Expression |null
59
+ public function set ($ model , string $ key , $ value , array $ attributes ): ExpressionContract |null
59
60
{
60
61
if (! $ value ) {
61
62
return null ;
62
63
}
63
64
64
65
if (is_array ($ value )) {
65
- $ value = Geometry::fromArray ($ value );
66
+ $ value = Geometry::fromArray ($ value );
66
67
}
67
68
68
69
if (! ($ value instanceof $ this ->className )) {
@@ -75,16 +76,22 @@ public function set($model, string $key, $value, array $attributes): Expression|
75
76
return $ value ->toSqlExpression ($ model ->getConnection ());
76
77
}
77
78
78
- private function extractWktFromExpression (Expression $ expression ): string
79
+ private function extractWktFromExpression (ExpressionContract $ expression, Connection $ connection ): string
79
80
{
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 );
81
85
82
86
return $ match [1 ];
83
87
}
84
88
85
- private function extractSridFromExpression (Expression $ expression ): int
89
+ private function extractSridFromExpression (ExpressionContract $ expression, Connection $ connection ): int
86
90
{
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 );
88
95
89
96
return (int ) $ match [1 ];
90
97
}
0 commit comments