Skip to content

Commit 87ef448

Browse files
author
Matan Yadaev
committed
pr fixes
1 parent 904a0a8 commit 87ef448

File tree

4 files changed

+66
-75
lines changed

4 files changed

+66
-75
lines changed

API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
Geometry classes can be also created by these static methods:
1414

15+
* `fromArray(array $geometry)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array.
1516
* `fromJson(string $geoJson, int $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
16-
* `fromArray(array $geometry)` - Creates a geometry object from a array. Reverse to `->toArray()` method.
1717
* `fromWkt(string $wkt, int $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).
1818
* `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).
1919

src/Objects/Geometry.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,36 +106,40 @@ public static function fromWkt(string $wkt, int $srid = 0): static
106106
return $geometry;
107107
}
108108

109-
/**
110-
* @param string $geoJson
111-
* @param int $srid
112-
* @return static
113-
*
114-
* @throws InvalidArgumentException
115-
*/
116-
public static function fromJson(string $geoJson, int $srid = 0): static
117-
{
118-
$geometry = Factory::parse($geoJson);
119-
$geometry->srid = $srid;
120-
121-
if (! ($geometry instanceof static)) {
122-
throw new InvalidArgumentException(
123-
sprintf('Expected %s, %s given.', static::class, $geometry::class)
124-
);
125-
}
126-
127-
return $geometry;
128-
}
109+
/**
110+
* @param string $geoJson
111+
* @param int $srid
112+
* @return static
113+
*
114+
* @throws InvalidArgumentException
115+
*/
116+
public static function fromJson(string $geoJson, int $srid = 0): static
117+
{
118+
$geometry = Factory::parse($geoJson);
119+
$geometry->srid = $srid;
129120

130-
/**
131-
* @param array<mixed> $geometry
132-
* @return static
133-
*/
134-
public static function fromArray(array $geometry): static
135-
{
136-
return static::fromJson(json_encode($geometry));
121+
if (! ($geometry instanceof static)) {
122+
throw new InvalidArgumentException(
123+
sprintf('Expected %s, %s given.', static::class, $geometry::class)
124+
);
137125
}
138126

127+
return $geometry;
128+
}
129+
130+
/**
131+
* @param array<mixed> $geometry
132+
* @return static
133+
*
134+
* @throws JsonException
135+
*/
136+
public static function fromArray(array $geometry): static
137+
{
138+
$geoJson = json_encode($geometry, JSON_THROW_ON_ERROR);
139+
140+
return static::fromJson($geoJson);
141+
}
142+
139143
/**
140144
* @return array<mixed>
141145
*/

tests/Objects/GeometryCollectionTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,22 @@
5252
});
5353

5454
it('creates geometry collection from JSON', function (): void {
55-
$geometryCollection = new GeometryCollection([
56-
new Polygon([
57-
new LineString([
58-
new Point(0, 180),
59-
new Point(1, 179),
60-
new Point(2, 178),
61-
new Point(3, 177),
62-
new Point(0, 180),
63-
]),
55+
$geometryCollection = new GeometryCollection([
56+
new Polygon([
57+
new LineString([
58+
new Point(0, 180),
59+
new Point(1, 179),
60+
new Point(2, 178),
61+
new Point(3, 177),
62+
new Point(0, 180),
6463
]),
65-
new Point(0, 180),
66-
]);
64+
]),
65+
new Point(0, 180),
66+
]);
6767

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

70-
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
70+
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
7171
});
7272

7373
it('creates geometry collection with SRID from JSON', function (): void {

tests/Objects/GeometryTest.php

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
use Illuminate\Support\Facades\DB;
55
use MatanYadaev\EloquentSpatial\AxisOrder;
66
use MatanYadaev\EloquentSpatial\Objects\Geometry;
7-
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
87
use MatanYadaev\EloquentSpatial\Objects\LineString;
98
use MatanYadaev\EloquentSpatial\Objects\Point;
10-
use MatanYadaev\EloquentSpatial\Objects\Polygon;
119
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;
1210

1311
it('throws exception when generating geometry from other geometry WKB', function (): void {
@@ -82,44 +80,33 @@
8280
})->toThrow(InvalidArgumentException::class);
8381
});
8482

85-
it('creates geometry collection from array (serialized model)', function (): void {
86-
$geometryCollection = new GeometryCollection([
87-
new Polygon([
88-
new LineString([
89-
new Point(0, 180),
90-
new Point(1, 179),
91-
new Point(2, 178),
92-
new Point(3, 177),
93-
new Point(0, 180),
94-
]),
95-
]),
96-
new Point(0, 180),
97-
]);
98-
$geometryCollectionArray = $geometryCollection->toArray();
99-
100-
$geometryCollectionFromArray = GeometryCollection::fromArray($geometryCollectionArray);
101-
102-
expect($geometryCollectionFromArray)->toEqual($geometryCollection);
83+
it('creates a geometry object from a geo json array', function (): void {
84+
$point = new Point(0, 180);
85+
$pointGeoJsonArray = $point->toArray();
86+
87+
$geometryCollectionFromArray = Point::fromArray($pointGeoJsonArray);
88+
89+
expect($geometryCollectionFromArray)->toEqual($point);
10390
});
10491

105-
it('throws exception when generating geometry from wrong array', function (): void {
106-
expect(function (): void {
107-
$geometryCollectionArray = [
108-
'type' => 'Point2',
109-
'coordinates' => [0, 180],
110-
];
92+
it('throws exception when creating a geometry object from an invalid geo json array', function (): void {
93+
$invalidPointGeoJsonArray = [
94+
'type' => 'InvalidGeometryType',
95+
'coordinates' => [0, 180],
96+
];
11197

112-
Geometry::fromArray($geometryCollectionArray);
98+
expect(function () use ($invalidPointGeoJsonArray): void {
99+
Geometry::fromArray($invalidPointGeoJsonArray);
113100
})->toThrow(InvalidArgumentException::class);
114101
});
115102

116-
it('throws exception when generating geometry from other geometry array', function (): void {
117-
expect(function (): void {
118-
$geometryCollectionArray = [
119-
'type' => 'Point',
120-
'coordinates' => [0, 180],
121-
];
103+
it('throws exception when creating a geometry object from another geometry geo json array', function (): void {
104+
$pointGeoJsonArray = [
105+
'type' => 'Point',
106+
'coordinates' => [0, 180],
107+
];
122108

123-
LineString::fromArray($geometryCollectionArray);
109+
expect(function () use ($pointGeoJsonArray): void {
110+
LineString::fromArray($pointGeoJsonArray);
124111
})->toThrow(InvalidArgumentException::class);
125112
});

0 commit comments

Comments
 (0)