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
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ parameters:
- src
- tests
level: max
ignoreErrors:
- '#Method MatanYadaev\\EloquentSpatial\\Objects\\.+::getGeometries\(\) should return array<.+> but returns array<MatanYadaev\\EloquentSpatial\\Objects\\Geometry>\.#'
- '#Method MatanYadaev\\EloquentSpatial\\Objects\\(Geometry|GeometryCollection)::(toJson|toFeatureCollectionJson)\(\) should return string but returns string\|false\.#'
excludePaths:
- ./src/Factory.php
checkMissingIterableValueType: true
10 changes: 9 additions & 1 deletion src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public static function fromWkt(string $wkt): static

public function toJson($options = 0): string
{
/* @phpstan-ignore-next-line */
return json_encode($this, $options);
}

Expand Down Expand Up @@ -79,6 +78,15 @@ public function toArray(): array
];
}

public function toFeature(): array
{
return [
'type' => 'Feature',
'properties' => [],
'geometry' => $this->toArray(),
];
}

/**
* @return array<mixed>
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Objects/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ public function getGeometries(): array
return $this->geometries->all();
}

public function toFeatureCollectionJson(): string
{
/** @var Collection<Geometry> $geometries */
$geometries = static::class === self::class
? $this->geometries
: collect([$this]);

$features = $geometries->map(static function (Geometry $geometry): array {
return $geometry->toFeature();
});

return json_encode([
'type' => 'FeatureCollection',
'features' => $features,
]);
}

/**
* @throws InvalidArgumentException
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Objects/LineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public function toWkt(): Expression
{
return DB::raw("LINESTRING({$this->toCollectionWkt()})");
}

/**
* @return array<Point>
*/
public function getGeometries(): array
{
return parent::getGeometries();
}
}
11 changes: 8 additions & 3 deletions src/Objects/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

/**
* @method array<LineString> getGeometries()
*/
class MultiLineString extends GeometryCollection
{
/** @var Collection<LineString> */
Expand All @@ -32,4 +29,12 @@ public function toWkt(): Expression
{
return DB::raw("MULTILINESTRING({$this->toCollectionWkt()})");
}

/**
* @return array<LineString>
*/
public function getGeometries(): array
{
return parent::getGeometries();
}
}
11 changes: 8 additions & 3 deletions src/Objects/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;

/**
* @method array<Polygon> getGeometries()
*/
class MultiPolygon extends GeometryCollection
{
/** @var Collection<Polygon> */
Expand All @@ -35,4 +32,12 @@ public function toWkt(): Expression
{
return DB::raw("MULTIPOLYGON({$this->toCollectionWkt()})");
}

/**
* @return array<Polygon>
*/
public function getGeometries(): array
{
return parent::getGeometries();
}
}
11 changes: 8 additions & 3 deletions src/Objects/PointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Illuminate\Support\Collection;
use InvalidArgumentException;

/**
* @method array<Point> getGeometries()
*/
abstract class PointCollection extends GeometryCollection
{
/** @var Collection<Point> */
Expand All @@ -26,4 +23,12 @@ public function __construct(Collection | array $geometries)
{
parent::__construct($geometries);
}

/**
* @return array<Point>
*/
public function getGeometries(): array
{
return parent::getGeometries();
}
}
121 changes: 77 additions & 44 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public function it_stores_geometry_collection(): void
'geometry_collection' => new GeometryCollection([
new Polygon([
new LineString([
new Point(23.1, 55.5),
new Point(23.2, 55.6),
new Point(23.3, 55.7),
new Point(23.1, 55.5),
new Point(0, 0),
new Point(1, 1),
new Point(2, 2),
new Point(3, 3),
new Point(0, 0),
]),
]),
new Point(23.1, 55.5),
new Point(0, 0),
]),
])->fresh();

Expand All @@ -40,20 +41,22 @@ public function it_stores_geometry_collection(): void
$lineStrings = $polygon->getGeometries();
$points = $lineStrings[0]->getGeometries();

$this->assertEquals(23.1, $points[0]->latitude);
$this->assertEquals(55.5, $points[0]->longitude);
$this->assertEquals(23.2, $points[1]->latitude);
$this->assertEquals(55.6, $points[1]->longitude);
$this->assertEquals(23.3, $points[2]->latitude);
$this->assertEquals(55.7, $points[2]->longitude);
$this->assertEquals(23.1, $points[3]->latitude);
$this->assertEquals(55.5, $points[3]->longitude);
$this->assertEquals(0, $points[0]->latitude);
$this->assertEquals(0, $points[0]->longitude);
$this->assertEquals(1, $points[1]->latitude);
$this->assertEquals(1, $points[1]->longitude);
$this->assertEquals(2, $points[2]->latitude);
$this->assertEquals(2, $points[2]->longitude);
$this->assertEquals(3, $points[3]->latitude);
$this->assertEquals(3, $points[3]->longitude);
$this->assertEquals(0, $points[4]->latitude);
$this->assertEquals(0, $points[4]->longitude);

/** @var Point $point */
$point = $geometries[1];

$this->assertEquals(23.1, $point->latitude);
$this->assertEquals(55.5, $point->longitude);
$this->assertEquals(0, $point->latitude);
$this->assertEquals(0, $point->longitude);

