Skip to content

Commit

Permalink
feat(box): add JsonSerializable to Box classes
Browse files Browse the repository at this point in the history
Box classes now implement `JsonSerializable` and serialize to float arrays, which is a common format in frontend libraries.

Fixes #36
  • Loading branch information
saibotk committed Jan 3, 2025
1 parent 07c5507 commit 508de05
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `withMagellanCasts()` as EloquentBuilder macro
- Added `AsGeometry` and `AsGeography` database expressions
- Added `fromString()` to `Box` classes to create a box from a string
- Added `JsonSerializable` to `Box2D` and `Box3D`

### Improved

Expand Down
3 changes: 2 additions & 1 deletion src/Data/Boxes/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Clickbar\Magellan\Cast\BBoxCast;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Query\Expression as ExpressionContract;
use JsonSerializable;

abstract class Box implements Castable, ExpressionContract
abstract class Box implements Castable, ExpressionContract, JsonSerializable
{
abstract public static function fromString(string $box): self;

Expand Down
8 changes: 8 additions & 0 deletions src/Data/Boxes/Box2D.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ public static function fromString(string $box): self
floatval($coordinates[4])
);
}

/**
* @return array{float, float, float, float}
*/
public function jsonSerialize(): array
{
return [$this->xMin, $this->yMin, $this->xMax, $this->yMax];
}
}
8 changes: 8 additions & 0 deletions src/Data/Boxes/Box3D.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ public static function fromString(string $box): self
floatval($coordinates[6])
);
}

/**
* @return array{float, float, float, float, float, float}
*/
public function jsonSerialize(): array
{
return [$this->xMin, $this->yMin, $this->zMin, $this->xMax, $this->yMax, $this->zMax];
}
}

0 comments on commit 508de05

Please sign in to comment.