Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions phpinsights.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UselessOverridingMethodSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff;
use SlevomatCodingStandard\Sniffs\Classes\SuperfluousExceptionNamingSniff;
use SlevomatCodingStandard\Sniffs\Commenting\InlineDocCommentDeclarationSniff;
use SlevomatCodingStandard\Sniffs\Functions\UnusedParameterSniff;
use SlevomatCodingStandard\Sniffs\TypeHints\DisallowMixedTypeHintSniff;
use SlevomatCodingStandard\Sniffs\TypeHints\ParameterTypeHintSniff;
Expand Down Expand Up @@ -90,8 +91,11 @@
'ignoreComments' => false,
],
MaxNestingLevelSniff::class => [
'maxNestingLevel' => 3,
],
InlineDocCommentDeclarationSniff::class => [
'exclude' => [
'src/Objects/Geometry.php',
'src/Factory.php',
],
],
UnusedParameterSniff::class => [
Expand Down Expand Up @@ -119,6 +123,7 @@
CyclomaticComplexityIsHigh::class => [
'exclude' => [
'src/Factory.php',
'src/Objects/GeometryCollection.php',
],
],
],
Expand All @@ -136,7 +141,7 @@

'requirements' => [
'min-quality' => 90,
'min-complexity' => 90,
'min-complexity' => 80,
'min-architecture' => 90,
'min-style' => 90,
'disable-security-check' => true,
Expand Down
17 changes: 0 additions & 17 deletions src/Exceptions/InvalidTypeException.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Exceptions/UnsupportedDatabaseDriverException.php

This file was deleted.

115 changes: 33 additions & 82 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace MatanYadaev\EloquentSpatial;

use Geometry as geoPHPGeometry;
use GeometryCollection as geoPHPGeometryCollection;
use geoPHP;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use LineString as geoPHPLineString;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
Expand All @@ -33,106 +34,56 @@ public static function parse(string $value): Geometry
$value = substr($value, 4);
}

/** @var geoPHPGeometry $geoPHPGeometry */
$geoPHPGeometry = geoPHP::load($value);
try {
/** @var geoPHPGeometry|false $geoPHPGeometry */
$geoPHPGeometry = geoPHP::load($value);
} finally {
if (! isset($geoPHPGeometry) || ! $geoPHPGeometry) {
throw new InvalidArgumentException('Invalid spatial value');
}
}

return self::createFromGeometry($geoPHPGeometry);
}

protected static function createFromGeometry(geoPHPGeometry $geometry): Geometry
{
if ($geometry instanceof geoPHPPoint) {
return self::createPoint($geometry->coords[1], $geometry->coords[0]);
if ($geometry->coords[0] === null || $geometry->coords[1] === null) {
if (! isset($geoPHPGeometry) || ! $geoPHPGeometry) {
throw new InvalidArgumentException('Invalid spatial value');
}
}

return new Point($geometry->coords[1], $geometry->coords[0]);
}

/** @var geoPHPGeometryCollection $geometry */
$components = collect($geometry->components)
->map(static function (geoPHPGeometry $geometryComponent): Geometry {
return self::createFromGeometry($geometryComponent);
});

$className = $geometry::class;

if ($className === geoPHPMultiPoint::class) {
return self::createMultiPoint($components);
}
if ($className === geoPHPLineString::class) {
return self::createLineString($components);
}
if ($className === geoPHPPolygon::class) {
return self::createPolygon($components);
}
if ($className === geoPHPMultiLineString::class) {
return self::createMultiLineString($components);
}
if ($className === geoPHPMultiPolygon::class) {
return self::createMultiPolygon($components);
if ($geometry::class === geoPHPMultiPoint::class) {
return new MultiPoint($components);
}

return self::createGeometryCollection($components);
}

protected static function createPoint(float $latitude, float $longitude): Point
{
return new Point($latitude, $longitude);
}

/**
* @param Collection<Point> $points
*
* @return MultiPoint
*/
protected static function createMultiPoint(Collection $points): MultiPoint
{
return new MultiPoint($points);
}

/**
* @param Collection<Point> $points
*
* @return LineString
*/
protected static function createLineString(Collection $points): LineString
{
return new LineString($points);
}
if ($geometry::class === geoPHPLineString::class) {
return new LineString($components);
}

/**
* @param Collection<LineString> $lineStrings
*
* @return Polygon
*/
protected static function createPolygon(Collection $lineStrings): Polygon
{
return new Polygon($lineStrings);
}
if ($geometry::class === geoPHPPolygon::class) {
return new Polygon($components);
}

/**
* @param Collection<LineString> $lineStrings
*
* @return MultiLineString
*/
protected static function createMultiLineString(Collection $lineStrings): MultiLineString
{
return new MultiLineString($lineStrings);
}
if ($geometry::class === geoPHPMultiLineString::class) {
return new MultiLineString($components);
}

/**
* @param Collection<Polygon> $polygons
*
* @return MultiPolygon
*/
protected static function createMultiPolygon(Collection $polygons): MultiPolygon
{
return new MultiPolygon($polygons);
}
if ($geometry::class === geoPHPMultiPolygon::class) {
return new MultiPolygon($components);
}

/**
* @param Collection<Geometry> $geometries
*
* @return GeometryCollection
*/
protected static function createGeometryCollection(Collection $geometries): GeometryCollection
{
return new GeometryCollection($geometries);
return new GeometryCollection($components);
}
}
17 changes: 12 additions & 5 deletions src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Expression;
use MatanYadaev\EloquentSpatial\Exceptions\InvalidTypeException;
use InvalidArgumentException;
use MatanYadaev\EloquentSpatial\Objects\Geometry;