$this->assertDatabaseCount($testPlace->getTable(), 1);
}
Expand All @@ -63,7 +66,7 @@ public function it_stores_geometry_collection_from_geo_json(): void
{
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create([
'geometry_collection' => GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]},{"type":"Point","coordinates":[55.5,23.1]}]}'),
'geometry_collection' => GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]},{"type":"Point","coordinates":[0,0]}]}'),
])->fresh();

$this->assertTrue($testPlace->geometry_collection instanceof GeometryCollection);
Expand All @@ -74,20 +77,22 @@ public function it_stores_geometry_collection_from_geo_json(): void
$lineStrings = $polygon->getGeometries();
$points = $lineStrings[0]->getGeometries();

$this->assertEquals(23.1, $points[0]->latitude);
$this->assertEquals(55.5, $points[0]->longitude);
$this->assertEquals(23.2, $points[1]->latitude);
$this->assertEquals(55.6, $points[1]->longitude);
$this->assertEquals(23.3, $points[2]->latitude);
$this->assertEquals(55.7, $points[2]->longitude);
$this->assertEquals(23.1, $points[3]->latitude);
$this->assertEquals(55.5, $points[3]->longitude);
$this->assertEquals(0, $points[0]->latitude);
$this->assertEquals(0, $points[0]->longitude);
$this->assertEquals(1, $points[1]->latitude);
$this->assertEquals(1, $points[1]->longitude);
$this->assertEquals(2, $points[2]->latitude);
$this->assertEquals(2, $points[2]->longitude);
$this->assertEquals(3, $points[3]->latitude);
$this->assertEquals(3, $points[3]->longitude);
$this->assertEquals(0, $points[4]->latitude);
$this->assertEquals(0, $points[4]->longitude);

/** @var Point $point */
$point = $geometries[1];

$this->assertEquals(23.1, $point->latitude);
$this->assertEquals(55.5, $point->longitude);
$this->assertEquals(0, $point->latitude);
$this->assertEquals(0, $point->longitude);

$this->assertDatabaseCount($testPlace->getTable(), 1);
}
Expand All @@ -97,7 +102,7 @@ public function it_stores_geometry_collection_from_feature_collection_geo_json()
{
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create([
'geometry_collection' => GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]}},{"type":"Feature","geometry":{"type":"Point","coordinates":[55.5,23.1]}}]}'),
'geometry_collection' => GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[0,0]}}]}'),
])->fresh();

$this->assertTrue($testPlace->geometry_collection instanceof GeometryCollection);
Expand All @@ -108,39 +113,67 @@ public function it_stores_geometry_collection_from_feature_collection_geo_json()
$lineStrings = $polygon->getGeometries();
$points = $lineStrings[0]->getGeometries();

$this->assertEquals(23.1, $points[0]->latitude);
$this->assertEquals(55.5, $points[0]->longitude);
$this->assertEquals(23.2, $points[1]->latitude);
$this->assertEquals(55.6, $points[1]->longitude);
$this->assertEquals(23.3, $points[2]->latitude);
$this->assertEquals(55.7, $points[2]->longitude);
$this->assertEquals(23.1, $points[3]->latitude);
$this->assertEquals(55.5, $points[3]->longitude);
$this->assertEquals(0, $points[0]->latitude);
$this->assertEquals(0, $points[0]->longitude);
$this->assertEquals(1, $points[1]->latitude);
$this->assertEquals(1, $points[1]->longitude);
$this->assertEquals(2, $points[2]->latitude);
$this->assertEquals(2, $points[2]->longitude);
$this->assertEquals(3, $points[3]->latitude);
$this->assertEquals(3, $points[3]->longitude);
$this->assertEquals(0, $points[4]->latitude);
$this->assertEquals(0, $points[4]->longitude);

/** @var Point $point */
$point = $geometries[1];

$this->assertEquals(23.1, $point->latitude);
$this->assertEquals(55.5, $point->longitude);
$this->assertEquals(0, $point->latitude);
$this->assertEquals(0, $point->longitude);

$this->assertDatabaseCount($testPlace->getTable(), 1);
}

/** @test */
public function it_generates_multi_polygon_geo_json(): void
public function it_generates_geometry_collection_geo_json(): void
{
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(23.1, 55.5),
new Point(23.2, 55.6),
new Point(23.3, 55.7),
new Point(23.1, 55.5),
new Point(0, 0),
new Point(1, 1),
new Point(2, 2),
new Point(3, 3),
new Point(0, 0),
]),
]),
new Point(23.1, 55.5),
new Point(0, 0),
]);

$this->assertEquals('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[55.5,23.1],[55.6,23.2],[55.7,23.3],[55.5,23.1]]]},{"type":"Point","coordinates":[55.5,23.1]}]}', $geometryCollection->toJson());
$this->assertEquals(
'{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]},{"type":"Point","coordinates":[0,0]}]}',
$geometryCollection->toJson()
);
}

/** @test */
public function it_generates_geometry_collection_feature_collection_json(): void
{
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(0, 0),
new Point(1, 1),
new Point(2, 2),
new Point(3, 3),
new Point(0, 0),
]),
]),
new Point(0, 0),
]);

$this->assertEquals(
'{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,1],[2,2],[3,3],[0,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[0,0]}}]}',
$geometryCollection->toFeatureCollectionJson()
);
}
}
Loading