Skip to content

Commit

Permalink
Make CoordinatesCast parameterized
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Sep 17, 2024
1 parent 45883aa commit 7078ba1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions src/Casts/Coordinates96Well.php

This file was deleted.

12 changes: 7 additions & 5 deletions src/Casts/CoordinatesCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
*
* @implements CastsAttributes<Coordinates<TCoordinateSystem>, Coordinates<TCoordinateSystem>>
*/
abstract class CoordinatesCast implements CastsAttributes
class CoordinatesCast implements CastsAttributes
{
public function __construct(
/** @var class-string<TCoordinateSystem> */
protected string $coordinateSystemClass,
) {}

/**
* @param Model $model
* @param string $key
Expand All @@ -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());
}

/**
Expand All @@ -39,7 +44,4 @@ public function set($model, $key, $value, $attributes): string

return $value->toString();
}

/** @return TCoordinateSystem */
abstract protected function coordinateSystem(): CoordinateSystem;
}
8 changes: 4 additions & 4 deletions tests/Casts/CastsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoordinateSystem96Well>|null $coordinates
* @property Coordinates<CoordinateSystem12x8>|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,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7078ba1

Please sign in to comment.