Skip to content

Commit

Permalink
Merge pull request #37 from atournayre/feature/duration
Browse files Browse the repository at this point in the history
feat(Duration): Milliseconds could be int or float
  • Loading branch information
atournayre authored Oct 2, 2024
2 parents 83f8439 + a5106dd commit 7829e31
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 15 additions & 6 deletions src/Common/VO/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,42 @@ final class Duration

private const HOURS_IN_DAY = 24;

private int $milliseconds;
/**
* @var int|float $milliseconds
*/
private $milliseconds;

private function __construct(int $milliseconds)
/**
* @param $milliseconds int|float
*/
private function __construct($milliseconds)
{
$this->milliseconds = $milliseconds;
}

/**
* @api
* @var int|float $milliseconds
*/
public static function of(int $milliseconds): self
public static function of($milliseconds): self
{
return new self($milliseconds);
}

/**
* @api
* @return int|float
*/
public function asIs(): int
public function asIs()
{
return $this->milliseconds;
}

/**
* @api
* @return int|float
*/
public function milliseconds(): int
public function milliseconds()
{
return $this->milliseconds;
}
Expand Down Expand Up @@ -82,7 +91,7 @@ public function inDays(): float
/**
* @api
*/
public function forHumanReading(string $glue = ' '): string
public function humanReadable(string $glue = ' '): string
{
$days = floor($this->milliseconds / self::MILLISECONDS_IN_SECOND / self::SECONDS_IN_MINUTE / self::MINUTES_IN_HOUR / self::HOURS_IN_DAY);
$hours = floor($this->milliseconds / self::MILLISECONDS_IN_SECOND / self::SECONDS_IN_MINUTE / self::MINUTES_IN_HOUR) % self::HOURS_IN_DAY;
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Common/VO/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class DurationTest extends TestCase
*/
public function dataProvider(): array
{
$test1 = 1 * 24 * 60 * 60 * 1000 // 1 day
$test1 = (float) 1 * 24 * 60 * 60 * 1000 // 1 day
+ 4 * 60 * 60 * 1000 // 4 hours
+ 31 * 60 * 1000 // 31 minutes
+ 12 * 1000 // 12 seconds
Expand Down Expand Up @@ -54,8 +54,8 @@ public function dataProvider(): array
* @dataProvider dataProvider
*/
public function testDuration(
int $milliseconds,
int $expectedMilliseconds,
float $milliseconds,
float $expectedMilliseconds,
float $expectedSeconds,
float $expectedMinutes,
float $expectedHours,
Expand All @@ -70,6 +70,6 @@ public function testDuration(
self::assertEquals($expectedMinutes, $duration->inMinutes());
self::assertEquals($expectedHours, $duration->inHours());
self::assertEquals($expectedDays, $duration->inDays());
self::assertEquals($expectedHuman, $duration->forHumanReading($glueForHumanReading));
self::assertEquals($expectedHuman, $duration->humanReadable($glueForHumanReading));
}
}

0 comments on commit 7829e31

Please sign in to comment.