Skip to content

Commit cb05acf

Browse files
committed
Added SRID to GeometryInterface and Geometry models. Updated query builder.
1 parent ccce934 commit cb05acf

17 files changed

+254
-46
lines changed

src/Eloquent/BaseBuilder.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ class BaseBuilder extends QueryBuilder
88
{
99
protected function cleanBindings(array $bindings)
1010
{
11-
$bindings = array_map(function ($binding) {
12-
return $binding instanceof SpatialExpression ? $binding->getSpatialValue() : $binding;
13-
}, $bindings);
11+
$spatialBindings = [];
12+
foreach ($bindings as &$binding) {
13+
if ($binding instanceof SpatialExpression) {
14+
$spatialBindings[] = $binding->getSpatialValue();
15+
$spatialBindings[] = $binding->getSrid();
16+
} else {
17+
$spatialBindings[] = $binding;
18+
}
19+
}
1420

15-
return parent::cleanBindings($bindings);
21+
return parent::cleanBindings($spatialBindings);
1622
}
1723
}

src/Eloquent/SpatialExpression.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ class SpatialExpression extends Expression
88
{
99
public function getValue()
1010
{
11-
return 'ST_GeomFromText(?)';
11+
return 'ST_GeomFromText(?, ?)';
1212
}
1313

1414
public function getSpatialValue()
1515
{
1616
return $this->value->toWkt();
1717
}
18+
19+
public function getSrid()
20+
{
21+
return $this->value->getSrid();
22+
}
1823
}

src/Types/Factory.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,41 @@ class Factory implements \GeoIO\Factory
66
{
77
public function createPoint($dimension, array $coordinates, $srid = null)
88
{
9-
return new Point($coordinates['y'], $coordinates['x']);
9+
return new Point($coordinates['y'], $coordinates['x'], $srid);
1010
}
1111

1212
public function createLineString($dimension, array $points, $srid = null)
1313
{
14-
return new LineString($points);
14+
return new LineString($points, $srid);
1515
}
1616

1717
public function createLinearRing($dimension, array $points, $srid = null)
1818
{
19-
return new LineString($points);
19+
return new LineString($points, $srid);
2020
}
2121

2222
public function createPolygon($dimension, array $lineStrings, $srid = null)
2323
{
24-
return new Polygon($lineStrings);
24+
return new Polygon($lineStrings, $srid);
2525
}
2626

2727
public function createMultiPoint($dimension, array $points, $srid = null)
2828
{
29-
return new MultiPoint($points);
29+
return new MultiPoint($points, $srid);
3030
}
3131

3232
public function createMultiLineString($dimension, array $lineStrings, $srid = null)
3333
{
34-
return new MultiLineString($lineStrings);
34+
return new MultiLineString($lineStrings, $srid);
3535
}
3636

3737
public function createMultiPolygon($dimension, array $polygons, $srid = null)
3838
{
39-
return new MultiPolygon($polygons);
39+
return new MultiPolygon($polygons, $srid);
4040
}
4141

4242
public function createGeometryCollection($dimension, array $geometries, $srid = null)
4343
{
44-
return new GeometryCollection($geometries);
44+
return new GeometryCollection($geometries, $srid);
4545
}
4646
}

src/Types/Geometry.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializabl
1919
7 => GeometryCollection::class,
2020
];
2121

22+
protected $srid;
23+
24+
public function __construct($srid = 0)
25+
{
26+
$this->srid = (int) $srid;
27+
}
28+
29+
public function getSrid() {
30+
return $this->srid;
31+
}
32+
33+
public function setSrid($srid)
34+
{
35+
$this->srid = (int) $srid;
36+
}
37+
2238
public static function getWKTArgument($value)
2339
{
2440
$left = strpos($value, '(');
@@ -61,11 +77,11 @@ public static function fromWKB($wkb)
6177
return $parser->parse($wkb);
6278
}
6379

64-
public static function fromWKT($wkt)
80+
public static function fromWKT($wkt, $srid = 0)
6581
{
6682
$wktArgument = static::getWKTArgument($wkt);
6783

68-
return static::fromString($wktArgument);
84+
return static::fromString($wktArgument, $srid);
6985
}
7086

7187
public static function fromJson($geoJson)

src/Types/GeometryCollection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc
2323

