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

Commit

Permalink
Add Coordinate#toPaddedString()
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Jun 23, 2022
1 parent e051eec commit 63bd937
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 44 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.5.0

### Added

- Add `Coordinate#toPaddedString()`

## v3.4.0

### Added
Expand Down
34 changes: 17 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"name": "mll-lab/microplate",
"type": "library",
"description": "PHP package to easily work with microplate data",
"homepage": "https://github.com/mll-lab/microplate",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Simon Bigelmayr",
"email": "[email protected]"
}
],
"homepage": "https://github.com/mll-lab/microplate",
"support": {
"issues": "https://github.com/mll-lab/microplate/issues",
"source": "https://github.com/mll-lab/microplate"
},
"require": {
"php": "^7.4 || ^8",
"illuminate/support": "^7 || ^8",
Expand All @@ -34,18 +38,6 @@
"symfony/var-dumper": "^5",
"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"
},
"preferred-install": "dist",
"sort-packages": true
},
"autoload": {
"psr-4": {
"Mll\\Microplate\\": "src/"
Expand All @@ -59,8 +51,16 @@
"vendor/symfony/var-dumper/Resources/functions/dump.php"
]
},
"support": {
"issues": "https://github.com/mll-lab/microplate/issues",
"source": "https://github.com/mll-lab/microplate"
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"infection/extension-installer": true,
"phpstan/extension-installer": true
},
"platform": {
"php": "7.4.24"
},
"preferred-install": "dist",
"sort-packages": true
}
}
48 changes: 24 additions & 24 deletions composer.lock

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

8 changes: 8 additions & 0 deletions src/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public function toString(): string
return $this->row . $this->column;
}

/**
* Format the coordinate with the column 0-padded so all are the same length.
*/
public function toPaddedString(): string
{
return $this->row . $this->coordinateSystem->padColumn($this->column);
}

/**
* @param TCoordinateSystem $coordinateSystem
*
Expand Down
18 changes: 15 additions & 3 deletions src/CoordinateSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ abstract public function columns(): array;
*/
public function paddedColumns(): array
{
$maxColumnLength = strlen((string) $this->columnsCount());

$paddedColumns = [];
foreach ($this->columns() as $column) {
$paddedColumns[] = str_pad((string) $column, $maxColumnLength, '0', STR_PAD_LEFT);
$paddedColumns[] = $this->padColumn($column);
}

return $paddedColumns;
}

/**
* 0-pad column to be as long as the longest column in the coordinate system.
*/
public function padColumn(int $column): string
{
$maxColumnLength = strlen((string) $this->columnsCount());

return str_pad((string) $column, $maxColumnLength, '0', STR_PAD_LEFT);
}

public function rowForRowFlowPosition(int $position): string
{
return $this->rows()[floor(($position - 1) / $this->columnsCount())];
Expand All @@ -57,6 +65,10 @@ public function positionsCount(): int
}

/**
* Returns all possible coordinates of the system, ordered by column then row.
*
* e.g. A1, A2, B1, B2
*
* @return iterable<int, Coordinate>
*/
public function all(): iterable
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/CoordinateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function testFromPaddedCoordinatesString(string $paddedCoordinate, string
$coordinateFromPadded = Coordinate::fromString($paddedCoordinate, new CoordinateSystem96Well());
self::assertSame($row, $coordinateFromPadded->row);
self::assertSame($column, $coordinateFromPadded->column);
self::assertSame($paddedCoordinate, $coordinateFromPadded->toPaddedString());
}

/**
Expand Down

0 comments on commit 63bd937

Please sign in to comment.