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

Commit

Permalink
Add CoordinateSystem::first() and CoordinateSystem::last()
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Jun 27, 2023
1 parent 713c3eb commit 4c34d7c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v6.2.0

### Added

- Add `CoordinateSystem::first()` and `CoordinateSystem::last()`

## v6.1.0

### Added
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/CoordinateSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Mll\Microplate;

use Illuminate\Support\Arr;

abstract class CoordinateSystem
{
/** @return list<string> */
Expand Down Expand Up @@ -74,6 +76,46 @@ public function all(): iterable
}
}

/**
* Returns the coordinates of the first row and first column.
*
* @return Coordinates<$this>
*/
public function first(): Coordinates
{
$firstRow = Arr::first($this->rows());
if (! is_string($firstRow)) {
throw new \Exception('First row must be string.');
}

$firstColumn = Arr::first($this->columns());
if (! is_int($firstColumn)) {
throw new \Exception('First column must be string.');
}

return new Coordinates($firstRow, $firstColumn, $this);
}

/**
* Returns the coordinates of the last row and last column.
*
* @return Coordinates<$this>
*/
public function last(): Coordinates
{
$lastRow = Arr::last($this->rows());
if (! is_string($lastRow)) {
throw new \Exception('Last row must be string.');
}

$lastColumn = Arr::last($this->columns());
if (! is_int($lastColumn)) {
throw new \Exception('Last column must be string.');
}

return new Coordinates($lastRow, $lastColumn, $this);
}

public function rowsCount(): int
{
return count($this->rows());
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/CoordinateSystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace Mll\Microplate\Tests\Unit;

use Mll\Microplate\CoordinateSystem;
use Mll\Microplate\CoordinateSystem12Well;
use Mll\Microplate\CoordinateSystem48Well;
use Mll\Microplate\CoordinateSystem96Well;
use PHPUnit\Framework\TestCase;

final class CoordinateSystemTest extends TestCase
{
/** @dataProvider firstLast */
public function testFirstLast(CoordinateSystem $coordinateSystem, string $expectedFirst, string $expectedLast): void
{
$actualFirst = $coordinateSystem->first();
self::assertSame($expectedFirst, $actualFirst->toString());
self::assertSame($coordinateSystem, $actualFirst->coordinateSystem);

$actualLast = $coordinateSystem->last();
self::assertSame($expectedLast, $actualLast->toString());
self::assertSame($coordinateSystem, $actualLast->coordinateSystem);
}

/** @return iterable<array{CoordinateSystem, string, string}> */
public static function firstLast(): iterable
{
yield [new CoordinateSystem12Well(), 'A1', 'C4'];
yield [new CoordinateSystem48Well(), 'A1', 'F8'];
yield [new CoordinateSystem96Well(), 'A1', 'H12'];
}
}

0 comments on commit 4c34d7c

Please sign in to comment.