Skip to content

Commit df289a5

Browse files
committed
where touches; fix tests
1 parent 85bb877 commit df289a5

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/Builders/SpatialBuilder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,21 @@ public function whereWithin(string $column, Geometry | string $geometryOrColumn)
6969
{
7070
$geometryOrColumn = $this->toExpression($geometryOrColumn);
7171

72-
return $this->orderByRaw("ST_WITHIN(`{$column}`, {$geometryOrColumn})");
72+
return $this->whereRaw("ST_WITHIN(`{$column}`, {$geometryOrColumn})");
7373
}
7474

7575
public function whereContains(string $column, Geometry | string $geometryOrColumn): self
7676
{
7777
$geometryOrColumn = $this->toExpression($geometryOrColumn);
7878

79-
return $this->orderByRaw("ST_CONTAINS(`{$column}`, {$geometryOrColumn})");
79+
return $this->whereRaw("ST_CONTAINS(`{$column}`, {$geometryOrColumn})");
80+
}
81+
82+
public function whereTouches(string $column, Geometry | string $geometryOrColumn): self
83+
{
84+
$geometryOrColumn = $this->toExpression($geometryOrColumn);
85+
86+
return $this->whereRaw("ST_TOUCHES(`{$column}`, {$geometryOrColumn})");
8087
}
8188

8289
protected function toExpression(Geometry | string $geometryOrColumn): Expression

tests/Builders/SpatialBuilderTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Testing\DatabaseMigrations;
66
use MatanYadaev\EloquentSpatial\Objects\MultiPolygon;
77
use MatanYadaev\EloquentSpatial\Objects\Point;
8+
use MatanYadaev\EloquentSpatial\Objects\Polygon;
89
use MatanYadaev\EloquentSpatial\Tests\TestCase;
910
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;
1011

@@ -173,11 +174,11 @@ public function it_orders_by_distance_sphere(): void
173174
public function it_filters_by_within(): void
174175
{
175176
TestPlace::factory()->create([
176-
'multi_polygon' => MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]]}'),
177+
'point' => new Point(23.1, 55.5),
177178
]);
178179

179180
$testPlace = TestPlace::query()
180-
->whereWithin('multi_polygon', new Point(23.1, 55.51))
181+
->whereWithin('point', MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[55.5,23.0],[55.4,23.2],[55.8,23.3],[55.5,23.0]]]]}'))
181182
->first();
182183

183184
$this->assertNotNull($testPlace);
@@ -187,11 +188,25 @@ public function it_filters_by_within(): void
187188
public function it_filters_by_contains(): void
188189
{
189190
TestPlace::factory()->create([
190-
'point' => new Point(23.1, 55.51),
191+
'multi_polygon' => MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[55.5,23.0],[55.4,23.2],[55.8,23.3],[55.5,23.0]]]]}'),
192+
]);
193+
194+
$testPlace = TestPlace::query()
195+
->whereContains('multi_polygon', new Point(23.1, 55.5))
196+
->first();
197+
198+
$this->assertNotNull($testPlace);
199+
}
200+
201+
/** @test */
202+
public function it_filters_by_touches(): void
203+
{
204+
TestPlace::factory()->create([
205+
'point' => new Point(23.1, 55.5),
191206
]);
192207

193208
$testPlace = TestPlace::query()
194-
->whereContains('point', MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]]}'))
209+
->whereTouches('point', MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]]}'))
195210
->first();
196211

197212
$this->assertNotNull($testPlace);

0 commit comments

Comments
 (0)