From 0aaf58eb69b39ffbeb6be4264a7062c4b7ed4fee Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 28 Jan 2022 12:12:13 +0100 Subject: [PATCH] Add method `AbstractMicroplate::toWellWithCoordinateMapper()` --- CHANGELOG.md | 6 ++++++ src/AbstractMicroplate.php | 31 +++++++++++++++++++++---------- src/WellWithCoordinate.php | 30 ++++++++++++++++++++++++++++++ tests/Unit/MicroplateTest.php | 19 +++++++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 src/WellWithCoordinate.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ca5e58f..8f713c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## v3.3.0 + +### Added + +- Add method `AbstractMicroplate::toWellWithCoordinateMapper()` + ## v3.2.1 ### Changed diff --git a/src/AbstractMicroplate.php b/src/AbstractMicroplate.php index bda294f..43dcb64 100644 --- a/src/AbstractMicroplate.php +++ b/src/AbstractMicroplate.php @@ -59,9 +59,9 @@ public function sortedWells(FlowDirection $flowDirection): Collection { return $this->wells()->sortBy( /** - * @param TWell $value + * @param TWell $content */ - function ($value, string $key) use ($flowDirection): string { + function ($content, string $key) use ($flowDirection): string { switch ($flowDirection->getValue()) { case FlowDirection::ROW: return $key; @@ -86,9 +86,9 @@ public function freeWells(): Collection { return $this->wells()->filter( /** - * @param TWell $value + * @param TWell $content */ - static fn ($value): bool => self::EMPTY_WELL === $value + static fn ($content): bool => self::EMPTY_WELL === $content ); } @@ -99,18 +99,18 @@ public function filledWells(): Collection { return $this->wells()->filter( /** - * @param TWell $value + * @param TWell $content */ - static fn ($value): bool => self::EMPTY_WELL !== $value + static fn ($content): bool => self::EMPTY_WELL !== $content ); } /** - * @return callable(mixed $value, string $coordinateString): bool + * @return callable(TWell $content, string $coordinateString): bool */ public function matchRow(string $row): callable { - return function ($value, string $coordinateString) use ($row): bool { + return function ($content, string $coordinateString) use ($row): bool { $coordinate = Coordinate::fromString($coordinateString, $this->coordinateSystem); return $coordinate->row === $row; @@ -118,14 +118,25 @@ public function matchRow(string $row): callable } /** - * @return callable(mixed $value, string $coordinateString): bool + * @return callable(TWell $content, string $coordinateString): bool */ public function matchColumn(int $column): callable { - return function ($value, string $coordinateString) use ($column): bool { + return function ($content, string $coordinateString) use ($column): bool { $coordinate = Coordinate::fromString($coordinateString, $this->coordinateSystem); return $coordinate->column === $column; }; } + + /** + * @return callable(TWell $content, string $coordinateString): WellWithCoordinate + */ + public function toWellWithCoordinateMapper(): callable + { + return fn ($content, string $coordinateString): WellWithCoordinate => new WellWithCoordinate( + $content, + Coordinate::fromString($coordinateString, $this->coordinateSystem) + ); + } } diff --git a/src/WellWithCoordinate.php b/src/WellWithCoordinate.php new file mode 100644 index 0000000..0bc8198 --- /dev/null +++ b/src/WellWithCoordinate.php @@ -0,0 +1,30 @@ + + */ + public Coordinate $coordinate; + + /** + * @param TWell $content + * @param Coordinate $coordinate + */ + public function __construct($content, Coordinate $coordinate) + { + $this->content = $content; + $this->coordinate = $coordinate; + } +} diff --git a/tests/Unit/MicroplateTest.php b/tests/Unit/MicroplateTest.php index a90aea3..adf2b40 100644 --- a/tests/Unit/MicroplateTest.php +++ b/tests/Unit/MicroplateTest.php @@ -9,6 +9,7 @@ use Mll\Microplate\Enums\FlowDirection; use Mll\Microplate\Exceptions\MicroplateIsFullException; use Mll\Microplate\Microplate; +use Mll\Microplate\WellWithCoordinate; use PHPUnit\Framework; class MicroplateTest extends Framework\TestCase @@ -77,6 +78,24 @@ public function testMatchColumn(): void self::assertFalse($matchColumn('foo', $coordinateColumn2)); } + public function testtoWellWithCoordinateMapper(): void + { + $coordinateSystem = new CoordinateSystem96Well(); + $microplate = new Microplate($coordinateSystem); + + $coordinate = new Coordinate('A', 1, $coordinateSystem); + $content = 'foo'; + $microplate->addWell($coordinate, $content); + + $wellWithCoordinate = $microplate->wells() + ->map($microplate->toWellWithCoordinateMapper()) + ->first(); + + self::assertInstanceOf(WellWithCoordinate::class, $wellWithCoordinate); + self::assertSame($content, $wellWithCoordinate->content); + self::assertEquals($coordinate, $wellWithCoordinate->coordinate); + } + public function testSortedWells(): void { $microplate = $this->preparePlate();