Skip to content

Commit

Permalink
feat!(box): implement Stringable interface
Browse files Browse the repository at this point in the history
This properly adds `Stringable` to `Box` classes, so they can be printed etc.
We still keep an extra function to create the string, because it concerns the database and should not be tied to the method that converts the object to a string in general.
  • Loading branch information
saibotk committed Jan 3, 2025
1 parent f18a3e7 commit 2a766b7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed automatic SRID transformation
- Removed st prefixed builder functions (e.g. `stSelect`, `stWhere`, ...)
- Removed `GeometryType` Enum
- Renamed `toString` to `toSqlString` on `Box` classes

### Added

Expand All @@ -26,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `AsGeometry` and `AsGeography` database expressions
- Added `fromString()` to `Box` classes to create a box from a string
- Added `JsonSerializable` to `Box2D` and `Box3D`
- Added `Stringable` interface to all box classes

### Improved

Expand Down
2 changes: 1 addition & 1 deletion src/Cast/BBoxCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public function get($model, string $key, mixed $value, array $attributes)
*/
public function set($model, string $key, mixed $value, array $attributes)
{
return $value->toString();
return $value->toSqlString();
}
}
5 changes: 3 additions & 2 deletions src/Data/Boxes/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Query\Expression as ExpressionContract;
use JsonSerializable;
use Stringable;

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

abstract public function toString(): string;
abstract public function toSqlString(): string;

/**
* @return BBoxCast<Box>
Expand Down
9 changes: 7 additions & 2 deletions src/Data/Boxes/Box2D.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ public function getYMax(): float
return $this->yMax;
}

public function toString(): string
public function toSqlString(): string
{
$min = GeometryHelper::stringifyFloat($this->xMin).' '.GeometryHelper::stringifyFloat($this->yMin);
$max = GeometryHelper::stringifyFloat($this->xMax).' '.GeometryHelper::stringifyFloat($this->yMax);

return "BOX({$min},{$max})";
}

public function __toString(): string
{
return $this->toSqlString();
}

public function getValue(Grammar $grammar): string
{
return $grammar->quoteString($this->toString()).'::box2d';
return $grammar->quoteString($this->toSqlString()).'::box2d';
}

public static function fromString(string $box): self
Expand Down
9 changes: 7 additions & 2 deletions src/Data/Boxes/Box3D.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ public function getZMax(): float
return $this->zMax;
}

public function toString(): string
public function toSqlString(): string
{
$min = GeometryHelper::stringifyFloat($this->xMin).' '.GeometryHelper::stringifyFloat($this->yMin).' '.GeometryHelper::stringifyFloat($this->zMin);
$max = GeometryHelper::stringifyFloat($this->xMax).' '.GeometryHelper::stringifyFloat($this->yMax).' '.GeometryHelper::stringifyFloat($this->zMax);

return "BOX3D({$min},{$max})";
}

public function __toString(): string
{
return $this->toSqlString();
}

public function getValue(Grammar $grammar): string
{
return $grammar->quoteString($this->toString()).'::box3d';
return $grammar->quoteString($this->toSqlString()).'::box3d';
}

public static function fromString(string $box): self
Expand Down

0 comments on commit 2a766b7

Please sign in to comment.