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

Commit

Permalink
Merge pull request #174 from tdomy/add-return-type-for-php81
Browse files Browse the repository at this point in the history
  • Loading branch information
mstaack authored Jul 17, 2022
2 parents cd19db2 + 940855e commit 8e9ac21
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 85 deletions.
4 changes: 2 additions & 2 deletions src/Geometries/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function ($geometry_string) {
);
}

public function count()
public function count(): int
{
return count($this->geometries);
}
Expand All @@ -79,7 +79,7 @@ public function count()
*
* @return \GeoJson\Geometry\GeometryCollection
*/
public function jsonSerialize()
public function jsonSerialize(): \GeoJson\Geometry\GeometryCollection
{
$geometries = [];
foreach ($this->geometries as $geometry) {
Expand Down
2 changes: 1 addition & 1 deletion src/Geometries/LineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __toString()
*
* @return \GeoJson\Geometry\LineString
*/
public function jsonSerialize()
public function jsonSerialize(): \GeoJson\Geometry\LineString
{
$points = [];
foreach ($this->points as $point) {
Expand Down
68 changes: 68 additions & 0 deletions src/Geometries/LineStringCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace MStaack\LaravelPostgis\Geometries;

use Countable;
use InvalidArgumentException;

abstract class LineStringCollection extends Geometry implements Countable
{
/**
* @var LineString[]
*/
protected $linestrings = [];

/**
* @param LineString[] $linestrings
*/
public function __construct(array $linestrings)
{
if (count($linestrings) < 1) {
throw new InvalidArgumentException('$linestrings must contain at least one entry');
}

$validated = array_filter($linestrings, function ($value) {
return $value instanceof LineString;
});

if (count($linestrings) !== count($validated)) {
throw new InvalidArgumentException('$linestrings must be an array of Points');
}

$this->linestrings = $linestrings;
}

public function getLineStrings()
{
return $this->linestrings;
}

public function is3d()
{
if (count($this->linestrings) === 0) return false;
return $this->linestrings[0]->is3d();
}

public static function fromString($wktArgument)
{
$str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1));
$linestrings = array_map(function ($data) {
return LineString::fromString($data);
}, $str);


return new static($linestrings);
}

public function __toString()
{
return implode(',', array_map(function (LineString $linestring) {
return sprintf('(%s)', (string)$linestring);
}, $this->getLineStrings()));
}

public function count(): int
{
return count($this->linestrings);
}
}
66 changes: 2 additions & 64 deletions src/Geometries/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,21 @@

namespace MStaack\LaravelPostgis\Geometries;

use Countable;
use InvalidArgumentException;

class MultiLineString extends Geometry implements Countable
class MultiLineString extends LineStringCollection
{
/**
* @var LineString[]
*/
protected $linestrings = [];

/**
* @param LineString[] $linestrings
*/
public function __construct(array $linestrings)
{
if (count($linestrings) < 1) {
throw new InvalidArgumentException('$linestrings must contain at least one entry');
}

$validated = array_filter($linestrings, function ($value) {
return $value instanceof LineString;
});

if (count($linestrings) !== count($validated)) {
throw new InvalidArgumentException('$linestrings must be an array of Points');
}

$this->linestrings = $linestrings;
}

public function getLineStrings()
{
return $this->linestrings;
}

public function is3d()
{
if (count($this->linestrings) === 0) return false;
return $this->linestrings[0]->is3d();
}

public function toWKT()
{
$wktType = 'MULTILINESTRING';
if ($this->is3d()) $wktType .= ' Z';
return sprintf('%s(%s)', $wktType, (string)$this);
}

public static function fromString($wktArgument)
{
$str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1));
$linestrings = array_map(function ($data) {
return LineString::fromString($data);
}, $str);


return new static($linestrings);
}

public function __toString()
{
return implode(',', array_map(function (LineString $linestring) {
return sprintf('(%s)', (string)$linestring);
}, $this->getLineStrings()));
}

public function count()
{
return count($this->linestrings);
}

/**
* Convert to GeoJson Point that is jsonable to GeoJSON
*
* @return \GeoJson\Geometry\MultiLineString
*/
public function jsonSerialize()
public function jsonSerialize(): \GeoJson\Geometry\MultiLineString
{
$linestrings = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Geometries/MultiPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __toString()
*
* @return \GeoJson\Geometry\MultiPoint
*/
public function jsonSerialize()
public function jsonSerialize(): \GeoJson\Geometry\MultiPoint
{
$points = [];
foreach ($this->points as $point) {
Expand Down
4 changes: 2 additions & 2 deletions src/Geometries/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function fromString($wktArgument)
* <p>
* The return value is cast to an integer.
*/
public function count()
public function count(): int
{
return count($this->polygons);
}
Expand Down Expand Up @@ -121,7 +121,7 @@ protected static function assembleParts(array $parts)
*
* @return \GeoJson\Geometry\MultiPolygon
*/
public function jsonSerialize()
public function jsonSerialize(): \GeoJson\Geometry\MultiPolygon
{
$polygons = [];
foreach ($this->polygons as $polygon) {
Expand Down
2 changes: 1 addition & 1 deletion src/Geometries/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function __toString()
*
* @return \GeoJson\Geometry\Point
*/
public function jsonSerialize()
public function jsonSerialize(): \GeoJson\Geometry\Point
{
$position = [$this->getLng(), $this->getLat()];
if ($this->is3d()) $position[] = $this->getAlt();
Expand Down
13 changes: 7 additions & 6 deletions src/Geometries/PointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use InvalidArgumentException;
use IteratorAggregate;
use JsonSerializable;
use Traversable;

abstract class PointCollection implements IteratorAggregate, Arrayable, ArrayAccess, Countable, JsonSerializable
{
Expand Down Expand Up @@ -46,7 +47,7 @@ public function toArray()
return $this->points;
}

public function getIterator()
public function getIterator(): Traversable
{
return new ArrayIterator($this->points);
}
Expand All @@ -70,7 +71,7 @@ public function insertPoint($index, Point $point)
array_splice($this->points, $offset, 0, [$point]);
}

public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->points[$offset]);
}
Expand All @@ -79,12 +80,12 @@ public function offsetExists($offset)
* @param mixed $offset
* @return null|Point
*/
public function offsetGet($offset)
public function offsetGet($offset): ?Point
{
return $this->offsetExists($offset) ? $this->points[$offset] : null;
}

public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
if (!($value instanceof Point)) {
throw new InvalidArgumentException('$value must be an instance of Point');
Expand All @@ -97,12 +98,12 @@ public function offsetSet($offset, $value)
}
}

public function offsetUnset($offset)
public function offsetUnset($offset): void
{
unset($this->points[$offset]);
}

public function count()
public function count(): int
{
return count($this->points);
}
Expand Down
10 changes: 2 additions & 8 deletions src/Geometries/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@

use GeoJson\Geometry\LinearRing;

class Polygon extends MultiLineString
class Polygon extends LineStringCollection
{
public function is3d()
{
if (count($this->linestrings) === 0) return false;
return $this->linestrings[0]->is3d();
}

public function toWKT()
{
$wktType = 'POLYGON';
Expand All @@ -24,7 +18,7 @@ public function toWKT()
*
* @return \GeoJson\Geometry\Polygon
*/
public function jsonSerialize()
public function jsonSerialize(): \GeoJson\Geometry\Polygon
{
$linearrings = [];
foreach ($this->linestrings as $linestring) {
Expand Down

0 comments on commit 8e9ac21

Please sign in to comment.