7
7
use ASanikovich \LaravelSpatial \Exceptions \LaravelSpatialException ;
8
8
use ASanikovich \LaravelSpatial \Geometry \Geometry ;
9
9
use Illuminate \Contracts \Database \Eloquent \CastsAttributes ;
10
- use Illuminate \Database \Query \Expression ;
10
+ use Illuminate \Contracts \Database \Query \Expression ;
11
+ use Illuminate \Database \Connection ;
12
+ use Illuminate \Database \Eloquent \Model ;
11
13
12
14
class GeometryCast implements CastsAttributes
13
15
{
@@ -26,15 +28,15 @@ public function __construct(string $className)
26
28
* @param string|Expression|null $value
27
29
* @param array<string, mixed> $attributes
28
30
*/
29
- public function get ($ model , string $ key , mixed $ value , array $ attributes ): ?Geometry
31
+ public function get (Model $ model , string $ key , mixed $ value , array $ attributes ): ?Geometry
30
32
{
31
33
if (! $ value ) {
32
34
return null ;
33
35
}
34
36
35
37
if ($ value instanceof Expression) {
36
- $ wkt = $ this ->extractWktFromExpression ($ value );
37
- $ srid = $ this ->extractSridFromExpression ($ value );
38
+ $ wkt = $ this ->extractWktFromExpression ($ value, $ model -> getConnection () );
39
+ $ srid = $ this ->extractSridFromExpression ($ value, $ model -> getConnection () );
38
40
39
41
return $ this ->className ::fromWkt ($ wkt , $ srid );
40
42
}
@@ -48,7 +50,7 @@ public function get($model, string $key, mixed $value, array $attributes): ?Geom
48
50
*
49
51
* @throws LaravelSpatialException
50
52
*/
51
- public function set ($ model , string $ key , mixed $ value , array $ attributes ): Expression |null
53
+ public function set (Model $ model , string $ key , mixed $ value , array $ attributes ): Expression |null
52
54
{
53
55
if (! $ value ) {
54
56
return null ;
@@ -64,24 +66,27 @@ public function set($model, string $key, mixed $value, array $attributes): Expre
64
66
65
67
if (! ($ value instanceof $ this ->className )) {
66
68
$ geometryType = is_object ($ value ) ? $ value ::class : gettype ($ value );
67
- throw new LaravelSpatialException (
68
- sprintf ('Expected %s, %s given. ' , static ::class, $ geometryType ) // todo
69
- );
69
+
70
+ throw new LaravelSpatialException (sprintf ('Expected %s, %s given. ' , static ::class, $ geometryType ));
70
71
}
71
72
72
73
return $ value ->toSqlExpression ($ model ->getConnection ());
73
74
}
74
75
75
- private function extractWktFromExpression (Expression $ expression ): string
76
+ private function extractWktFromExpression (Expression $ expression, Connection $ connection ): string
76
77
{
77
- preg_match ('/ST_GeomFromText\( \'(.+) \', .+(, .+)?\)/ ' , (string ) $ expression , $ match );
78
+ $ expressionValue = $ expression ->getValue ($ connection ->getQueryGrammar ());
79
+
80
+ preg_match ('/ST_GeomFromText\( \'(.+) \', .+(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
78
81
79
82
return $ match [1 ];
80
83
}
81
84
82
- private function extractSridFromExpression (Expression $ expression ): int
85
+ private function extractSridFromExpression (Expression $ expression, Connection $ connection ): int
83
86
{
84
- preg_match ('/ST_GeomFromText\( \'.+ \', (.+)(, .+)?\)/ ' , (string ) $ expression , $ match );
87
+ $ expressionValue = $ expression ->getValue ($ connection ->getQueryGrammar ());
88
+
89
+ preg_match ('/ST_GeomFromText\( \'.+ \', (.+)(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
85
90
86
91
return (int ) $ match [1 ];
87
92
}
0 commit comments