Skip to content

Commit 4f50a63

Browse files
committed
Rearranged parameters of spatial analysis Eloquent scopes and fixed tests accordingly
1 parent d7751bb commit 4f50a63

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/Eloquent/SpatialTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,25 @@ public function getSpatialFields()
7272
}
7373
}
7474

75-
public function scopeDistance($query, $distance, $geometry, $column_name, $exclude_self = false)
75+
public function scopeDistance($query, $geometryColumn, $geometry, $distance, $exclude_self = false)
7676
{
77-
$query->whereRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
77+
$query->whereRaw("st_distance(`{$geometryColumn}`, GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
7878

7979
if ($exclude_self) {
80-
$query->whereRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) != 0");
80+
$query->whereRaw("st_distance(`{$geometryColumn}`, GeomFromText('{$geometry->toWkt()}')) != 0");
8181
}
8282

8383
return $query;
8484
}
8585

86-
public function scopeDistanceValue($query, $geometry, $column_name)
86+
public function scopeDistanceValue($query, $geometryColumn, $geometry)
8787
{
8888
$columns = $query->getQuery()->columns;
8989

9090
if (! $columns) {
9191
$query->select('*');
9292
}
93-
$query->selectRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) as distance");
93+
$query->selectRaw("st_distance(`{$geometryColumn}`, GeomFromText('{$geometry->toWkt()}')) as distance");
9494
}
9595

9696
public function scopeComparison($query, $geometryColumn, $geometry, $relationship)

tests/Integration/SpatialTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,20 @@ public function testDistance()
192192
$loc3->location = new Point(3, 3); // Distance from loc1: 2.8284271247462
193193
$loc3->save();
194194

195-
$a = GeometryModel::distance(2, $loc1->location, 'location')->get();
195+
$a = GeometryModel::distance('location', $loc1->location, 2)->get();
196196
$this->assertCount(2, $a);
197197
$this->assertTrue($a->contains('location', $loc1->location));
198198
$this->assertTrue($a->contains('location', $loc2->location));
199199
$this->assertFalse($a->contains('location', $loc3->location));
200200

201201
// Excluding self
202-
$b = GeometryModel::distance(2, $loc1->location, 'location', true)->get();
202+
$b = GeometryModel::distance('location', $loc1->location, 2, true)->get();
203203
$this->assertCount(1, $b);
204204
$this->assertFalse($b->contains('location',$loc1->location));
205205
$this->assertTrue($b->contains('location',$loc2->location));
206206
$this->assertFalse($b->contains('location',$loc3->location));
207207

208-
$c = GeometryModel::distance(1, $loc1->location, 'location')->get();
208+
$c = GeometryModel::distance('location', $loc1->location, 1)->get();
209209
$this->assertCount(1, $c);
210210
$this->assertTrue($c->contains('location', $loc1->location));
211211
$this->assertFalse($c->contains('location', $loc2->location));
@@ -222,7 +222,7 @@ public function testDistanceValue()
222222
$loc2->location = new Point(2, 2); // Distance from loc1: 1.4142135623731
223223
$loc2->save();
224224

225-
$a = GeometryModel::distanceValue($loc1->location, 'location')->get();
225+
$a = GeometryModel::distanceValue('location', $loc1->location)->get();
226226
$this->assertCount(2, $a);
227227
$this->assertEquals(0, $a[0]->distance);
228228
$this->assertEquals(1.4142135623, $a[1]->distance); // PHP floats' 11th+ digits don't matter

tests/Unit/Eloquent/SpatialTraitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function testInsertUpdateGeometryCollectionHasCorrectSql()
194194
public function testScopeDistance()
195195
{
196196
$point = new Point(1, 2);
197-
$query = TestModel::Distance(10, $point, 'point');
197+
$query = TestModel::Distance('point', $point, 10);
198198

199199
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
200200
$q = $query->getQuery();
@@ -205,7 +205,7 @@ public function testScopeDistance()
205205
public function testScopeDistanceExcludingSelf()
206206
{
207207
$point = new Point(1, 2);
208-
$query = TestModel::Distance(10, $point, 'point', true);
208+
$query = TestModel::Distance('point', $point, 10, true);
209209

210210
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
211211
$q = $query->getQuery();
@@ -217,7 +217,7 @@ public function testScopeDistanceExcludingSelf()
217217
public function testScopeDistanceValue()
218218
{
219219
$point = new Point(1, 2);
220-
$query = TestModel::DistanceValue($point, 'point');
220+
$query = TestModel::DistanceValue('point', $point);
221221

222222
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
223223
$q = $query->getQuery();
@@ -230,7 +230,7 @@ public function testScopeDistanceValue()
230230
public function testScopeDistanceValueWithSelect()
231231
{
232232
$point = new Point(1, 2);
233-
$query = TestModel::select('some_column')->distanceValue($point, 'point');
233+
$query = TestModel::select('some_column')->distanceValue('point', $point);
234234

235235
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
236236
$q = $query->getQuery();

0 commit comments

Comments
 (0)