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

Commit 8e9ac21

Browse files
authored
Merge pull request #174 from tdomy/add-return-type-for-php81
2 parents cd19db2 + 940855e commit 8e9ac21

File tree

9 files changed

+86
-85
lines changed

9 files changed

+86
-85
lines changed

src/Geometries/GeometryCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function ($geometry_string) {
6969
);
7070
}
7171

72-
public function count()
72+
public function count(): int
7373
{
7474
return count($this->geometries);
7575
}
@@ -79,7 +79,7 @@ public function count()
7979
*
8080
* @return \GeoJson\Geometry\GeometryCollection
8181
*/
82-
public function jsonSerialize()
82+
public function jsonSerialize(): \GeoJson\Geometry\GeometryCollection
8383
{
8484
$geometries = [];
8585
foreach ($this->geometries as $geometry) {

src/Geometries/LineString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __toString()
4444
*
4545
* @return \GeoJson\Geometry\LineString
4646
*/
47-
public function jsonSerialize()
47+
public function jsonSerialize(): \GeoJson\Geometry\LineString
4848
{
4949
$points = [];
5050
foreach ($this->points as $point) {
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()
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/MultiPoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __toString()
7777
*
7878
* @return \GeoJson\Geometry\MultiPoint
7979
*/
80-
public function jsonSerialize()
80+
public function jsonSerialize(): \GeoJson\Geometry\MultiPoint
8181
{
8282
$points = [];
8383
foreach ($this->points as $point) {

src/Geometries/MultiPolygon.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function fromString($wktArgument)
6767
* <p>
6868
* The return value is cast to an integer.
6969
*/
70-
public function count()
70+
public function count(): int
7171
{
7272
return count($this->polygons);
7373
}
@@ -121,7 +121,7 @@ protected static function assembleParts(array $parts)
121121
*
122122
* @return \GeoJson\Geometry\MultiPolygon
123123
*/
124-
public function jsonSerialize()
124+
public function jsonSerialize(): \GeoJson\Geometry\MultiPolygon
125125
{
126126
$polygons = [];
127127
foreach ($this->polygons as $polygon) {

src/Geometries/Point.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function __toString()
115115
*
116116
* @return \GeoJson\Geometry\Point
117117
*/
118-
public function jsonSerialize()
118+
public function jsonSerialize(): \GeoJson\Geometry\Point
119119
{
120120
$position = [$this->getLng(), $this->getLat()];
121121
if ($this->is3d()) $position[] = $this->getAlt();

src/Geometries/PointCollection.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use InvalidArgumentException;
1010
use IteratorAggregate;
1111
use JsonSerializable;
12+
use Traversable;
1213

1314
abstract class PointCollection implements IteratorAggregate, Arrayable, ArrayAccess, Countable, JsonSerializable
1415
{
@@ -46,7 +47,7 @@ public function toArray()
4647
return $this->points;
4748
}
4849

49-
public function getIterator()
50+
public function getIterator(): Traversable
5051
{
5152
return new ArrayIterator($this->points);
5253
}
@@ -70,7 +71,7 @@ public function insertPoint($index, Point $point)
7071
array_splice($this->points, $offset, 0, [$point]);
7172
}
7273

73-
public function offsetExists($offset)
74+
public function offsetExists($offset): bool
7475
{
7576
return isset($this->points[$offset]);
7677
}
@@ -79,12 +80,12 @@ public function offsetExists($offset)
7980
* @param mixed $offset
8081
* @return null|Point
8182
*/
82-
public function offsetGet($offset)
83+
public function offsetGet($offset): ?Point
8384
{
8485
return $this->offsetExists($offset) ? $this->points[$offset] : null;
8586
}
8687

87-
public function offsetSet($offset, $value)
88+
public function offsetSet($offset, $value): void
8889
{
8990
if (!($value instanceof Point)) {
9091
throw new InvalidArgumentException('$value must be an instance of Point');
@@ -97,12 +98,12 @@ public function offsetSet($offset, $value)
9798
}
9899
}
99100

100-
public function offsetUnset($offset)
101+
public function offsetUnset($offset): void
101102
{
102103
unset($this->points[$offset]);
103104
}
104105

105-
public function count()
106+
public function count(): int
106107
{
107108
return count($this->points);
108109
}

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)