Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion src/Cast/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Clickbar\Magellan\IO\Generator\BaseGenerator;
use Clickbar\Magellan\IO\Generator\WKT\WKTGenerator;
use Clickbar\Magellan\IO\Parser\WKB\WKBParser;
use Clickbar\Magellan\IO\Parser\WKT\WKTParser;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
Expand All @@ -21,13 +22,16 @@ class GeometryCast implements CastsAttributes
{
protected WKBParser $wkbParser;

protected WKTParser $wktParser;

protected BaseGenerator $sqlGenerator;

/** @param class-string<T> $geometryClass */
public function __construct(
protected string $geometryClass,
) {
$this->wkbParser = App::make(WKBParser::class);
$this->wktParser = App::make(WKTParser::class);

$generatorClass = config('magellan.sql_generator', WKTGenerator::class);
$this->sqlGenerator = new $generatorClass;
Expand All @@ -45,7 +49,13 @@ public function get($model, string $key, mixed $value, array $attributes)
return null;
}

$geometry = $this->wkbParser->parse($value);
// determine if value is a HEX string
if (ctype_xdigit($value)) {
$geometry = $this->wkbParser->parse($value);
} else {
$geometry = $this->wktParser->parse($value);
}

$this->assertGeometryType($geometry);

return $geometry;
Expand Down
20 changes: 20 additions & 0 deletions tests/Models/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,23 @@
expect($nearbyLocations->pluck('name'))->toContain('Berlin', 'Hamburg');
expect($nearbyLocations->pluck('name'))->not->toContain('Munich');
});

test('it can access a point after serializing the model', function () {
$location = Location::make([
'name' => 'Test Location',
'location' => Point::makeGeodetic(51.087, 8.76),
]);
$location->save();

serialize($location);

expect($location->location)->toBeInstanceOf(Point::class);
expect($location->location->getLatitude())->toBe(51.087);
expect($location->location->getLongitude())->toBe(8.76);

// Test after fresh retrieval
$location = $location->fresh();
expect($location->location)->toBeInstanceOf(Point::class);
expect($location->location->getLatitude())->toBe(51.087);
expect($location->location->getLongitude())->toBe(8.76);
});
Loading