Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/Geometry/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
}
Expand Down
3 changes: 1 addition & 2 deletions src/LaravelSpatialServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions tests/Custom/CustomPointJsonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use ASanikovich\LaravelSpatial\Geometry\Point;
use ASanikovich\LaravelSpatial\Tests\Custom\CustomPoint;

it('check serialisation of custom point', function (): void {
$point = new CustomPoint(0, 180);

$array = $point->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);
});
3 changes: 3 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function isSupportAxisOrder(): bool
return (new Connection())->isSupportAxisOrder(DB::connection());
}

/**
* @return class-string
*/
function getDatabaseTruncationClass(): string
{
if (class_exists(DatabaseTruncation::class)) {
Expand Down