Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Add method AbstractMicroplate::toWellWithCoordinateMapper()
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Jan 28, 2022
1 parent 32f839e commit 0aaf58e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 21 additions & 10 deletions src/AbstractMicroplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
);
}

Expand All @@ -99,33 +99,44 @@ 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;
};
}

/**
* @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<TWell, TCoordinateSystem>
*/
public function toWellWithCoordinateMapper(): callable
{
return fn ($content, string $coordinateString): WellWithCoordinate => new WellWithCoordinate(
$content,
Coordinate::fromString($coordinateString, $this->coordinateSystem)
);
}
}
30 changes: 30 additions & 0 deletions src/WellWithCoordinate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace Mll\Microplate;

/**
* @template TWell
* @template TCoordinateSystem of CoordinateSystem
*/
class WellWithCoordinate
{
/**
* @var TWell
*/
public $content;

/**
* @var Coordinate<TCoordinateSystem>
*/
public Coordinate $coordinate;

/**
* @param TWell $content
* @param Coordinate<TCoordinateSystem> $coordinate
*/
public function __construct($content, Coordinate $coordinate)
{
$this->content = $content;
$this->coordinate = $coordinate;
}
}
19 changes: 19 additions & 0 deletions tests/Unit/MicroplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0aaf58e

Please sign in to comment.