From 7078ba1eaa7bdb37b3e711c835518ba46d0e1c10 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 17 Sep 2024 10:23:55 +0200 Subject: [PATCH] Make CoordinatesCast parameterized --- CHANGELOG.md | 10 ++++++++++ src/Casts/Coordinates96Well.php | 15 --------------- src/Casts/CoordinatesCast.php | 12 +++++++----- tests/Casts/CastsModel.php | 8 ++++---- ...ates96WellTest.php => CoordinatesCastTest.php} | 6 +++--- 5 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 src/Casts/Coordinates96Well.php rename tests/Casts/{Coordinates96WellTest.php => CoordinatesCastTest.php} (75%) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0e7bcb..d579019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,16 @@ See [GitHub releases](https://github.com/mll-lab/laravel-utils/releases). ## Unreleased +## v6.0.0 + +### Changed + +- BREAKING CHANGE: `MLL\LaravelUtils\Casts\CoordinatesCast` requires class-string of `MLL\Utils\Microplate\CoordinateSystem` as a parameter + +### Removed + +- Remove `MLL\LaravelUtils\Casts\Coordinates96Well` + ## v5.8.0 ### Added diff --git a/src/Casts/Coordinates96Well.php b/src/Casts/Coordinates96Well.php deleted file mode 100644 index 2c22142..0000000 --- a/src/Casts/Coordinates96Well.php +++ /dev/null @@ -1,15 +0,0 @@ - */ -class Coordinates96Well extends CoordinatesCast -{ - protected function coordinateSystem(): CoordinateSystem - { - return new CoordinateSystem96Well(); - } -} diff --git a/src/Casts/CoordinatesCast.php b/src/Casts/CoordinatesCast.php index d5abb73..8ba18bd 100644 --- a/src/Casts/CoordinatesCast.php +++ b/src/Casts/CoordinatesCast.php @@ -12,8 +12,13 @@ * * @implements CastsAttributes, Coordinates> */ -abstract class CoordinatesCast implements CastsAttributes +class CoordinatesCast implements CastsAttributes { + public function __construct( + /** @var class-string */ + protected string $coordinateSystemClass, + ) {} + /** * @param Model $model * @param string $key @@ -25,7 +30,7 @@ public function get($model, $key, $value, $attributes): Coordinates { assert(is_string($value)); - return Coordinates::fromString($value, $this->coordinateSystem()); + return Coordinates::fromString($value, new $this->coordinateSystemClass()); } /** @@ -39,7 +44,4 @@ public function set($model, $key, $value, $attributes): string return $value->toString(); } - - /** @return TCoordinateSystem */ - abstract protected function coordinateSystem(): CoordinateSystem; } diff --git a/tests/Casts/CastsModel.php b/tests/Casts/CastsModel.php index 849f544..89eea58 100644 --- a/tests/Casts/CastsModel.php +++ b/tests/Casts/CastsModel.php @@ -3,19 +3,19 @@ namespace MLL\LaravelUtils\Tests\Casts; use Illuminate\Database\Eloquent\Model; -use MLL\LaravelUtils\Casts\Coordinates96Well; +use MLL\LaravelUtils\Casts\CoordinatesCast; use MLL\LaravelUtils\Casts\UnsignedInt; use MLL\Utils\Microplate\Coordinates; -use MLL\Utils\Microplate\CoordinateSystem96Well; +use MLL\Utils\Microplate\CoordinateSystem12x8; /** - * @property Coordinates|null $coordinates + * @property Coordinates|null $coordinates * @property int|null $unsigned_int */ final class CastsModel extends Model { protected $casts = [ - 'coordinates' => Coordinates96Well::class, + 'coordinates' => CoordinatesCast::class . ':' . CoordinateSystem12x8::class, 'unsigned_int' => UnsignedInt::class, ]; diff --git a/tests/Casts/Coordinates96WellTest.php b/tests/Casts/CoordinatesCastTest.php similarity index 75% rename from tests/Casts/Coordinates96WellTest.php rename to tests/Casts/CoordinatesCastTest.php index 4de3138..624e9ea 100644 --- a/tests/Casts/Coordinates96WellTest.php +++ b/tests/Casts/CoordinatesCastTest.php @@ -4,16 +4,16 @@ use MLL\LaravelUtils\Tests\TestCase; use MLL\Utils\Microplate\Coordinates; -use MLL\Utils\Microplate\CoordinateSystem96Well; +use MLL\Utils\Microplate\CoordinateSystem12x8; -final class Coordinates96WellTest extends TestCase +final class CoordinatesCastTest extends TestCase { public function testCast(): void { $model = new CastsModel(); self::assertNull($model->coordinates); - $cast = new Coordinates('A', 2, new CoordinateSystem96Well()); + $cast = new Coordinates('A', 2, new CoordinateSystem12x8()); $raw = 'A2'; $model->coordinates = $cast;