Skip to content

Commit afbb4d0

Browse files
committed
Removed flag exclude_self in favor of new scopes.
1 parent d77f61d commit afbb4d0

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/Eloquent/SpatialTrait.php

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

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

79-
if ($exclude_self) {
80-
$query->whereRaw("st_distance(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) != 0");
81-
}
82-
8379
return $query;
8480
}
8581

86-
public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distance, $exclude_self = false)
82+
public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $distance)
8783
{
88-
$query->whereRaw("st_distance_sphere(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
84+
$query = $this->scopeDistance($query, $geometryColumn, $geometry, $distance);
8985

90-
if ($exclude_self) {
91-
$query->whereRaw("st_distance_sphere(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) != 0");
92-
}
86+
$query->whereRaw("st_distance(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) != 0");
9387

9488
return $query;
9589
}
@@ -104,6 +98,22 @@ public function scopeDistanceValue($query, $geometryColumn, $geometry)
10498
$query->selectRaw("st_distance(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) as distance");
10599
}
106100

101+
public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distance)
102+
{
103+
$query->whereRaw("st_distance_sphere(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
104+
105+
return $query;
106+
}
107+
108+
public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geometry, $distance)
109+
{
110+
$query = $this->scopeDistanceSphere($query, $geometryColumn, $geometry, $distance);
111+
112+
$query->whereRaw("st_distance_sphere(`{$geometryColumn}`, ST_GeomFromText('{$geometry->toWkt()}')) != 0");
113+
114+
return $query;
115+
}
116+
107117
public function scopeDistanceSphereValue($query, $geometryColumn, $geometry)
108118
{
109119
$columns = $query->getQuery()->columns;

tests/Integration/SpatialTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function testDistance()
199199
$this->assertFalse($a->contains('location', $loc3->location));
200200

201201
// Excluding self
202-
$b = GeometryModel::distance('location', $loc1->location, 2, true)->get();
202+
$b = GeometryModel::distanceExcludingSelf('location', $loc1->location, 2)->get();
203203
$this->assertCount(1, $b);
204204
$this->assertFalse($b->contains('location', $loc1->location));
205205
$this->assertTrue($b->contains('location', $loc2->location));
@@ -233,7 +233,7 @@ public function testDistanceSphere()
233233
$this->assertFalse($a->contains('location', $loc3->location));
234234

235235
// Excluding self
236-
$b = GeometryModel::distanceSphere('location', $loc1->location, 200, true)->get();
236+
$b = GeometryModel::distanceSphereExcludingSelf('location', $loc1->location, 200)->get();
237237
$this->assertCount(1, $b);
238238
$this->assertFalse($b->contains('location', $loc1->location));
239239
$this->assertTrue($b->contains('location', $loc2->location));

tests/Unit/Eloquent/SpatialTraitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function testScopeDistance()
205205
public function testScopeDistanceExcludingSelf()
206206
{
207207
$point = new Point(1, 2);
208-
$query = TestModel::Distance('point', $point, 10, true);
208+
$query = TestModel::DistanceExcludingSelf('point', $point, 10);
209209

210210
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
211211
$q = $query->getQuery();
@@ -228,7 +228,7 @@ public function testScopeDistanceSphere()
228228
public function testScopeDistanceSphereExcludingSelf()
229229
{
230230
$point = new Point(1, 2);
231-
$query = TestModel::DistanceSphere('point', $point, 10, true);
231+
$query = TestModel::DistanceSphereExcludingSelf('point', $point, 10);
232232

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

0 commit comments

Comments
 (0)