77use ASanikovich \LaravelSpatial \Exceptions \LaravelSpatialException ;
88use ASanikovich \LaravelSpatial \Geometry \Geometry ;
99use 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 ;
1113
1214class GeometryCast implements CastsAttributes
1315{
@@ -26,15 +28,15 @@ public function __construct(string $className)
2628 * @param string|Expression|null $value
2729 * @param array<string, mixed> $attributes
2830 */
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
3032 {
3133 if (! $ value ) {
3234 return null ;
3335 }
3436
3537 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 () );
3840
3941 return $ this ->className ::fromWkt ($ wkt , $ srid );
4042 }
@@ -48,7 +50,7 @@ public function get($model, string $key, mixed $value, array $attributes): ?Geom
4850 *
4951 * @throws LaravelSpatialException
5052 */
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
5254 {
5355 if (! $ value ) {
5456 return null ;
@@ -64,24 +66,27 @@ public function set($model, string $key, mixed $value, array $attributes): Expre
6466
6567 if (! ($ value instanceof $ this ->className )) {
6668 $ 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 ));
7071 }
7172
7273 return $ value ->toSqlExpression ($ model ->getConnection ());
7374 }
7475
75- private function extractWktFromExpression (Expression $ expression ): string
76+ private function extractWktFromExpression (Expression $ expression, Connection $ connection ): string
7677 {
77- preg_match ('/ST_GeomFromText\( \'(.+) \', .+(, .+)?\)/ ' , (string ) $ expression , $ match );
78+ $ expressionValue = $ expression ->getValue ($ connection ->getQueryGrammar ());
79+
80+ preg_match ('/ST_GeomFromText\( \'(.+) \', .+(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
7881
7982 return $ match [1 ];
8083 }
8184
82- private function extractSridFromExpression (Expression $ expression ): int
85+ private function extractSridFromExpression (Expression $ expression, Connection $ connection ): int
8386 {
84- preg_match ('/ST_GeomFromText\( \'.+ \', (.+)(, .+)?\)/ ' , (string ) $ expression , $ match );
87+ $ expressionValue = $ expression ->getValue ($ connection ->getQueryGrammar ());
88+
89+ preg_match ('/ST_GeomFromText\( \'.+ \', (.+)(, .+)?\)/ ' , (string ) $ expressionValue , $ match );
8590
8691 return (int ) $ match [1 ];
8792 }
0 commit comments