Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JanGalek committed Apr 13, 2018
1 parent 77e0f3a commit 2f29ff8
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 407 deletions.
2 changes: 1 addition & 1 deletion src/Galek/Utils/Calendar/Business/Shipper.php
Expand Up @@ -57,7 +57,7 @@ class Shipper implements IShipper
public function __construct(string $name, Localization $configuration, int $hour, int $minute, bool $weekend = false, int $deliveryTime = 1)
{
$this->setTime($hour, $minute);
$this->enableWeekend($weekend);
$this->weekend = $weekend;
$this->configuration = $configuration;
$this->deliveryTime = $deliveryTime;
$this->name = $name;
Expand Down
157 changes: 6 additions & 151 deletions src/Galek/Utils/Calendar/Calendar.php
Expand Up @@ -245,17 +245,6 @@ public function isSunday(): bool
}


public function isWorkTime(): bool
{
if ($this->isWorkDay()) {
$startTime = $this->getWorkTime(1);
$endTime = $this->getWorkTime(2);
return $this->timeBetween((int) $startTime[0], (int) $startTime[1], (int) $endTime[0], (int) $endTime[1]);
}

return false;
}

/**
* Check Holiday
* @return bool
Expand Down Expand Up @@ -283,7 +272,7 @@ public function getDaysInMonth(int $year, int $month)
}


public function getWorkDayNumberInMonth(int $month, int $year)
public function getWorkDayNumberInMonth(int $month, int $year): int
{
return $this->getWorkDayNumberInMonthTo($month, $year, $this->getDaysInMonth($year, $month));
}
Expand Down Expand Up @@ -412,8 +401,7 @@ public function werbDif(): string
*/
public function getEasterMonday($year = null): Calendar
{
$year = $year ?? $this->getYear();
return EasterHoliday::getMonday($year);
return EasterHoliday::getMonday($year ?? $this->getYear());
}

/**
Expand All @@ -423,8 +411,7 @@ public function getEasterMonday($year = null): Calendar
*/
public function getEaster($year = null): Calendar
{
$year = $year ?? $this->getYear();
return EasterHoliday::getEaster($year);
return EasterHoliday::getEaster($year ?? $this->getYear());
}

/**
Expand All @@ -434,154 +421,22 @@ public function getEaster($year = null): Calendar
*/
public function getGoodFriday($year = null): Calendar
{
$year = $year ?? $this->getYear();
return EasterHoliday::getGoodFriday($year);
return EasterHoliday::getGoodFriday($year ?? $this->getYear());
}


/**
* @param \DateTime $date
* @param integer $pad
* @return string
*/
public function sklonovaniDays($date, $pad = 1): string
{
return $this->configuration->getLocalization()->getInflexion($date->format('w'), $pad);
}

/**
* Get workday
* @param bool|Calendar|\DateTime|DateTime $next
* @param null|Calendar $date
* @return Calendar
*/
public function getWorkDay($next = false, $date = null): Calendar
{
if (!$date) {
$date = $this;
}

if ($next instanceof self) {
$date = $next;
$next = false;
}

if ($next) {
$date->modify('+1 days');
}

if (Day::isWeekend($date)) {
if (Day::isSunday($date)) {
$date->modify('+1 days');
} else {
$date->modify('+2 days');
}
} elseif ($this->getHolidays()->isHoliday($date)) {
$date->modify('+1 days');
}

if (!$this->isWorkDay($date)) {
$this->getWorkDay();
}

return $date;
}


/**
* @param bool $workTime
* @param Calendar|null $date
* @return Calendar
* @throws \Exception
*/
public function getWorkDayLimit($workTime = true, $date = null): Calendar
public function getWorkDay($next = false, Calendar $date = null): Calendar
{
if ($date === null) {
$date = $this;
}

if ($workTime === true) {
$limit = $this->getWorkTime();
[$eH, $eM] = $limit[1];

if ($this->timeBellow($eH, $eM) === false) {
$date->modify('+1 days');
}
}

$date->getWorkDay();
return $date;
}

/**
*
* @param mixed $worktime Work time in array () or hour of work time (int)
* @param int|boolean $startMinute [description]
* @param int|boolean $endHour [description]
* @param int|boolean $endMinute [description]
* @return Calendar
* @throws \Exception
*/
public function setWorkTime($worktime, $startMinute = false, $endHour = false, $endMinute = false): Calendar
{
if (\is_array($worktime)) {

if (\is_array($worktime[0])) {
[$startHour, $startMinute] = $worktime[0];

if (isset($worktime[1])) {
[$endHour, $endMinute] = $worktime[1];
}

if (isset($worktime[0][2])) {
$endHour = $worktime[0][2];
}

if (isset($worktime[0][3])) {
$endMinute = $worktime[0][3];
}
} else {
[$startHour, $startMinute, $endHour, $endMinute] = $worktime;
}

} elseif (is_numeric($worktime)) {
$startHour = $worktime;
$startMinute = (int) $startMinute;
$endHour = (int) $endHour;
$endMinute = (int) $endMinute;
} else {
throw new \Exception("Value '$worktime' is not allowed, use array (full list) or int (hour)");
}

HourValidator::validate($startHour);
HourValidator::validate($endHour);
MinuteValidator::validate($startMinute);
MinuteValidator::validate($endMinute);

$this->working_time = [[(int) $startHour, (int) $startMinute], [(int) $endHour, (int) $endMinute]];

return $this;
}

