From d253b9e434867f548906cc5b5aee967a70329cb7 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 21 Jan 2022 11:25:40 +0100 Subject: [PATCH] Add methods `AbstractMicroplate::matchRow()` and `AbstractMicroplate::matchColumn()` --- CHANGELOG.md | 6 +++++ composer.json | 5 ++++ composer.lock | 6 ++--- src/AbstractMicroplate.php | 24 +++++++++++++++++++ tests/Unit/MicroplateTest.php | 43 ++++++++++++++++++++++++++++++++++- 5 files changed, 79 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b48da3..4810a98 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.2.0 + +### Added + +- Add methods `AbstractMicroplate::matchRow()` and `AbstractMicroplate::matchColumn()` + ## v3.1.0 ### Added diff --git a/composer.json b/composer.json index 1aa86a1..55933b1 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,11 @@ "thecodingmachine/phpstan-safe-rule": "^1.1" }, "config": { + "allow-plugins": { + "ergebnis/composer-normalize": true, + "infection/extension-installer": true, + "phpstan/extension-installer": true + }, "platform": { "php": "7.4.24" }, diff --git a/composer.lock b/composer.lock index d93bda6..8b9d4dc 100644 --- a/composer.lock +++ b/composer.lock @@ -2060,6 +2060,7 @@ "type": "tidelift" } ], + "abandoned": "symfony/mailer", "time": "2021-10-18T15:26:12+00:00" }, { @@ -6498,9 +6499,6 @@ "require": { "php": "^7.1 || ^8.0" }, - "replace": { - "myclabs/deep-copy": "self.version" - }, "require-dev": { "doctrine/collections": "^1.0", "doctrine/common": "^2.6", @@ -10468,5 +10466,5 @@ "platform-overrides": { "php": "7.4.24" }, - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.2.0" } diff --git a/src/AbstractMicroplate.php b/src/AbstractMicroplate.php index d4ae991..bda294f 100644 --- a/src/AbstractMicroplate.php +++ b/src/AbstractMicroplate.php @@ -104,4 +104,28 @@ public function filledWells(): Collection static fn ($value): bool => self::EMPTY_WELL !== $value ); } + + /** + * @return callable(mixed $value, string $coordinateString): bool + */ + public function matchRow(string $row): callable + { + return function ($value, string $coordinateString) use ($row): bool { + $coordinate = Coordinate::fromString($coordinateString, $this->coordinateSystem); + + return $coordinate->row === $row; + }; + } + + /** + * @return callable(mixed $value, string $coordinateString): bool + */ + public function matchColumn(int $column): callable + { + return function ($value, string $coordinateString) use ($column): bool { + $coordinate = Coordinate::fromString($coordinateString, $this->coordinateSystem); + + return $coordinate->column === $column; + }; + } } diff --git a/tests/Unit/MicroplateTest.php b/tests/Unit/MicroplateTest.php index 6f2e088..8fc8d65 100644 --- a/tests/Unit/MicroplateTest.php +++ b/tests/Unit/MicroplateTest.php @@ -13,7 +13,7 @@ class MicroplateTest extends Framework\TestCase { - public function testCanAddAndRetriedWellBasedOnCoordinateSystem(): void + public function testCanAddAndRetrieveWellBasedOnCoordinateSystem(): void { $coordinateSystem = new CoordinateSystem96Well(); @@ -36,6 +36,47 @@ public function testCanAddAndRetriedWellBasedOnCoordinateSystem(): void $microplate->addWell($coordinateWithOtherCoordinateSystem, 'foo'); } + public function testMatchRow(): void + { + $coordinateSystem = new CoordinateSystem96Well(); + + $microplate = new Microplate($coordinateSystem); + + $content = 'foo'; + $coordinate = new Coordinate('A', 1, $coordinateSystem); + $key = $coordinate->toString(); + $microplate->addWell($coordinate, $content); + + $microplate->addWell(new Coordinate('B', 1, $coordinateSystem), 'bar'); + + $wells = $microplate->wells(); + + $a = $wells->filter($microplate->matchRow('A')); + self::assertCount($coordinateSystem->columnsCount(), $a); + self::assertSame($content, $a[$key]); + + $notA = $wells->reject($microplate->matchRow('A')); + self::assertCount($coordinateSystem->positionsCount() - $coordinateSystem->columnsCount(), $notA); + + $noMatch = $microplate->filledWells() + ->filter($microplate->matchRow('C')); + self::assertCount(0, $noMatch); + } + + public function testMatchColumn(): void + { + $coordinateSystem = new CoordinateSystem96Well(); + + $microplate = new Microplate($coordinateSystem); + + $coordinateColumn1 = (new Coordinate('A', 1, $coordinateSystem))->toString(); + $coordinateColumn2 = (new Coordinate('A', 2, $coordinateSystem))->toString(); + + $matchColumn = $microplate->matchColumn(1); + self::assertTrue($matchColumn('foo', $coordinateColumn1)); + self::assertFalse($matchColumn('foo', $coordinateColumn2)); + } + public function testSortedWells(): void { $microplate = $this->preparePlate();