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
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Geometry classes can be also created by these static methods:

* `fromJson(string $geoJson, int $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
* `fromArray(array $geometry)` - Creates a geometry object from a array. Reverse to `->toArray()` method.
* `fromWkt(string $wkt, int $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).
* `fromWkb(string $wkb, int $srid = 0)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary).

Expand Down
4 changes: 4 additions & 0 deletions src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function set($model, string $key, $value, array $attributes): Expression|
return null;
}

if (is_array($value)) {
$value = Geometry::fromArray($value);
}

if (! ($value instanceof $this->className)) {
$geometryType = is_object($value) ? $value::class : gettype($value);
throw new InvalidArgumentException(
Expand Down
45 changes: 27 additions & 18 deletions src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,35 @@ public static function fromWkt(string $wkt, int $srid = 0): static
return $geometry;
}

/**
* @param string $geoJson
* @param int $srid
* @return static
*
* @throws InvalidArgumentException
*/
public static function fromJson(string $geoJson, int $srid = 0): static
{
$geometry = Factory::parse($geoJson);
$geometry->srid = $srid;

if (! ($geometry instanceof static)) {
throw new InvalidArgumentException(
sprintf('Expected %s, %s given.', static::class, $geometry::class)
);
/**
* @param string $geoJson
* @param int $srid
* @return static
*
* @throws InvalidArgumentException
*/
public static function fromJson(string $geoJson, int $srid = 0): static
{
$geometry = Factory::parse($geoJson);
$geometry->srid = $srid;

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

return $geometry;
}

return $geometry;
}
/**
* @param array<mixed> $geometry
* @return static
*/
public static function fromArray(array $geometry): static
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this method to the API.md

{
return static::fromJson(json_encode($geometry));
}

/**
* @return array<mixed>
Expand Down
26 changes: 13 additions & 13 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@
});

it('creates geometry collection from JSON', function (): void {
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(0, 180),
new Point(1, 179),
new Point(2, 178),
new Point(3, 177),
new Point(0, 180),
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(0, 180),
new Point(1, 179),
new Point(2, 178),
new Point(3, 177),
new Point(0, 180),
]),
]),
]),
new Point(0, 180),
]);
new Point(0, 180),
]);

$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}');
$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}');

expect($geometryCollectionFromJson)->toEqual($geometryCollection);
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
});

it('creates geometry collection with SRID from JSON', function (): void {
Expand Down
45 changes: 45 additions & 0 deletions tests/Objects/GeometryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use MatanYadaev\EloquentSpatial\AxisOrder;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Objects\Polygon;
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;

it('throws exception when generating geometry from other geometry WKB', function (): void {
Expand Down Expand Up @@ -78,3 +81,45 @@
LineString::fromJson($pointJson);
})->toThrow(InvalidArgumentException::class);
});

it('creates geometry collection from array (serialized model)', function (): void {
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(0, 180),
new Point(1, 179),
new Point(2, 178),
new Point(3, 177),
new Point(0, 180),
]),
]),
new Point(0, 180),
]);
$geometryCollectionArray = $geometryCollection->toArray();

$geometryCollectionFromArray = GeometryCollection::fromArray($geometryCollectionArray);

expect($geometryCollectionFromArray)->toEqual($geometryCollection);
});

it('throws exception when generating geometry from wrong array', function (): void {
expect(function (): void {
$geometryCollectionArray = [
'type' => 'Point2',
'coordinates' => [0, 180],
];

Geometry::fromArray($geometryCollectionArray);
})->toThrow(InvalidArgumentException::class);
});

it('throws exception when generating geometry from other geometry array', function (): void {
expect(function (): void {
$geometryCollectionArray = [
'type' => 'Point',
'coordinates' => [0, 180],
];

LineString::fromArray($geometryCollectionArray);
})->toThrow(InvalidArgumentException::class);
});