Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ coverage-report
# Ignore operating system files
.DS_Store
Thumbs.db

composer.lock
23 changes: 16 additions & 7 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use NAL\TimeTracker\Exception\UnknownUnit;
use NAL\TimeTracker\Exception\UnsupportedLogic;

class Result
final class Result
{
/**
* Result Constructor
Expand All @@ -27,14 +27,23 @@ public function __construct(
}

/**
* Formats the calculated time.
* Formats the calculated time using a named-placeholder template.
*
* @param string $format The format string, where `%s` is replaced with the time and unit.
* @return Result
* Supported placeholders:
* - `{time}`: the calculated value
* - `{unit}`: the current unit
*
* @param string $format The template string. Defaults to `{time} {unit}`.
* @return Result A new result instance containing the formatted string.
*/
public function format(string $format = '%s %s'): Result
public function format(string $format = '{time} {unit}'): Result
{
return new self($this->unit, sprintf($format, $this->calculated, $this->lastUpdatedUnit), $this->lastUpdatedUnit);
$replace = [
'{time}' => $this->calculated,
'{unit}' => $this->lastUpdatedUnit,
];

return new self($this->unit, str_replace(array_keys($replace),array_values($replace), $format), $this->lastUpdatedUnit);
}

/**
Expand All @@ -58,7 +67,7 @@ public function get(): float|int|string|null
*/
public function convert(string $unit): Result
{
if (!in_array($unit, $this->unit->getSupportedUnits())) {
if (!in_array($unit, $this->unit->getSupportedUnits(), true)) {
throw new UnknownUnit($unit, $this->unit->getSupportedUnits());
}

Expand Down
65 changes: 65 additions & 0 deletions tests/ResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use NAL\TimeTracker\Exception\UnknownUnit;
use NAL\TimeTracker\Result;
use NAL\TimeTracker\Unit;
use PHPUnit\Framework\TestCase;

class ResultTest extends TestCase
{
public function testFormat(): void
{
$unit = new Unit();
$result = new Result($unit, 123.456, 'ms');

$formatted = $result->format('{time} {unit}');

$this->assertSame('123.456 ms', $formatted->get());
}

public function testConvert(): void
{
$unit = new Unit();
$result = new Result($unit, 1, 's');

$converted = $result->convert('ms');

$this->assertSame(1000, $converted->get());
}

public function testUnknownUnit(): void
{
$this->expectException(UnknownUnit::class);

$unit = new Unit();
$result = new Result($unit, 1, 's');
$result->convert('unknown_unit');
}

public function testConvertOneUnitToAnotherUnit(): void
{
$unit = new Unit();
$result = new Result($unit, 1, 's');

$convertedDivision = $result->convert('ms')->convert('us');
$convertedMultiply = $result->convert('m');

$unit->add('minus', '-', '10');
$unit->add('plus', '+', '10');

$convertedPlus = $result->convert('minus');
$convertedMinus = $result->convert('plus');

$this->assertSame(1000000, $convertedDivision->get());
$this->assertSame(1 / 60, $convertedMultiply->get());
$this->assertSame(-9, $convertedPlus->get());
$this->assertSame(11, $convertedMinus->get());
}

public function testResultToString(): void
{
$result = new Result(new Unit(), 10, 's');

$this->assertSame('10', "$result");
}
}
Loading
Loading