2424
/**
2525
* @param GeometryInterface[] $geometries
26+
* @param int $srid
2627
*
2728
* @throws InvalidArgumentException
2829
*/
29-
public function __construct(array $geometries)
30+
public function __construct(array $geometries, $srid = 0)
3031
{
32+
parent::__construct($srid);
33+
3134
$validated = array_filter($geometries, function ($value) {
3235
return $value instanceof GeometryInterface;
3336
});
@@ -56,15 +59,15 @@ public function __toString()
5659
}, $this->items));
5760
}
5861

59-
public static function fromString($wktArgument)
62+
public static function fromString($wktArgument, $srid = 0)
6063
{
6164
$geometry_strings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument);
6265

6366
return new static(array_map(function ($geometry_string) {
6467
$klass = Geometry::getWKTClass($geometry_string);
6568

6669
return call_user_func($klass.'::fromWKT', $geometry_string);
67-
}, $geometry_strings));
70+
}, $geometry_strings), $srid);
6871
}
6972

7073
public function toArray()

src/Types/GeometryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ interface GeometryInterface
66
{
77
public function toWKT();
88

9-
public static function fromWKT($wkt);
9+
public static function fromWKT($wkt, $srid = 0);
1010

1111
public function __toString();
1212

13-
public static function fromString($wktArgument);
13+
public static function fromString($wktArgument, $srid = 0);
1414

1515
public static function fromJson($geoJson);
1616
}

src/Types/LineString.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ public function toWKT()
1313
return sprintf('LINESTRING(%s)', $this->toPairList());
1414
}
1515

16-
public static function fromWkt($wkt)
16+
public static function fromWkt($wkt, $srid = 0)
1717
{
1818
$wktArgument = Geometry::getWKTArgument($wkt);
1919

20-
return static::fromString($wktArgument);
20+
return static::fromString($wktArgument, $srid);
2121
}
2222

23-
public static function fromString($wktArgument)
23+
public static function fromString($wktArgument, $srid = 0)
2424
{
2525
$pairs = explode(',', trim($wktArgument));
2626
$points = array_map(function ($pair) {
2727
return Point::fromPair($pair);
2828
}, $pairs);
2929

30-
return new static($points);
30+
return new static($points, $srid);
3131
}
3232

3333
public function __toString()

src/Types/MultiLineString.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class MultiLineString extends GeometryCollection
1111
{
1212
/**
1313
* @param LineString[] $lineStrings
14+
* @param int $srid
1415
*/
15-
public function __construct(array $lineStrings)
16+
public function __construct(array $lineStrings, $srid = 0)
1617
{
1718
if (count($lineStrings) < 1) {
1819
throw new InvalidArgumentException('$lineStrings must contain at least one entry');
@@ -26,7 +27,7 @@ public function __construct(array $lineStrings)
2627
throw new InvalidArgumentException('$lineStrings must be an array of LineString');
2728
}
2829

29-
parent::__construct($lineStrings);
30+
parent::__construct($lineStrings, $srid);
3031
}
3132

3233
public function getLineStrings()
@@ -39,14 +40,14 @@ public function toWKT()
3940
return sprintf('MULTILINESTRING(%s)', (string) $this);
4041
}
4142

42-
public static function fromString($wktArgument)
43+
public static function fromString($wktArgument, $srid = 0)
4344
{
4445
$str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1));
4546
$lineStrings = array_map(function ($data) {
4647
return LineString::fromString($data);
4748
}, $str);
4849

49-
return new static($lineStrings);
50+
return new static($lineStrings, $srid);
5051
}
5152

5253
public function __toString()

src/Types/MultiPoint.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public function toWKT()
1313
return sprintf('MULTIPOINT(%s)', (string) $this);
1414
}
1515

16-
public static function fromWkt($wkt)
16+
public static function fromWkt($wkt, $srid = 0)
1717
{
1818
$wktArgument = Geometry::getWKTArgument($wkt);
1919

20-
return static::fromString($wktArgument);
20+
return static::fromString($wktArgument, $srid);
2121
}
2222

23-
public static function fromString($wktArgument)
23+
public static function fromString($wktArgument, $srid = 0)
2424
{
2525
$matches = [];
2626
preg_match_all('/\(\s*(\d+\s+\d+)\s*\)/', trim($wktArgument), $matches);
@@ -29,7 +29,7 @@ public static function fromString($wktArgument)
2929
return Point::fromPair($pair);
3030
}, $matches[1]);
3131

