Skip to content

Commit

Permalink
Merge pull request #11 from kylekatarnls/name-and-observe
Browse files Browse the repository at this point in the history
Implement observe methods
  • Loading branch information
kylekatarnls authored Nov 2, 2018
2 parents 10a88da + 0a96756 commit ad74d83
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/Cmixin/BusinessDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Cmixin;

use Carbon\Carbon;
use Cmixin\BusinessDay\HolidayObserver;

class BusinessDay extends Holiday
class BusinessDay extends HolidayObserver
{
/**
* Checks the date to see if it is a business day.
Expand Down Expand Up @@ -107,16 +108,17 @@ public function currentOrPreviousBusinessDay()
public function addBusinessDays($factor = 1)
{
$getThisOrToday = static::getThisOrToday();
$carbonClass = static::getCarbonClass();

return function ($days = 1, $self = null) use ($factor, $getThisOrToday) {
/** @var Carbon|BusinessDay $self */
$self = $getThisOrToday($self, isset($this) ? $this : null);

return function ($days = 1, $self = null) use ($factor, $getThisOrToday, $carbonClass) {
if ($days instanceof \DateTime || $days instanceof \DateTimeInterface) {
$self = $days;
$self = $carbonClass::instance($days);
$days = 1;
}

/** @var Carbon|BusinessDay $self */
$self = $getThisOrToday($self, isset($this) ? $this : null);

$days *= $factor;

for ($i = $days; $i > 0; $i--) {
Expand Down
7 changes: 4 additions & 3 deletions src/Cmixin/Holiday.php → src/Cmixin/BusinessDay/Holiday.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

namespace Cmixin;
namespace Cmixin\BusinessDay;

use Carbon\Carbon;
use Cmixin\BusinessDay;

class Holiday extends HolidaysList
{
/**
* Get the identifier of the current holiday or false if it's not an holiday.
* Get the identifier of the current holiday or false if it's not a holiday.
*
* @return \Closure
*/
Expand Down Expand Up @@ -37,7 +38,7 @@ public function getHolidayId()
}

/**
* Checks the date to see if it is an holiday.
* Checks the date to see if it is a holiday.
*
* @return \Closure
*/
Expand Down
216 changes: 216 additions & 0 deletions src/Cmixin/BusinessDay/HolidayObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?php

namespace Cmixin\BusinessDay;

use Carbon\Carbon;
use Cmixin\BusinessDay;

class HolidayObserver extends Holiday
{
const OBSERVE_ALL_HOLIDAYS = 'all';

/**
* @var array
*/
public $observedHolidays = array();

/**
* @var string
*/
public $observedHolidaysZone = 'default';

/**
* Set the selected zone for observed holidays.
*
* @return \Closure
*/
public function setObservedHolidaysZone()
{
$mixin = $this;

return function ($zone, $self = null) use ($mixin) {
$mixin->observedHolidaysZone = $zone;

return isset($this) ? $this : (isset($self) ? $self : null);
};
}

/**
* Get the selected zone for observed holidays.
*
* @return \Closure
*/
public function getObservedHolidaysZone()
{
$mixin = $this;

return function () use ($mixin) {
return $mixin->observedHolidaysZone;
};
}

/**
* Set a holiday as observed in the selected zone.
*
* @return \Closure
*/
public function setHolidayObserveStatus()
{
$mixin = $this;
$allHolidays = static::OBSERVE_ALL_HOLIDAYS;

return function ($day, $value, $self = null) use ($mixin, $allHolidays) {
if (!is_string($day)) {
throw new \InvalidArgumentException(
'You must pass holiday names as a string or "'.$allHolidays.'".'
);
}

$zone = $mixin->observedHolidaysZone;
if ($day === $allHolidays || !isset($mixin->observedHolidays[$zone])) {
$mixin->observedHolidays[$zone] = array();
}

$mixin->observedHolidays[$zone][$day] = $value;

return isset($this) ? $this : (isset($self) ? $self : null);
};
}

/**
* Set a holiday as observed in the selected zone.
*
* @return \Closure
*/
public function getObserveHolidayMethod($defaultValue = null, $defaultDay = null)
{
$mixin = $this;

return function ($day = null, $value = null, $self = null) use ($mixin, $defaultValue, $defaultDay) {
if (!$day && $defaultDay) {
$day = $defaultDay;
}
if ($value === null) {
$value = $defaultValue;
}
$days = (array) $day;
$setter = $mixin->setHolidayObserveStatus();
foreach ($days as $day) {
$setter($day, $value);
}

return isset($this) ? $this : (isset($self) ? $self : null);
};
}

/**
* Set a holiday as observed in the selected zone.
*
* @return \Closure
*/
public function observeHoliday()
{
return $this->getObserveHolidayMethod(true);
}

/**
* Set a holiday as not observed in the selected zone.
*
* @return \Closure
*/
public function unobserveHoliday()
{
return $this->getObserveHolidayMethod(false);
}

/**
* Set a holiday as observed in the selected zone.
*
* @return \Closure
*/
public function observeHolidays()
{
return $this->getObserveHolidayMethod(true);
}

/**
* Set a holiday as not observed in the selected zone.
*
* @return \Closure
*/
public function unobserveHolidays()
{
return $this->getObserveHolidayMethod(false);
}

/**
* Set all holidays as observed in the selected zone.
*
* @return \Closure
*/
public function observeAllHolidays()
{
return $this->getObserveHolidayMethod(true, static::OBSERVE_ALL_HOLIDAYS);
}

/**
* Set all holidays as observed in the selected zone.
*
* @return \Closure
*/
public function unobserveAllHolidays()
{
return $this->getObserveHolidayMethod(false, static::OBSERVE_ALL_HOLIDAYS);
}

public function checkObservedHoliday()
{
$mixin = $this;
$allHolidays = static::OBSERVE_ALL_HOLIDAYS;

return function ($name = null) use ($mixin, $allHolidays) {
$zone = $mixin->observedHolidaysZone;
$days = isset($mixin->observedHolidays[$zone]) ? $mixin->observedHolidays[$zone] : array();

if ($name) {
if (isset($days[$name])) {
return (bool) $days[$name];
}
if (isset($days[$allHolidays])) {
return (bool) $days[$allHolidays];
}
}

return false;
};
}

/**
* Checks the date to see if it is a holiday.
*
* @return \Closure
*/
public function isObservedHoliday()
{
$mixin = $this;
$getThisOrToday = static::getThisOrToday();
$carbonClass = static::getCarbonClass();

return function ($name = null, $self = null) use ($mixin, $getThisOrToday, $carbonClass) {
if ($name instanceof \DateTime || $name instanceof \DateTimeInterface) {
$self = $carbonClass::instance($name);
$name = null;
}

if (!$name) {
/** @var Carbon|BusinessDay $self */
$self = $getThisOrToday($self, isset($this) ? $this : null);
$name = $self->getHolidayId();
}

$check = $mixin->checkObservedHoliday();

return $check($name);
};
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Cmixin;
namespace Cmixin\BusinessDay;

class HolidaysList extends EnableFacadeMixinBase
class HolidaysList extends MixinBase
{
/**
* @var array
Expand All @@ -26,7 +26,7 @@ public function setHolidaysRegion()
return function ($region) use ($mixin) {
$region = preg_replace('/[^a-zA-Z0-9_-]/', '', $region);
$mixin->holidaysRegion = $region;
if (!isset($mixin->holidays[$region]) && file_exists($file = __DIR__."/Holidays/$region.php")) {
if (!isset($mixin->holidays[$region]) && file_exists($file = __DIR__."/../Holidays/$region.php")) {
$mixin->holidays[$region] = include $file;
}
};
Expand Down Expand Up @@ -83,6 +83,8 @@ public function resetHolidays()
/**
* Initialize holidays region storage.
*
* @param string|null $region
*
* @return \Closure|$this
*/
public function initializeHolidaysRegion($region = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Cmixin;
namespace Cmixin\BusinessDay;

class EnableFacadeMixinBase
abstract class MixinBase
{
protected static $carbonClass = null;

Expand Down
Loading

0 comments on commit ad74d83

Please sign in to comment.