Skip to content

feat: add time of day object #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
### MacOs ###
.DS_Store

### PhpStorm ###
/.idea

### Composer ###
composer.lock
composer.phar
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"files": [ "src/classnames/function.php" ],
"psr-4": {
"ClassNames\\": "src/classnames",
"MergeCoverage\\": "src/merge-coverage"
"MergeCoverage\\": "src/merge-coverage",
"TimeOfDay\\": "src/time-of-day"
}
},
"require-dev": {
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<testsuite name="classnames">
<directory>src/classnames/Tests</directory>
</testsuite>
<testsuite name="time-of-day">
<directory>src/time-of-day/Tests</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
Expand Down
56 changes: 56 additions & 0 deletions src/time-of-day/AbstractTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace TimeOfDay;

abstract class AbstractTime
{
const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
const MILLISECONDS_IN_A_HOUR = 60 * 60 * 1000;
const MILLISECONDS_IN_A_MINUTE = 60 * 1000;

protected int $counter;

public static function fromParts(
int $hours,
?int $minutes = 0,
?int $seconds = 0,
?int $milliseconds = 0
) {
match(true) {
(0 > $hours || $hours > 23) => throw new \ValueError('Hour is not valid'),
(0 > $minutes || $minutes > 59) => throw new \ValueError('Minute is not valid'),
(0 > $seconds || $seconds > 59) => throw new \ValueError('Second is not valid'),
(0 > $milliseconds || $milliseconds > 999) => throw new \ValueError('Millisecond is not valid'),
default => 'Everything is ok'
};

return new static(
$hours * self::MILLISECONDS_IN_A_HOUR
+ $minutes * self::MILLISECONDS_IN_A_MINUTE
+ $seconds * 1000
+ $milliseconds
);
}

protected function hours(): int
{
return intdiv($this->counter, self::MILLISECONDS_IN_A_HOUR);
}

protected function minutes(): int
{
$rest = $this->counter % self::MILLISECONDS_IN_A_HOUR;
return intdiv($rest, self::MILLISECONDS_IN_A_MINUTE);
}

protected function seconds(): int
{
$rest = $this->counter % self::MILLISECONDS_IN_A_MINUTE;
return intdiv($rest, 1000);
}

protected function milliseconds(): int
{
return $this->counter % 1000;
}
}
29 changes: 29 additions & 0 deletions src/time-of-day/Duration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace TimeOfDay;

class Duration extends AbstractTime
{
public function __construct(int $counter)
{
if (0 > $counter) {
throw new \ValueError('Milliseconds should be a positive integer');
}
$this->counter = $counter;
}

public function equals(Duration $other): bool
{
return $this->counter === $other->counter;
}

public function isGreater(Duration $other): bool
{
return $this->counter > $other->counter;
}

public function isLess(Duration $other): bool
{
return $this->counter < $other->counter;
}
}
93 changes: 93 additions & 0 deletions src/time-of-day/Tests/DurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);

namespace TimeOfDay\Tests;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;
use TimeOfDay\Duration;
use TimeOfDay\TimeOfDay;

class DurationTest extends TestCase
{
#[Test]
public function fromParts(): void
{
$result = Duration::fromParts(12);
$expected = new Duration(12 * 60 * 60 * 1000);
self::assertEquals($expected, $result);

$result = Duration::fromParts(12,34);
$expected = new Duration(12 * 60 * 60 * 1000 + 34 * 60 * 1000);
self::assertEquals($expected, $result);

$result = Duration::fromParts(12, 34 , 56 );
$expected = new Duration(12 * 60 * 60 * 1000 + 34 * 60 * 1000 + 56 * 1000);
self::assertEquals($expected, $result);

$result = Duration::fromParts(0);
$expected = new Duration(0);
self::assertEquals($expected, $result);
}

#[Test]
public function hourNotValidTooBig(): void
{
self::expectException(\ValueError::class);
self::expectExceptionMessage('Hour is not valid');
Duration::fromParts(24);
}

#[Test]
public function hourNotValidTooSmall(): void
{
self::expectException(\ValueError::class);
self::expectExceptionMessage('Hour is not valid');
TimeOfDay::fromParts(-3);
}

#[Test]
public function minuteNotValid(): void
{
self::expectException(\ValueError::class);
self::expectExceptionMessage('Minute is not valid');
TimeOfDay::fromParts(12, 60);
}

#[Test]
public function secondNotValid(): void
{
self::expectException(\ValueError::class);
self::expectExceptionMessage('Second is not valid');
TimeOfDay::fromParts(12, 34 , 60);
}

#[Test]
public function comparison(): void
{
$foo = Duration::fromParts(12, 34 , 56);
$bar = Duration::fromParts(12);

self::assertTrue($foo->isGreater($bar));
self::assertTrue($bar->isLess($foo));
}

#[Test]
public function equalsComparison(): void
{
$foo = Duration::fromParts(12, 34 , 0);
$bar = Duration::fromParts(12, 34);

self::assertTrue($foo->equals($bar));
}

#[Test]
public function equalsComparisonBetweenChildrenObjects(): void
{
$foo = Duration::fromParts(12, 34 , 0);
$bar = TimeOfDay::fromParts(12, 34);

self::expectException(\TypeError::class);
self::assertTrue($foo->equals($bar));
}
}
140 changes: 140 additions & 0 deletions src/time-of-day/Tests/TimeOfDayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
declare(strict_types=1);

