Skip to content

Commit 9835035

Browse files
MatanYadaevMatan Yadaev
and
Matan Yadaev
authored
Refactor toWkt (#48)
* Add tests * Refactor WKT logic Co-authored-by: Matan Yadaev <[email protected]>
1 parent 79a3511 commit 9835035

16 files changed

+192
-44
lines changed

src/Objects/Geometry.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializab
2020
{
2121
public int $srid = 0;
2222

23-
abstract public function toWkt(bool $withFunction = true): string;
23+
abstract public function toWkt(): string;
24+
25+
abstract public function getWktData(): string;
2426

2527
/**
2628
* @param int $options

src/Objects/GeometryCollection.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ public function __construct(Collection|array $geometries, int $srid = 0)
3737
$this->validateGeometriesCount();
3838
}
3939

40-
public function toWkt(bool $withFunction = true): string
40+
public function toWkt(): string
4141
{
42-
$wkt = $this->toCollectionWkt(withFunction: true);
42+
$wktData = $this->getWktData();
4343

44-
return "GEOMETRYCOLLECTION({$wkt})";
44+
return "GEOMETRYCOLLECTION({$wktData})";
45+
}
46+
47+
public function getWktData(): string
48+
{
49+
return $this->geometries
50+
->map(static function (Geometry $geometry): string {
51+
return $geometry->toWkt();
52+
})
53+
->join(', ');
4554
}
4655

4756
/**
@@ -152,15 +161,6 @@ protected function validateGeometriesType(): void
152161
});
153162
}
154163

155-
protected function toCollectionWkt(bool $withFunction): string
156-
{
157-
return $this->geometries
158-
->map(static function (Geometry $geometry) use ($withFunction): string {
159-
return $geometry->toWkt($withFunction);
160-
})
161-
->join(', ');
162-
}
163-
164164
/**
165165
* Checks whether the class is used directly or via a sub-class.
166166
*

src/Objects/LineString.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ class LineString extends PointCollection
88
{
99
protected int $minimumGeometries = 2;
1010

11-
public function toWkt(bool $withFunction = true): string
11+
public function toWkt(): string
1212
{
13-
$wkt = $this->toCollectionWkt(withFunction: false);
13+
$wktData = $this->getWktData();
1414

15-
if ($withFunction) {
16-
return "LINESTRING(${wkt})";
17-
}
15+
return "LINESTRING({$wktData})";
16+
}
1817

19-
return "(${wkt})";
18+
public function getWktData(): string
19+
{
20+
return $this->geometries
21+
->map(static function (Point $point): string {
22+
return $point->getWktData();
23+
})
24+
->join(', ');
2025
}
2126
}

src/Objects/MultiLineString.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,21 @@ public function __construct(Collection|array $geometries, int $srid = 0)
3232
parent::__construct($geometries, $srid);
3333
}
3434

35-
public function toWkt(bool $withFunction = true): string
35+
public function toWkt(): string
3636
{
37-
$wkt = $this->toCollectionWkt(withFunction: false);
37+
$wktData = $this->getWktData();
3838

39-
return "MULTILINESTRING({$wkt})";
39+
return "MULTILINESTRING({$wktData})";
40+
}
41+
42+
public function getWktData(): string
43+
{
44+
return $this->geometries
45+
->map(static function (LineString $lineString): string {
46+
$wktData = $lineString->getWktData();
47+
48+
return "({$wktData})";
49+
})
50+
->join(', ');
4051
}
4152
}

src/Objects/MultiPoint.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ class MultiPoint extends PointCollection
88
{
99
protected int $minimumGeometries = 1;
1010

11-
public function toWkt(bool $withFunction = true): string
11+
public function toWkt(): string
1212
{
13-
$wkt = $this->toCollectionWkt(withFunction: false);
13+
$wktData = $this->getWktData();
1414

15-
return "MULTIPOINT({$wkt})";
15+
return "MULTIPOINT({$wktData})";
16+
}
17+
18+
public function getWktData(): string
19+
{
20+
return $this->geometries
21+
->map(static function (Point $point): string {
22+
return $point->getWktData();
23+
})
24+
->join(', ');
1625
}
1726
}

src/Objects/MultiPolygon.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,21 @@ public function __construct(Collection|array $geometries, int $srid = 0)
3232
parent::__construct($geometries, $srid);
3333
}
3434

35-
public function toWkt(bool $withFunction = true): string
35+
public function toWkt(): string
3636
{
37-
$wkt = $this->toCollectionWkt(withFunction: false);
37+
$wktData = $this->getWktData();
3838

39-
return "MULTIPOLYGON({$wkt})";
39+
return "MULTIPOLYGON({$wktData})";
40+
}
41+
42+
public function getWktData(): string
43+
{
44+
return $this->geometries
45+
->map(static function (Polygon $polygon): string {
46+
$wktData = $polygon->getWktData();
47+
48+
return "({$wktData})";
49+
})
50+
->join(', ');
4051
}
4152
}

src/Objects/Point.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ public function __construct(float $latitude, float $longitude, int $srid = 0)
1717
$this->srid = $srid;
1818
}
1919

20-
public function toWkt(bool $withFunction = true): string
20+
public function toWkt(): string
2121
{
22-
$wkt = "{$this->longitude} {$this->latitude}";
22+
$wktData = $this->getWktData();
2323

24-
if ($withFunction) {
25-
return "POINT({$wkt})";
26-
}
24+
return "POINT({$wktData})";
25+
}
2726

28-
return $wkt;
27+
public function getWktData(): string
28+
{
29+
return "{$this->longitude} {$this->latitude}";
2930
}
3031

3132
/**

src/Objects/Polygon.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66

77
class Polygon extends MultiLineString
88
{
9-
public function toWkt(bool $withFunction = true): string
9+
public function toWkt(): string
1010
{
11-
$wkt = $this->toCollectionWkt(withFunction: false);
11+
$wktData = $this->getWktData();
1212

13-
if ($withFunction) {
14-
return "POLYGON({$wkt})";
15-
}
13+
return "POLYGON({$wktData})";
14+
}
15+
16+
public function getWktData(): string
17+
{
18+
return $this->geometries
19+
->map(static function (LineString $lineString): string {
20+
$wktData = $lineString->getWktData();
1621

17-
return "(${wkt})";
22+
return "({$wktData})";
23+
})
24+
->join(', ');
1825
}
1926
}

src/SpatialBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function whereSrid(
263263
protected function toExpression(Geometry|string $geometryOrColumn): Expression
264264
{
265265
if ($geometryOrColumn instanceof Geometry) {
266-
$wkt = $geometryOrColumn->toWkt(withFunction: true);
266+
$wkt = $geometryOrColumn->toWkt();
267267

268268
return DB::raw("ST_GeomFromText('{$wkt}')");
269269
}

tests/Objects/GeometryCollectionTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
new Point(180, 0),
162162
]);
163163

164-
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)),POINT(0 180))');
164+
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)), POINT(0 180))');
165165

166166
expect($geometryCollectionFromWkt)->toEqual($geometryCollection);
167167
});
@@ -180,11 +180,31 @@
180180
new Point(180, 0),
181181
], 4326);
182182

183-
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)),POINT(0 180))', 4326);
183+
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)), POINT(0 180))', 4326);
184184

185185
expect($geometryCollectionFromWkt)->toEqual($geometryCollection);
186186
});
187187

188+
it('generates geometry collection WKT', function (): void {
189+
$geometryCollection = new GeometryCollection([
190+
new Polygon([
191+
new LineString([
192+
new Point(180, 0),
193+
new Point(179, 1),
194+
new Point(178, 2),
195+
new Point(177, 3),
196+
new Point(180, 0),
197+
]),
198+
]),
199+
new Point(180, 0),
200+
]);
201+
202+
$wkt = $geometryCollection->toWkt();
203+
204+
$expectedWkt = 'GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)), POINT(0 180))';
205+
expect($wkt)->toBe($expectedWkt);
206+
});
207+
188208
it('creates geometry collection from WKB', function (): void {
189209
$geometryCollection = new GeometryCollection([
190210
new Polygon([

tests/Objects/LineStringTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@
101101
expect($lineStringFromWkt)->toEqual($lineString);
102102
});
103103

104+
it('generates line string WKT', function (): void {
105+
$lineString = new LineString([
106+
new Point(180, 0),
107+
new Point(179, 1),
108+
]);
109+
110+
$wkt = $lineString->toWkt();
111+
112+
$expectedWkt = 'LINESTRING(0 180, 1 179)';
113+
expect($wkt)->toBe($expectedWkt);
114+
});
115+
104116
it('creates line string from WKB', function (): void {
105117
$lineString = new LineString([
106118
new Point(180, 0),

tests/Objects/MultiLineStringTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@
117117
expect($multiLineStringFromWkt)->toEqual($multiLineString);
118118
});
119119

120+
it('generates multi line string WKT', function (): void {
121+
$multiLineString = new MultiLineString([
122+
new LineString([
123+
new Point(180, 0),
124+
new Point(179, 1),
125+
]),
126+
]);
127+
128+
$wkt = $multiLineString->toWkt();
129+
130+
$expectedWkt = 'MULTILINESTRING((0 180, 1 179))';
131+
expect($wkt)->toBe($expectedWkt);
132+
});
133+
120134
it('creates multi line string from WKB', function (): void {
121135
$multiLineString = new MultiLineString([
122136
new LineString([

tests/Objects/MultiPointTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@
9393
expect($multiPointFromWkt)->toEqual($multiPoint);
9494
});
9595

96+
it('generates multi point WKT', function (): void {
97+
$multiPoint = new MultiPoint([
98+
new Point(180, 0),
99+
]);
100+
101+
$wkt = $multiPoint->toWkt();
102+
103+
$expectedWkt = 'MULTIPOINT(0 180)';
104+
expect($wkt)->toBe($expectedWkt);
105+
});
106+
96107
it('creates multi point from WKB', function (): void {
97108
$multiPoint = new MultiPoint([
98109
new Point(180, 0),

tests/Objects/MultiPolygonTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@
158158
expect($multiPolygonFromWkt)->toEqual($multiPolygon);
159159
});
160160

161+
it('generates multi polygon WKT', function (): void {
162+
$multiPolygon = new MultiPolygon([
163+
new Polygon([
164+
new LineString([
165+
new Point(180, 0),
166+
new Point(179, 1),
167+
new Point(178, 2),
168+
new Point(177, 3),
169+
new Point(180, 0),
170+
]),
171+
]),
172+
]);
173+
174+
$wkt = $multiPolygon->toWkt();
175+
176+
$expectedWkt = 'MULTIPOLYGON(((0 180, 1 179, 2 178, 3 177, 0 180)))';
177+
expect($wkt)->toBe($expectedWkt);
178+
});
179+
161180
it('creates multi polygon from WKB', function (): void {
162181
$multiPolygon = new MultiPolygon([
163182
new Polygon([

tests/Objects/PointTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
expect($pointFromJson)->toEqual($point);
4242
});
4343

44-
it('generates point json', function (): void {
44+
it('generates point JSON', function (): void {
4545
$point = new Point(180, 0);
4646

4747
$json = $point->toJson();
@@ -72,6 +72,15 @@
7272
expect($pointFromWkt)->toEqual($point);
7373
});
7474

75+
it('generates point WKT', function (): void {
76+
$point = new Point(180, 0);
77+
78+
$wkt = $point->toWkt();
79+
80+
$expectedWkt = 'POINT(0 180)';
81+
expect($wkt)->toBe($expectedWkt);
82+
});
83+
7584
it('creates point from WKB', function (): void {
7685
$point = new Point(180, 0);
7786

tests/Objects/PolygonTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,23 @@
141141
expect($polygonFromWkt)->toEqual($polygon);
142142
});
143143

144+
it('generates polygon WKT', function (): void {
145+
$polygon = new Polygon([
146+
new LineString([
147+
new Point(180, 0),
148+
new Point(179, 1),
149+
new Point(178, 2),
150+
new Point(177, 3),
151+
new Point(180, 0),
152+
]),
153+
]);
154+
155+
$wkt = $polygon->toWkt();
156+
157+
$expectedWkt = 'POLYGON((0 180, 1 179, 2 178, 3 177, 0 180))';
158+
expect($wkt)->toBe($expectedWkt);
159+
});
160+
144161
it('creates polygon from WKB', function (): void {
145162
$polygon = new Polygon([
146163
new LineString([

0 commit comments

Comments
 (0)