From a5106dde4708732fd2a293449fb4f28460f4220d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Tournayre?= Date: Wed, 2 Oct 2024 18:07:51 +0200 Subject: [PATCH] feat(Duration): Milliseconds could be int or float --- src/Common/VO/Duration.php | 21 +++++++++++++++------ tests/Unit/Common/VO/DurationTest.php | 8 ++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Common/VO/Duration.php b/src/Common/VO/Duration.php index 3ff45a6..065d719 100644 --- a/src/Common/VO/Duration.php +++ b/src/Common/VO/Duration.php @@ -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; } @@ -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; diff --git a/tests/Unit/Common/VO/DurationTest.php b/tests/Unit/Common/VO/DurationTest.php index d0190b2..8203a0c 100644 --- a/tests/Unit/Common/VO/DurationTest.php +++ b/tests/Unit/Common/VO/DurationTest.php @@ -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 @@ -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, @@ -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)); } }