Skip to content

Commit

Permalink
Added Holidays abstraction with GoogleCalendarRegionalHolidays implem…
Browse files Browse the repository at this point in the history
…entation
  • Loading branch information
norberttech committed May 23, 2020
1 parent 9267619 commit 0606e73
Show file tree
Hide file tree
Showing 240 changed files with 1,084 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}
},
"require": {
"ext-json": "*",
"webmozart/assert": "^1.8"
},
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions src/Ocelot/Calendar/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Ocelot\Ocelot\Calendar\Exception;

class Exception extends \Exception
{
}
10 changes: 10 additions & 0 deletions src/Ocelot/Calendar/Exception/HolidayException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Ocelot\Ocelot\Calendar\Exception;

class HolidayException extends Exception
{

}
9 changes: 9 additions & 0 deletions src/Ocelot/Calendar/Exception/HolidayYearException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Ocelot\Ocelot\Calendar\Exception;

final class HolidayYearException extends HolidayException
{
}
28 changes: 28 additions & 0 deletions src/Ocelot/Calendar/Gregorian/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static function fromDateTime(\DateTimeImmutable $dateTime) : self

/**
* @throws \Exception
* @psalm-pure
*/
public static function fromString(string $date) : self
{
Expand Down Expand Up @@ -88,6 +89,11 @@ public function month() : Month
return $this->month;
}

public function year() : Year
{
return $this->month()->year();
}

public function number() : int
{
return $this->number;
Expand Down Expand Up @@ -127,8 +133,30 @@ public function dayOfYear() : int
return ((int) $this->toDateTimeImmutable()->format('z')) + 1;
}

public function isWeekend() : bool
{
return \in_array($this->dayOfWeek(), [6, 7]);
}

public function toDateTimeImmutable() : \DateTimeImmutable
{
return (new \DateTimeImmutable('now'))->setDate($this->month()->year()->number(), $this->month()->number(), $this->number());
}

public function format(string $format) : string
{
return $this->toDateTimeImmutable()->format($format);
}

public function equals(Day $day) : bool
{
return $this->year()->number() === $day->year()->number()
&& $this->month()->number() === $day->month()->number()
&& $this->number() === $day->number();
}

public function equalsString(string $date) : bool
{
return $this->equals(self::fromString($date));
}
}
16 changes: 16 additions & 0 deletions src/Ocelot/Calendar/Holidays.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Ocelot\Ocelot\Calendar;

use Ocelot\Ocelot\Calendar\Gregorian\Day;
use Ocelot\Ocelot\Calendar\Holidays\Holiday;

interface Holidays
{
public function isHoliday(Day $day) : bool;

/**
* @return array<Holiday>
*/
public function holidaysAt(Day $day) : array;
}
Loading

0 comments on commit 0606e73

Please sign in to comment.