namespace TimeOfDay\Tests;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;
use TimeOfDay\TimeOfDay;
use TimeOfDay\Duration;

class TimeOfDayTest extends TestCase
{
#[Test]
public function timeOfDay(): void
{
$result = new TimeOfDay(45296000);
$expected = new \DateTimeImmutable('2000-01-01 12:34:56');

self::assertSame($expected->format('H:i:s'), $result->format('H:i:s'));
}

#[Test]
public function plop(): void
{
$time = new TimeOfDay(45296000);
$result = $time->onDay(new \DateTimeImmutable('2000-01-01'));

$expected = new \DateTimeImmutable('2000-01-01 12:34:56');

self::assertEquals($expected, $result);
}

#[Test]
public function negativeMilliseconds(): void
{
$result = new TimeOfDay(-45296000);
self::assertEquals(new TimeOfDay(41104000), $result);

$result = new TimeOfDay(-131696000);
self::assertEquals(new TimeOfDay(41104000), $result);
}

#[Test]
public function comparison(): void
{
$foo = TimeOfDay::fromParts(12, 34 , 56);
$bar = TimeOfDay::fromParts(12);

self::assertTrue($foo->isAfter($bar));
self::assertTrue($bar->isBefore($foo));
}

#[Test]
public function equalsComparison(): void
{
$foo = TimeOfDay::fromParts(12, 34 , 0);
$bar = TimeOfDay::fromParts(12, 34);

self::assertTrue($foo->equals($bar));
}

#[Test]
public function fromParts(): void
{
$result = TimeOfDay::fromParts(12);
$expected = new TimeOfDay(12 * 60 * 60 * 1000);
self::assertEquals($expected, $result);

$result = TimeOfDay::fromParts(12,34);
$expected = new TimeOfDay(12 * 60 * 60 * 1000 + 34 * 60 * 1000);
self::assertEquals($expected, $result);

$result = TimeOfDay::fromParts(12, 34 , 56 );
$expected = new TimeOfDay(12 * 60 * 60 * 1000 + 34 * 60 * 1000 + 56 * 1000);
self::assertEquals($expected, $result);

$result = TimeOfDay::fromParts(0);
$expected = new TimeOfDay(0);
self::assertEquals($expected, $result);
}

#[Test]
public function hourNotValidTooBig(): void
{
self::expectException(\ValueError::class);
self::expectExceptionMessage('Hour is not valid');
TimeOfDay::fromParts(24);
}

#[Test]
public function hourNotValidTooSmall(): void
{
self::expectException(\ValueError::class);
self::expectExceptionMessage('Hour is not valid');
TimeOfDay::fromParts(-3);
}

#[Test]
public function minuteNotValid(): void
{
self::expectException(\ValueError::class);
self::expectExceptionMessage('Minute is not valid');
TimeOfDay::fromParts(12, 60);
}

#[Test]
public function secondNotValid(): void
{
self::expectException(\ValueError::class);
self::expectExceptionMessage('Second is not valid');
TimeOfDay::fromParts(12, 34 , 60);
}

#[Test]
public function passedSince(): void
{
$time = new TimeOfDay(45296000);
$duration = $time->passedSince(new TimeOfDay(44296000));

self::assertEquals(new Duration(1000000), $duration);
}

#[Test]
public function was(): void
{
$time = new TimeOfDay(45296000);
$was = $time->was(new Duration(1000000));

self::assertEquals(new TimeOfDay(44296000), $was);
}

#[Test]
public function willBe(): void
{
$time = new TimeOfDay(45296000);
$was = $time->willBe(new Duration(1000000));

self::assertEquals(new TimeOfDay(46296000), $was);
}
}
Loading