Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 940855e

Browse files
author
tdomy
committed
Added abstract class to add return type
1 parent 50cc71c commit 940855e

File tree

3 files changed

+72
-72
lines changed

3 files changed

+72
-72
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace MStaack\LaravelPostgis\Geometries;
4+
5+
use Countable;
6+
use InvalidArgumentException;
7+
8+
abstract class LineStringCollection extends Geometry implements Countable
9+
{
10+
/**
11+
* @var LineString[]
12+
*/
13+
protected $linestrings = [];
14+
15+
/**
16+
* @param LineString[] $linestrings
17+
*/
18+
public function __construct(array $linestrings)
19+
{
20+
if (count($linestrings) < 1) {
21+
throw new InvalidArgumentException('$linestrings must contain at least one entry');
22+
}
23+
24+
$validated = array_filter($linestrings, function ($value) {
25+
return $value instanceof LineString;
26+
});
27+
28+
if (count($linestrings) !== count($validated)) {
29+
throw new InvalidArgumentException('$linestrings must be an array of Points');
30+
}
31+
32+
$this->linestrings = $linestrings;
33+
}
34+
35+
public function getLineStrings()
36+
{
37+
return $this->linestrings;
38+
}
39+
40+
public function is3d()
41+
{
42+
if (count($this->linestrings) === 0) return false;
43+
return $this->linestrings[0]->is3d();
44+
}
45+
46+
public static function fromString($wktArgument)
47+
{
48+
$str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1));
49+
$linestrings = array_map(function ($data) {
50+
return LineString::fromString($data);
51+
}, $str);
52+
53+
54+
return new static($linestrings);
55+
}
56+
57+
public function __toString()
58+
{
59+
return implode(',', array_map(function (LineString $linestring) {
60+
return sprintf('(%s)', (string)$linestring);
61+
}, $this->getLineStrings()));
62+
}
63+
64+
public function count(): int
65+
{
66+
return count($this->linestrings);
67+
}
68+
}

src/Geometries/MultiLineString.php

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,21 @@
22

33
namespace MStaack\LaravelPostgis\Geometries;
44

5-
use Countable;
6-
use InvalidArgumentException;
7-
8-
class MultiLineString extends Geometry implements Countable
5+
class MultiLineString extends LineStringCollection
96
{
10-
/**
11-
* @var LineString[]
12-
*/
13-
protected $linestrings = [];
14-
15-
/**
16-
* @param LineString[] $linestrings
17-
*/
18-
public function __construct(array $linestrings)
19-
{
20-
if (count($linestrings) < 1) {
21-
throw new InvalidArgumentException('$linestrings must contain at least one entry');
22-
}
23-
24-
$validated = array_filter($linestrings, function ($value) {
25-
return $value instanceof LineString;
26-
});
27-
28-
if (count($linestrings) !== count($validated)) {
29-
throw new InvalidArgumentException('$linestrings must be an array of Points');
30-
}
31-
32-
$this->linestrings = $linestrings;
33-
}
34-
35-
public function getLineStrings()
36-
{
37-
return $this->linestrings;
38-
}
39-
40-
public function is3d()
41-
{
42-
if (count($this->linestrings) === 0) return false;
43-
return $this->linestrings[0]->is3d();
44-
}
45-
467
public function toWKT()
478
{
489
$wktType = 'MULTILINESTRING';
4910
if ($this->is3d()) $wktType .= ' Z';
5011
return sprintf('%s(%s)', $wktType, (string)$this);
5112
}
5213

53-
public static function fromString($wktArgument)
54-
{
55-
$str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1));
56-
$linestrings = array_map(function ($data) {
57-
return LineString::fromString($data);
58-
}, $str);
59-
60-
61-
return new static($linestrings);
62-
}
63-
64-
public function __toString()
65-
{
66-
return implode(',', array_map(function (LineString $linestring) {
67-
return sprintf('(%s)', (string)$linestring);
68-
}, $this->getLineStrings()));
69-
}
70-
71-
public function count(): int
72-
{
73-
return count($this->linestrings);
74-
}
75-
7614
/**
7715
* Convert to GeoJson Point that is jsonable to GeoJSON
7816
*
7917
* @return \GeoJson\Geometry\MultiLineString
8018
*/
81-
public function jsonSerialize()
19+
public function jsonSerialize(): \GeoJson\Geometry\MultiLineString
8220
{
8321
$linestrings = [];
8422

src/Geometries/Polygon.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@
44

55
use GeoJson\Geometry\LinearRing;
66

7-
class Polygon extends MultiLineString
7+
class Polygon extends LineStringCollection
88
{
9-
public function is3d()
10-
{
11-
if (count($this->linestrings) === 0) return false;
12-
return $this->linestrings[0]->is3d();
13-
}
14-
159
public function toWKT()
1610
{
1711
$wktType = 'POLYGON';
@@ -24,7 +18,7 @@ public function toWKT()
2418
*
2519
* @return \GeoJson\Geometry\Polygon
2620
*/
27-
public function jsonSerialize()
21+
public function jsonSerialize(): \GeoJson\Geometry\Polygon
2822
{
2923
$linearrings = [];
3024
foreach ($this->linestrings as $linestring) {

0 commit comments

Comments
 (0)