diff --git a/CHANGELOG.md b/CHANGELOG.md index 04b8608..a86b8df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.lock b/composer.lock index 4bb0b5d..4b751f8 100644 --- a/composer.lock +++ b/composer.lock @@ -6600,22 +6600,22 @@ }, { "name": "kubawerlos/php-cs-fixer-custom-fixers", - "version": "v3.14.0", + "version": "v3.15.0", "source": { "type": "git", "url": "https://github.com/kubawerlos/php-cs-fixer-custom-fixers.git", - "reference": "0414fbeada4360510df515b613ccc4055921a3c7" + "reference": "82c08eed3cc42dab45241cfd70e99b94cd54ca7e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/kubawerlos/php-cs-fixer-custom-fixers/zipball/0414fbeada4360510df515b613ccc4055921a3c7", - "reference": "0414fbeada4360510df515b613ccc4055921a3c7", + "url": "https://api.github.com/repos/kubawerlos/php-cs-fixer-custom-fixers/zipball/82c08eed3cc42dab45241cfd70e99b94cd54ca7e", + "reference": "82c08eed3cc42dab45241cfd70e99b94cd54ca7e", "shasum": "" }, "require": { "ext-filter": "*", "ext-tokenizer": "*", - "friendsofphp/php-cs-fixer": "^3.16.0", + "friendsofphp/php-cs-fixer": "^3.19", "php": "^7.4 || ^8.0" }, "require-dev": { @@ -6640,9 +6640,9 @@ "description": "A set of custom fixers for PHP CS Fixer", "support": { "issues": "https://github.com/kubawerlos/php-cs-fixer-custom-fixers/issues", - "source": "https://github.com/kubawerlos/php-cs-fixer-custom-fixers/tree/v3.14.0" + "source": "https://github.com/kubawerlos/php-cs-fixer-custom-fixers/tree/v3.15.0" }, - "time": "2023-04-03T20:47:19+00:00" + "time": "2023-06-26T21:00:04+00:00" }, { "name": "localheinz/diff", diff --git a/src/CoordinateSystem.php b/src/CoordinateSystem.php index c298c10..d64e69d 100644 --- a/src/CoordinateSystem.php +++ b/src/CoordinateSystem.php @@ -2,6 +2,8 @@ namespace Mll\Microplate; +use Illuminate\Support\Arr; + abstract class CoordinateSystem { /** @return list */ @@ -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()); diff --git a/tests/Unit/CoordinateSystemTest.php b/tests/Unit/CoordinateSystemTest.php new file mode 100644 index 0000000..a5254ea --- /dev/null +++ b/tests/Unit/CoordinateSystemTest.php @@ -0,0 +1,32 @@ +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 */ + public static function firstLast(): iterable + { + yield [new CoordinateSystem12Well(), 'A1', 'C4']; + yield [new CoordinateSystem48Well(), 'A1', 'F8']; + yield [new CoordinateSystem96Well(), 'A1', 'H12']; + } +}