Skip to content

Commit

Permalink
Added: holidays
Browse files Browse the repository at this point in the history
  • Loading branch information
JanGalek committed Mar 9, 2018
1 parent 80989c5 commit 354cd3c
Show file tree
Hide file tree
Showing 16 changed files with 379 additions and 48 deletions.
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -10,7 +10,9 @@
],
"autoload": {
"psr-4": {
"Galek\\Utils\\": "src/Galek/Utils/Calendar/"
"Galek\\Utils\\": "src/Galek/Utils/Calendar/",
"Galek\\Utils\\Calendar\\Enum\\": "src/Galek/Utils/Calendar/Enum/",
"Galek\\Utils\\Calendar\\Validators\\": "src/Galek/Utils/Calendar/Validators/"
}
},
"require": {
Expand Down
74 changes: 45 additions & 29 deletions src/Galek/Utils/Calendar/Calendar.php
Expand Up @@ -3,6 +3,7 @@

namespace Galek\Utils;

use Galek\Utils\Calendar\Enum\Country;
use Nette\Utils\DateTime;

date_default_timezone_set('Europe/Prague');
Expand All @@ -24,23 +25,6 @@ class Calendar extends DateTime
/** @var int Number of days to delivery */
public $shippingtime = 1;


/** @var array */
private $svatky = [
'01-01',
'03-25',
'05-01',
'05-08',
'07-05',
'07-06',
'09-28',
'10-28',
'11-17',
'12-24',
'12-25',
'12-26',
];

/**
* Use 24 hours type
* ['work start ([hour,minute])', 'work_end ([hour,minute])']
Expand All @@ -66,13 +50,19 @@ class Calendar extends DateTime
*/
private $localization;

/**
* @var Holidays
*/
private $holidays;


/**
* @param string $time [optional]
* @param $object [optional]
* @param string $localization
* @param string $country
*/
public function __construct($time = 'now', $object = null, $localization = 'cs')
public function __construct($time = 'now', $object = null, $localization = 'cs', $country = Country::CZ)
{
parent::__construct($time, $object);

Expand All @@ -81,9 +71,24 @@ public function __construct($time = 'now', $object = null, $localization = 'cs')
if (!isset($this->date)) {
$this->date = $this->curDate;
}

$this->localization = new Localization($localization);
$this->holidays = new Holidays($country);
}


public function getHolidays()
{
return $this->holidays;
}


public function getLocalization()
{
return $this->localization;
}


/**
* Set number of days to delivery
* @param int
Expand Down Expand Up @@ -286,18 +291,19 @@ public function isWorkTime()
public function isHoliday()
{
$date = $this;
if ($this->getEasterMonday()->format('Y-m-d') === $date->format('Y-m-d')) {

if ($this->holidays->allowedEaster() && $this->getEasterMonday()->format('Y-m-d') === $date->format('Y-m-d')) {
return TRUE;
}

if ($this->getBigFriday()->format('Y-m-d') === $date->format('Y-m-d')) {
if ($this->holidays->allowedGoodFriday() && $this->getBigFriday()->format('Y-m-d') === $date->format('Y-m-d')) {
return TRUE;
}

foreach ($this->svatky as $svatek) {
$testdate = $date->getYear() . '-' . $svatek;
foreach ($this->holidays->getHolidays() as $holiday) {
$yearHoliday = $date->getYear() . '-' . $holiday;

if ($this->format('Y-m-d') == $testdate) {
if ($this->format('Y-m-d') === $yearHoliday) {
return TRUE;
}
}
Expand Down Expand Up @@ -493,17 +499,27 @@ public function getEaster($rok = FALSE)
return $this->getVelikonoce($rok);
}

/**
* Is Big Friday (friday before Easter, Czech republic = Holiday) ?
* @param bool|integer $year
* @return DateTime
*/
public function getGoodFriday($year = FALSE)
{
$velNe = $this->getVelikonoce($year);
$day = DateTime::from($velNe);
$day->modify('-2 day');
return $day;
}

/**
* Is Big Friday (friday before Easter, Czech republic = Holiday) ?
* @param bool|integer $rok
* @param bool|integer $year
* @return DateTime
*/
public function getBigFriday($rok = FALSE)
public function getBigFriday($year = FALSE)
{
$velNe = $this->getVelikonoce($rok);
$day = DateTime::from($velNe);
$day->modify('-2 day');
return $day;
return $this->getGoodFriday($year);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/Galek/Utils/Calendar/Enum/Country.php
@@ -0,0 +1,27 @@
<?php
/**
* Created by PhpStorm.
* User: Galek
* Date: 9.3.2018
*/
declare(strict_types=1);

namespace Galek\Utils\Calendar\Enum;


class Country
{
public const CZ = 'CzechRepublic',
SK = 'Slovakia',
PL = 'Poland',
DE = 'Germany',
AT = 'Austria';

public static $list = [
self::CZ,
self::SK,
self::PL,
self::DE,
self::AT,
];
}
23 changes: 23 additions & 0 deletions src/Galek/Utils/Calendar/Enum/Localization.php
@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: Galek
* Date: 9.3.2018
*/
declare(strict_types=1);

namespace Galek\Utils\Calendar\Enum;


class Localization
{
public const CZ = 'cs',
SK = 'sk',
PL = 'en';

public static $list = [
self::CZ,
self::SK,
self::PL,
];
}
63 changes: 63 additions & 0 deletions src/Galek/Utils/Calendar/Holidays.php
@@ -0,0 +1,63 @@
<?php
/**
* Created by PhpStorm.
* User: Galek
* Date: 9.3.2018
*/
declare(strict_types=1);

namespace Galek\Utils;


use Galek\Utils\Calendar\Validators\CountryValidator;
use Nette\Neon\Neon;


class Holidays
{
/**
* @var string
*/
private $country;


public function __construct(string $country)
{
$this->setCountry($country);
}


public function setCountry(string $country)
{
CountryValidator::validate($country);
$this->country = $country;
}


/**
* @return array
*/
public function getHolidays()
{
return $this->loadConfig()['holidays'];
}


public function allowedEaster()
{
return $this->loadConfig()['easter'];
}


public function allowedGoodFriday()
{
return $this->loadConfig()['goodFriday'];
}


private function loadConfig()
{
$file = __DIR__ . '/Holidays/' . $this->country . '.neon';
return Neon::decode(file_get_contents($file), Neon::BLOCK);
}
}
20 changes: 20 additions & 0 deletions src/Galek/Utils/Calendar/Holidays/Austria.neon
@@ -0,0 +1,20 @@
holidays:
# měsíc - den
- '01-01'
- '01-06'
- '05-01'
- '05-10'
- '05-20'
- '05-21'
- '05-31'
- '08-15'
- '09-24'
- '10-26'
- '11-01'
- '11-15'
- '12-08'
- '12-25'
- '12-26'

easter: true
goodFriday: true
16 changes: 16 additions & 0 deletions src/Galek/Utils/Calendar/Holidays/CzechRepublic.neon
@@ -0,0 +1,16 @@
holidays:
# měsíc - den
- '01-01'
- '05-01'
- '05-08'
- '07-05'
- '07-06'
- '09-28'
- '10-28'
- '11-17'
- '12-24'
- '12-25'
- '12-26'

easter: true
goodFriday: true
18 changes: 18 additions & 0 deletions src/Galek/Utils/Calendar/Holidays/Germany.neon
@@ -0,0 +1,18 @@
holidays:
# měsíc - den
- '01-01'
- '01-06'
- '05-01'
- '05-10'
- '05-21'
- '05-31'
- '08-15'
- '10-03'
- '10-31'
- '11-01'
- '11-21'
- '12-25'
- '12-26'

easter: true
goodFriday: true
16 changes: 16 additions & 0 deletions src/Galek/Utils/Calendar/Holidays/Poland.neon
@@ -0,0 +1,16 @@
holidays:
# měsíc - den
- '01-01'
- '01-06'
- '05-01'
- '05-03'
- '05-20'
- '05-31'
- '08-15'
- '11-01'
- '11-11'
- '12-25'
- '12-26'

easter: true
goodFriday: true
18 changes: 18 additions & 0 deletions src/Galek/Utils/Calendar/Holidays/Slovakia.neon
@@ -0,0 +1,18 @@
holidays:
# měsíc - den
- '01-01'
- '01-06'
- '05-01'
- '05-08'
- '07-05'
- '08-29'
- '09-01'
- '09-15'
- '11-01'
- '11-17'
- '12-24'
- '12-25'
- '12-26'

easter: true
goodFriday: true
6 changes: 4 additions & 2 deletions src/Galek/Utils/Calendar/Localization.php
Expand Up @@ -9,6 +9,7 @@
namespace Galek\Utils;


use Galek\Utils\Calendar\Validators\LocalizationValidator;
use Nette\Neon\Neon;


Expand All @@ -22,12 +23,13 @@ class Localization

public function __construct(string $local)
{
$this->local = $local;
$this->setLocalization($local);
}


public function setLocalization(string $local)
{
LocalizationValidator::validate($local);
$this->local = $local;
}

Expand Down Expand Up @@ -71,6 +73,6 @@ private function getInflexions()
private function loadConfig()
{
$file = __DIR__ . '/Localization/' . $this->local . '.neon';
return Neon::decode(file_get_contents($file));
return Neon::decode(file_get_contents($file), Neon::BLOCK);
}
}

0 comments on commit 354cd3c

Please sign in to comment.