32-
return new static($points);
32+
return new static($points, $srid);
3333
}
3434

3535
public function __toString()

src/Types/MultiPolygon.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class MultiPolygon extends GeometryCollection
1111
{
1212
/**
1313
* @param Polygon[] $polygons
14+
* @param int $srid
1415
*/
15-
public function __construct(array $polygons)
16+
public function __construct(array $polygons, $srid = 0)
1617
{
1718
$validated = array_filter($polygons, function ($value) {
1819
return $value instanceof Polygon;
@@ -21,7 +22,7 @@ public function __construct(array $polygons)
2122
if (count($polygons) !== count($validated)) {
2223
throw new InvalidArgumentException('$polygons must be an array of Polygon');
2324
}
24-
parent::__construct($polygons);
25+
parent::__construct($polygons, $srid);
2526
}
2627

2728
public function toWKT()
@@ -36,14 +37,14 @@ public function __toString()
3637
}, $this->items));
3738
}
3839

39-
public static function fromString($wktArgument)
40+
public static function fromString($wktArgument, $srid = 0)
4041
{
4142
$parts = preg_split('/(\)\s*\)\s*,\s*\(\s*\()/', $wktArgument, -1, PREG_SPLIT_DELIM_CAPTURE);
4243
$polygons = static::assembleParts($parts);
4344

4445
return new static(array_map(function ($polygonString) {
4546
return Polygon::fromString($polygonString);
46-
}, $polygons));
47+
}, $polygons), $srid);
4748
}
4849

4950
/**

src/Types/Point.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ class Point extends Geometry
1212

1313
protected $lng;
1414

15-
public function __construct($lat, $lng)
15+
public function __construct($lat, $lng, $srid = 0)
1616
{
17+
parent::__construct($srid);
18+
1719
$this->lat = (float) $lat;
1820
$this->lng = (float) $lng;
1921
}
@@ -43,21 +45,21 @@ public function toPair()
4345
return $this->getLng().' '.$this->getLat();
4446
}
4547

46-
public static function fromPair($pair)
48+
public static function fromPair($pair, $srid = 0)
4749
{
4850
list($lng, $lat) = explode(' ', trim($pair, "\t\n\r \x0B()"));
4951

50-
return new static((float) $lat, (float) $lng);
52+
return new static((float) $lat, (float) $lng, (int)$srid);
5153
}
5254

5355
public function toWKT()
5456
{
5557
return sprintf('POINT(%s)', (string) $this);
5658
}
5759

58-
public static function fromString($wktArgument)
60+
public static function fromString($wktArgument, $srid = 0)
5961
{
60-
return static::fromPair($wktArgument);
62+
return static::fromPair($wktArgument, $srid);
6163
}
6264

6365
public function __toString()

src/Types/PointCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ abstract class PointCollection extends GeometryCollection
99
{
1010
/**
1111
* @param Point[] $points
12+
* @param int $srid
1213
*/
13-
public function __construct(array $points)
14+
public function __construct(array $points, $srid = 0)
1415
{
1516
if (count($points) < 2) {
1617
throw new InvalidArgumentException('$points must contain at least two entries');
@@ -24,7 +25,7 @@ public function __construct(array $points)
2425
throw new InvalidArgumentException('$points must be an array of Points');
2526
}
2627

27-
parent::__construct($points);
28+
parent::__construct($points, $srid);
2829
}
2930

3031
public function toPairList()

tests/Integration/IntegrationBaseTestCase.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function createApplication()
2222
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
2323

2424
/**
25-
* @param \Illuminate\Config\Repository $config
25+
* @param \Illuminate\Contracts\Config\Repository $config
2626
*/
2727
$config = $app->config;
2828
$config->set('database.default', 'mysql');
@@ -89,12 +89,15 @@ protected function assertDatabaseHas($table, array $data, $connection = null)
8989
}
9090
}
9191

92-
protected function assertException($exceptionName)
92+
protected function assertException($exceptionName, $exceptionMessage = null)
9393
{
9494
if (method_exists(parent::class, 'expectException')) {
9595
parent::expectException($exceptionName);
96+
if (!is_null($exceptionMessage)) {
97+
$this->expectExceptionMessage($exceptionMessage);
98+
}
9699
} else {
97-
$this->setExpectedException($exceptionName);
100+
$this->setExpectedException($exceptionName, $exceptionMessage);
98101
}
99102
}
100103

0 commit comments

Comments
 (0)