Skip to content

Commit 1383425

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

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

src/Geometry/Geometry.php

Lines changed: 12 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,27 @@ 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+
if (get_parent_class($this) === self::class) {
144+
$type = class_basename(static::class);
145+
} else {
146+
$types = array_values(class_parents($this));
147+
$type = class_basename($types[count($types) - 2]);
148+
}
149+
143150
return [
144-
'type' => class_basename(static::class),
151+
'type' => $type,
145152
'coordinates' => $this->getCoordinates(),
146153
];
147154
}
@@ -173,7 +180,7 @@ public function toFeatureCollectionJson(): string
173180
}
174181

175182
/**
176-
* @return array<mixed>
183+
* @return array{0: float, 1: float}
177184
*/
178185
abstract public function getCoordinates(): array;
179186

src/Geometry/GeometryCollection.php

Lines changed: 3 additions & 3 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
{
@@ -70,7 +70,7 @@ public function toArray(): array
7070
}
7171

7272
return [
73-
'type' => class_basename(static::class),
73+
'type' => class_basename(self::class),
7474
'geometries' => $this->geometries->map(static fn (Geometry $geometry): array => $geometry->toArray()),
7575
];
7676
}

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)