@@ -24,33 +24,41 @@ public function newEloquentBuilder($query)
24
24
return new Builder ($ query );
25
25
}
26
26
27
+ protected function geogFromText (GeometryInterface $ geometry )
28
+ {
29
+ return $ this ->getConnection ()->raw (sprintf ("%s.ST_GeogFromText('%s') " ,
30
+ function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ geometry ->toWKT ()));
31
+ }
32
+
33
+ protected function geomFromText (GeometryInterface $ geometry , $ srid = 4326 )
34
+ {
35
+ return $ this ->getConnection ()->raw (sprintf ("%s.ST_GeomFromText('%s', '%d') " ,
36
+ function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ geometry ->toWKT (), $ srid ));
37
+ }
38
+
39
+ public function asWKT (GeometryInterface $ geometry , $ attrs )
40
+ {
41
+ switch (strtoupper ($ attrs ['geomtype ' ])) {
42
+ case 'GEOMETRY ' :
43
+ return $ this ->geomFromText ($ geometry , $ attrs ['srid ' ]);
44
+ break ;
45
+ case 'GEOGRAPHY ' :
46
+ default :
47
+ return $ this ->geogFromText ($ geometry );
48
+ break ;
49
+ }
50
+ }
51
+
27
52
protected function performInsert (EloquentBuilder $ query , array $ options = [])
28
53
{
29
54
foreach ($ this ->attributes as $ key => $ value ) {
30
55
if ($ value instanceof GeometryInterface) {
31
56
$ this ->geometries [$ key ] = $ value ; //Preserve the geometry objects prior to the insert
32
57
if (! $ value instanceof GeometryCollection) {
33
58
$ attrs = $ this ->getPostgisType ($ key );
34
- switch (strtoupper ($ attrs ['geomtype ' ])) {
35
- case 'GEOMETRY ' :
36
- $ this ->attributes [$ key ] = $ this ->getConnection ()->raw (
37
- sprintf ("%s.ST_GeomFromText('%s', '%d') " ,
38
- function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ value ->toWKT (), $ attrs ['srid ' ])
39
- );
40
- break ;
41
- case 'GEOGRAPHY ' :
42
- default :
43
- $ this ->attributes [$ key ] = $ this ->getConnection ()->raw (
44
- sprintf ("%s.ST_GeogFromText('%s') " ,
45
- function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ value ->toWKT ())
46
- );
47
- break ;
48
- }
59
+ $ this ->attributes [$ key ] = $ this ->asWKT ($ value , $ attrs );
49
60
} else {
50
- $ this ->attributes [$ key ] = $ this ->getConnection ()->raw (
51
- sprintf ("%s.ST_GeomFromText('%s', 4326) " ,
52
- function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ value ->toWKT ())
53
- );
61
+ $ this ->attributes [$ key ] = $ this ->geomFromText ($ value );
54
62
}
55
63
}
56
64
}
0 commit comments