diff --git a/src/Geometry/Geometry.php b/src/Geometry/Geometry.php index ee238eb..5d2dc08 100644 --- a/src/Geometry/Geometry.php +++ b/src/Geometry/Geometry.php @@ -140,8 +140,16 @@ public function jsonSerialize(): array */ public function toArray(): array { + if (str_starts_with(static::class, __NAMESPACE__)) { + $type = class_basename(static::class); + } else { + $parents = class_parents($this); + $types = array_values(array_filter($parents, fn (string $cl) => str_starts_with($cl, __NAMESPACE__))); + $type = class_basename($types[0]); + } + return [ - 'type' => class_basename(static::class), + 'type' => $type, 'coordinates' => $this->getCoordinates(), ]; } diff --git a/src/LaravelSpatialServiceProvider.php b/src/LaravelSpatialServiceProvider.php index 684c3e9..bb4bfb6 100644 --- a/src/LaravelSpatialServiceProvider.php +++ b/src/LaravelSpatialServiceProvider.php @@ -74,8 +74,7 @@ private function validateConfig(): void } $baseClass = $type->getBaseGeometryClassName(); - /** @phpstan-ignore-next-line */ - if ($configType !== $baseClass && ! $configType instanceof $baseClass) { + if (! is_a($configType, $baseClass, true)) { throw new LaravelSpatialException(sprintf( 'Class for geometry type "%s" should be instance of "%s" ("%s" provided), please check config', $type->value, diff --git a/tests/Custom/CustomPointJsonTest.php b/tests/Custom/CustomPointJsonTest.php new file mode 100644 index 0000000..911e345 --- /dev/null +++ b/tests/Custom/CustomPointJsonTest.php @@ -0,0 +1,22 @@ +toArray(); + + expect($array)->toEqual(['type' => 'Point', 'coordinates' => [180.0, 0.0]]) + ->and(CustomPoint::fromArray($array))->toEqual($point); +}); + +it('check serialisation of point', function (): void { + $point = new Point(0, 180); + + $array = $point->toArray(); + + expect($array)->toEqual(['type' => 'Point', 'coordinates' => [180.0, 0.0]]) + ->and(Point::fromArray($array))->toEqual($point); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 98b5a95..8ce6957 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -13,6 +13,9 @@ function isSupportAxisOrder(): bool return (new Connection())->isSupportAxisOrder(DB::connection()); } +/** + * @return class-string + */ function getDatabaseTruncationClass(): string { if (class_exists(DatabaseTruncation::class)) {