Skip to content

Commit

Permalink
Date can now resolve weekends and working days & DayEnum introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
tuscanicz committed Jun 26, 2019
1 parent 1969fa1 commit cfed4ab
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"php": ">=7.1",
"symfony/dependency-injection": "~3.3|~4.0",
"symfony/config": "~3.3|~4.0",
"symfony/http-kernel": "~3.3|~4.0"
"symfony/http-kernel": "~3.3|~4.0",
"tuscanicz/enum": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "~6.3|~7.0",
Expand Down
28 changes: 18 additions & 10 deletions src/Date/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@

use DateTime as DateTimePhp;
use DateInterval as DateIntervalPhp;
use Tuscanicz\DateTimeBundle\Date\Day\DayEnum;

class Date
{
const DAY_MONDAY = 1;
const DAY_TUESDAY = 2;
const DAY_WEDNESDAY = 3;
const DAY_THURSDAY = 4;
const DAY_FRIDAY = 5;
const DAY_SATURDAY = 6;
const DAY_SUNDAY = 7;

private $year;
private $month;
private $day;
Expand Down Expand Up @@ -63,9 +56,24 @@ public function toFormat(string $format): string
return $stringFormat;
}

public function getDayOfWeek(): int
public function isWeekend(): bool
{
return (int) $this->toFormat('N');
return $this->getDayOfWeek()->in([DayEnum::DAY_SUNDAY, DayEnum::DAY_SATURDAY]) === true;
}

public function isWorkingDay(): bool
{
return $this->isWeekend() === false;
}

public function getDayOfWeek(): DayEnum
{
$dayOfWeek = (int) $this->toFormat('N');
if (DayEnum::hasValue($dayOfWeek) === true) {
return new DayEnum($dayOfWeek);
}

throw new \InvalidArgumentException('Could not get day of week, invalid value given: ' . $dayOfWeek);
}

public function getWeek(): int
Expand Down
18 changes: 18 additions & 0 deletions src/Date/Day/DayEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Tuscanicz\DateTimeBundle\Date\Day;

use Enum\AbstractEnum;

class DayEnum extends AbstractEnum
{
public const DAY_MONDAY = 1;
public const DAY_TUESDAY = 2;
public const DAY_WEDNESDAY = 3;
public const DAY_THURSDAY = 4;
public const DAY_FRIDAY = 5;
public const DAY_SATURDAY = 6;
public const DAY_SUNDAY = 7;
}
14 changes: 14 additions & 0 deletions tests/Date/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Tuscanicz\DateTimeBundle\Date;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Tuscanicz\DateTimeBundle\Date\Day\DayEnum;

class DateTest extends TestCase
{
Expand Down Expand Up @@ -107,6 +109,18 @@ public function testSubMonths(): void
self::assertEquals($expectedNewDateTime, $newDateTime);
}

public function testGetDayOfWeekWillFailOnUnresolvedDayNumber(): void
{
/** @var Date|MockObject $dateMock */
$dateMock = $this->getMockBuilder(Date::class)->disableOriginalConstructor()->setMethodsExcept(['getDayOfWeek'])->getMock();
$dateMock->method('toFormat')->willReturn(99);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Could not get day of week, invalid value given: 99');

$dateMock->getDayOfWeek();
}

public function getDaysFromDataProvider(): array
{
return [
Expand Down
18 changes: 17 additions & 1 deletion tests/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Tuscanicz\DateTimeBundle\Date\Date;
use Tuscanicz\DateTimeBundle\Date\Day\DayEnum;
use Tuscanicz\DateTimeBundle\Time\Time;

class DateTimeTest extends TestCase
Expand All @@ -24,7 +25,9 @@ public function testGetters(): void
new Time(11, 19, 59)
);

self::assertSame(Date::DAY_FRIDAY, $dateTime->getDate()->getDayOfWeek());
self::assertSame(DayEnum::DAY_FRIDAY, $dateTime->getDate()->getDayOfWeek()->getValue());
self::assertFalse($dateTime->getDate()->isWeekend());
self::assertTrue($dateTime->getDate()->isWorkingDay());
self::assertSame(31, $dateTime->getDate()->getWeek());
self::assertSame(1987, $dateTime->getDate()->getYear());
self::assertSame(7, $dateTime->getDate()->getMonth());
Expand All @@ -34,6 +37,19 @@ public function testGetters(): void
self::assertSame(59, $dateTime->getTime()->getSecond());
}

public function testGettersWithWeekend(): void
{
$dateTime = new DateTime(
new Date(1987, 7, 26),
new Time(14, 55, 00)
);

self::assertInstanceOf(DayEnum::class, $dateTime->getDate()->getDayOfWeek());
self::assertSame(DayEnum::DAY_SUNDAY, $dateTime->getDate()->getDayOfWeek()->getValue());
self::assertTrue($dateTime->getDate()->isWeekend());
self::assertFalse($dateTime->getDate()->isWorkingDay());
}

public function testToFormat(): void
{
$dateTime = new DateTime(
Expand Down

0 comments on commit cfed4ab

Please sign in to comment.