Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- Fixed using the casts after serialization, e.g. in queueable anonymous event listeners. (thanks @henridv #149)

## [2.0.0](https://github.com/clickbar/laravel-magellan/tree/2.0.0) - 2025-03-19

Please check out the [upgrade guide](UPGRADING.md) for more recommended steps.
Expand Down
14 changes: 13 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,15 @@ public function get($model, string $key, mixed $value, array $attributes)
return null;
}

$geometry = $this->wkbParser->parse($value);
// Detect format: WKB data is hex-encoded, WKT is human-readable text
// After serialization, the model contains the WKB value, and we need to
// parse it back to a Geometry object using the WKBParser.
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);
});