Skip to content

Commit 3590c4e

Browse files
committed
fix toArray for custom Geometry
1 parent 244c3dc commit 3590c4e

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

src/Geometry/Geometry.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static function fromJson(string $geoJson, int $srid = 0): static
112112
}
113113

114114
/**
115-
* @param array<mixed> $geometry
115+
* @param array<string, mixed> $geometry
116116
*
117117
* @throws LaravelSpatialJsonException
118118
*/
@@ -128,20 +128,29 @@ public static function fromArray(array $geometry): static
128128
}
129129

130130
/**
131-
* @return array<mixed>
131+
* @return array<string, mixed>
132132
*/
133133
public function jsonSerialize(): array
134134
{
135135
return $this->toArray();
136136
}
137137

138138
/**
139-
* @return array{type: string, coordinates: array<mixed>}
139+
* @return array{type: string, coordinates: array<string, mixed>}
140140
*/
141141
public function toArray(): array
142142
{
143+
$ns = str_replace('\Geometry\Geometry', '\Geometry', __CLASS__);
144+
145+
if (str_starts_with(static::class, $ns)) {
146+
$type = class_basename(static::class);
147+
} else {
148+
$types = array_values(array_filter(class_parents($this), fn (string $cl) => str_starts_with($cl, $ns)));
149+
$type = class_basename($types[0]);
150+
}
151+
143152
return [
144-
'type' => class_basename(static::class),
153+
'type' => $type,
145154
'coordinates' => $this->getCoordinates(),
146155
];
147156
}
@@ -173,7 +182,7 @@ public function toFeatureCollectionJson(): string
173182
}
174183

175184
/**
176-
* @return array<mixed>
185+
* @return array{0: float, 1: float}
177186
*/
178187
abstract public function getCoordinates(): array;
179188

src/Geometry/GeometryCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getWktData(): string
5151
}
5252

5353
/**
54-
* @return array<mixed>
54+
* @return array<int, array{0: float, 1: float}>
5555
*/
5656
public function getCoordinates(): array
5757
{
@@ -61,7 +61,7 @@ public function getCoordinates(): array
6161
}
6262

6363
/**
64-
* @return array<mixed>
64+
* @return array<string, mixed>
6565
*/
6666
public function toArray(): array
6767
{

src/LaravelSpatialServiceProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ private function validateConfig(): void
7474
}
7575

7676
$baseClass = $type->getBaseGeometryClassName();
77-
/** @phpstan-ignore-next-line */
78-
if ($configType !== $baseClass && ! $configType instanceof $baseClass) {
77+
if (! is_a($configType, $baseClass, true)) {
7978
throw new LaravelSpatialException(sprintf(
8079
'Class for geometry type "%s" should be instance of "%s" ("%s" provided), please check config',
8180
$type->value,

tests/Custom/CustomPointJsonTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use ASanikovich\LaravelSpatial\Geometry\Point;
4+
use ASanikovich\LaravelSpatial\Tests\Custom\CustomPoint;
5+
6+
it('check serialisation of custom point', function (): void {
7+
$point = new CustomPoint(0, 180);
8+
9+
$array = $point->toArray();
10+
11+
expect($array)->toEqual(['type' => 'Point', 'coordinates' => [180.0, 0.0]])
12+
->and(CustomPoint::fromArray($array))->toEqual($point);
13+
});
14+
15+
it('check serialisation of point', function (): void {
16+
$point = new Point(0, 180);
17+
18+
$array = $point->toArray();
19+
20+
expect($array)->toEqual(['type' => 'Point', 'coordinates' => [180.0, 0.0]])
21+
->and(Point::fromArray($array))->toEqual($point);
22+
});

tests/Pest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function isSupportAxisOrder(): bool
1313
return (new Connection())->isSupportAxisOrder(DB::connection());
1414
}
1515

16+
/**
17+
* @return class-string
18+
*/
1619
function getDatabaseTruncationClass(): string
1720
{
1821
if (class_exists(DatabaseTruncation::class)) {

0 commit comments

Comments
 (0)