/**
* @param int|null $type
* @return array|mixed
* @throws \Exception
*/
public function getWorkTime(int $type = null)
{
if ($type === null) {
return $this->working_time;
}

$type = (int) $type;

if ($type === self::WORK_TIME_START) {
return $this->working_time[0];
} elseif ($type === self::WORK_TIME_END) {
return $this->working_time[1];
}

throw new \Exception("Value '$type' is not allowed, you can use (false, 1, 2)");
return Day::getWorkDay($date, $this->getHolidays(), $next);
}
}
24 changes: 24 additions & 0 deletions src/Galek/Utils/Calendar/Day.php
Expand Up @@ -75,6 +75,30 @@ public static function isWork(Holidays $holidays, \DateTime $date): bool
}


public static function getWorkDay(Calendar $date, Holidays $holidays, $next = false)
{
if ($next) {
$date->modify('+1 days');
}

if (self::isWeekend($date)) {
if (self::isSunday($date)) {
$date->modify('+1 days');
} else {
$date->modify('+2 days');
}
} elseif ($holidays->isHoliday($date)) {
$date->modify('+1 days');
}

if (!self::isWork($holidays, $date)) {
$date = self::getWorkDay($date, $holidays);
}

return $date;
}


public static function isWeek(\DateTime $date): bool
{
$number = self::getNumber($date);
Expand Down
24 changes: 7 additions & 17 deletions tests/CalendarTests/Calendar.phpt
Expand Up @@ -9,7 +9,7 @@ class CalendarTest extends \Tester\TestCase
{


public function testBasic()
public function testBasic(): void
{
$date2 = new Calendar(DATE_RELEASE);
Assert::equal(TRUE, $date2->isWorkDay());
Expand All @@ -23,7 +23,7 @@ class CalendarTest extends \Tester\TestCase
Assert::equal(TRUE, $date2->isWorkDay());
}

public function testDays()
public function testDays(): void
{
$date = new Calendar(DATE_RELEASE);
Assert::equal(FALSE, $date->isMonday());
Expand All @@ -37,7 +37,7 @@ class CalendarTest extends \Tester\TestCase
Assert::equal(FALSE, $date->isHoliday());
}

public function testHoliday()
public function testHoliday(): void
{
$date = new Calendar('30.04.2016 14:02');
Assert::equal(FALSE, $date->isHoliday(), 'Test holiday '.$date->format( 'd.m.Y H:i').' ?');
Expand All @@ -54,13 +54,13 @@ class CalendarTest extends \Tester\TestCase
Assert::equal('1850-03-31', $date3->getEaster()->format('Y-m-d'), 'Test Easter '.$date3->getYear().' ?');
}

public function testDifWerbs()
public function testDifWerbs(): void
{
$date = new Calendar();
Assert::equal('dnes', $date->werbDif());
}

public function testDayNumbers()
public function testDayNumbers(): void
{
$date = new Calendar(DATE_RELEASE);
Assert::equal(5, $date->dayNumber(), 'Friday');
Expand Down Expand Up @@ -126,12 +126,10 @@ class CalendarTest extends \Tester\TestCase
Assert::equal(TRUE, $date2->timeBetween(20,0, 23, 58));
Assert::equal(TRUE, $date2->timeBetween(20,0, 0, 59));
Assert::equal(FALSE, $date2->timeBetween(0,0, 2, 0));

$date3 = new Calendar(DATE_RELEASE . ' 18:02');
Assert::equal(FALSE, $date2->timeBellow(16, 20), 'Time bellow 2');
}

public function testShippingTime()
public function testShippingTime(): void
{
$date = new Calendar(DATE_RELEASE . ' 11:02');

Expand All @@ -140,23 +138,15 @@ class CalendarTest extends \Tester\TestCase
Assert::equal(TRUE, $date->isWorkDay());
}

public function testSklonovani()
{
$date = new Calendar('2017-12-09 00:00:00');
Assert::equal('sobota', $date->sklonovaniDays($date),'sklonovani');
}

public function testFormat()
public function testFormat(): void
{
$date = new Calendar('2017-12-09 1:02:01');

Assert::equal($date->format('d.m.Y H:i:s'), $date->getDateTimeFormat(),'dateFormat');
}

public function testEaster()
{
$date = new Calendar('2017-04-16 0:00:00');

Assert::equal($date->format('d.m.Y'), $date->getEaster(2017)->format('d.m.Y'),'dateFormat');

$date2 = new Calendar('1969-04-06 0:00:00');
Expand Down
43 changes: 0 additions & 43 deletions tests/CalendarTests/Exceptions.phpt

This file was deleted.

0 comments on commit 2f29ff8

Please sign in to comment.