Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CoordinatesCast parameterized #23

Merged
merged 1 commit into from
Sep 17, 2024
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
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
Loading