class GeometryCast implements CastsAttributes
{
/** @var class-string<Geometry> */
private string $className;

/**
* @param class-string<Geometry> $className
*/
public function __construct(string $className)
{
$this->className = $className;
Expand All @@ -33,18 +37,18 @@ public function get($model, string $key, $wkt, array $attributes): ?Geometry
return null;
}

return $this->className::fromWkt($wkt, false);
return $this->className::fromWkb($wkt);
}

/**
* @param Model $model
* @param string $key
* @param Geometry|null $geometry
* @param Geometry|mixed|null $geometry
* @param array<string, mixed> $attributes
*
* @return Expression|string|null
*
* @throws InvalidTypeException
* @throws InvalidArgumentException
*/
public function set($model, string $key, $geometry, array $attributes): Expression | string | null
{
Expand All @@ -53,7 +57,10 @@ public function set($model, string $key, $geometry, array $attributes): Expressi
}

if (! ($geometry instanceof $this->className)) {
throw new InvalidTypeException($this->className, $geometry);
$geometryType = is_object($geometry) ? $geometry::class : gettype($geometry);
throw new InvalidArgumentException(
sprintf('Expected %s, %s given.', static::class, $geometryType)
);
}

return $geometry->toWkt();
Expand Down
35 changes: 22 additions & 13 deletions src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,55 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Database\Query\Expression;
use InvalidArgumentException;
use JsonSerializable;
use MatanYadaev\EloquentSpatial\Exceptions\InvalidTypeException;
use MatanYadaev\EloquentSpatial\Factory;
use MatanYadaev\EloquentSpatial\GeometryCast;

abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializable
{
abstract public function toWkt(): Expression;

public function toJson($options = 0): string
{
return json_encode($this, $options);
}

/**
* @param string $wkt
* @param string $wkb
*
* @return static
*
* @throws InvalidTypeException
* @throws InvalidArgumentException
*/
public static function fromWkt(string $wkt): static
public static function fromWkb(string $wkb): static
{
$geometry = Factory::parse($wkt);
$geometry = Factory::parse($wkb);

if (! ($geometry instanceof static)) {
throw new InvalidTypeException(static::class, $geometry);
throw new InvalidArgumentException(
sprintf('Expected %s, %s given.', static::class, $geometry::class)
);
}

return $geometry;
}

public function toJson($options = 0): string
{
return json_encode($this, $options);
}

/**
* @param string $geoJson
*
* @return static
*
* @throws InvalidTypeException
* @throws InvalidArgumentException
*/
public static function fromJson(string $geoJson): static
{
$geometry = Factory::parse($geoJson);

if (! ($geometry instanceof static)) {
throw new InvalidTypeException(static::class, $geometry);
throw new InvalidArgumentException(
sprintf('Expected %s, %s given.', static::class, $geometry::class)
);
}

return $geometry;
Expand All @@ -78,6 +82,11 @@ public function toArray(): array
];
}

/**
* @return array{
* type: string, properties: array<mixed>, geometry: array{type: string, coordinates: array<mixed>}
* }
*/
public function toFeature(): array
{
return [
Expand Down
20 changes: 10 additions & 10 deletions src/Objects/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function __construct(Collection | array $geometries)

$this->geometries = $geometries;

$this->validateGeometriesCount();
$this->validateGeometriesType();
$this->validateGeometriesCount();
}

public function toWkt(): Expression
Expand Down Expand Up @@ -102,11 +102,13 @@ protected function validateGeometriesCount(): void
{
$geometriesCount = $this->geometries->count();
if ($geometriesCount < $this->minimumGeometries) {
$className = self::class;

throw new InvalidArgumentException(
"{$className} must contain at least {$this->minimumGeometries} "
.Str::plural('entries', $geometriesCount)
sprintf(
'%s must contain at least %s %s',
static::class,
$this->minimumGeometries,
Str::plural('entries', $geometriesCount)
)
);
}
}
Expand All @@ -116,12 +118,10 @@ protected function validateGeometriesCount(): void
*/
protected function validateGeometriesType(): void
{
$this->geometries->each(function (Geometry $geometry): void {
if (! ($geometry instanceof $this->collectionOf)) {
$className = self::class;

$this->geometries->each(function (mixed $geometry): void {
if (! is_object($geometry) || ! ($geometry instanceof $this->collectionOf)) {
throw new InvalidArgumentException(
"{$className} must be a collection of {$this->collectionOf}"
sprintf('%s must be a collection of %s', static::class, $this->collectionOf)
);
}
});
Expand Down
Loading