Simplifying

This commit is contained in:
Dennis Eichhorn 2016-05-11 19:09:50 +02:00
parent 22244c712c
commit 321b0ae48d
2 changed files with 110 additions and 150 deletions

View File

@ -26,7 +26,7 @@ namespace phpOMS\Utils\TaskSchedule;
* @link http://orange-management.com * @link http://orange-management.com
* @since 1.0.0 * @since 1.0.0
*/ */
class Interval class Interval implements \Serializable
{ {
/** /**
@ -106,30 +106,10 @@ class Interval
$this->start = new \DateTime('now'); $this->start = new \DateTime('now');
if (isset($interval)) { if (isset($interval)) {
$this->parse($interval); $this->unserialize($interval);
} }
} }
/**
* Parse interval.
*
* @param string $interval Interval to parse
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function parse(string $interval)
{
$elements = explode(' ', trim($interval));
$this->minute = $this->parseMinute($elements[0]);
$this->hour = $this->parseHour($elements[1]);
$this->dayOfMonth = $this->parseDayOfMonth($elements[2]);
$this->month = $this->parseMonth($elements[3]);
$this->dayOfWeek = $this->parseDayOfWeek($elements[4]);
$this->year = $this->parseYear($elements[5]);
}
/** /**
* Parse element. * Parse element.
* *
@ -290,13 +270,12 @@ class Interval
*/ */
public function setMinute(array $minute, int $step = 0, bool $any = false) public function setMinute(array $minute, int $step = 0, bool $any = false)
{ {
if ($this->validateMinute($arr = [ if ($this->validateTime($minute, $step, 0, 59)) {
'minutes' => $minute, $this->hour = [
'step' => $step, 'minutes' => $minute,
'any' => $any, 'step' => $step,
]) 'any' => $any,
) { ];
$this->hour = $arr;
} else { } else {
throw new \Exception('Invalid format.'); throw new \Exception('Invalid format.');
} }
@ -316,13 +295,12 @@ class Interval
*/ */
public function setHour(array $hour, int $step = 0, bool $any = false) public function setHour(array $hour, int $step = 0, bool $any = false)
{ {
if ($this->validateHour($arr = [ if ($this->validateTime($hour, $step, 0, 23)) {
'hours' => $hour, $this->hour = [
'step' => $step, 'hours' => $hour,
'any' => $any, 'step' => $step,
]) 'any' => $any,
) { ];
$this->hour = $arr;
} else { } else {
throw new \Exception('Invalid format.'); throw new \Exception('Invalid format.');
} }
@ -372,13 +350,12 @@ class Interval
*/ */
public function setMonth(array $month, int $step = 0, bool $any = false) public function setMonth(array $month, int $step = 0, bool $any = false)
{ {
if ($this->validateMonth($arr = [ if ($this->validateTime($month, $step, 1, 12)) {
'month' => $month, $this->month = [
'step' => $step, 'month' => $month,
'any' => $any, 'step' => $step,
]) 'any' => $any,
) { ];
$this->hour = $arr;
} else { } else {
throw new \Exception('Invalid format.'); throw new \Exception('Invalid format.');
} }
@ -517,49 +494,27 @@ class Interval
} }
/** /**
* Validate minute. * Validate time.
* *
* @param array $array Element to validate * @param array $times Times
* @param int $step Step
* @param int $lowest Lowest limet
* @param int $highest Highest limet
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
private function validateMinute(array $array) : bool private function validateTime(array $times, int $step, int $lowest, int $highest) : bool
{ {
foreach ($array['minutes'] as $minute) { foreach ($times as $minute) {
if ($minute > 59 || $minute < 0) { if ($minute > $highest || $minute < $lowest) {
return false; return false;
} }
} }
if ($array['step'] > 59 || $array['step'] < 0) { if ($step > $highest || $step < $lowest) {
return false;
}
return true;
}
/**
* Validate hour.
*
* @param array $array Element to validate
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function validateHour(array $array) : bool
{
foreach ($array['hours'] as $hour) {
if ($hour > 23 || $hour < 0) {
return false;
}
}
if ($array['step'] > 23 || $array['step'] < 0) {
return false; return false;
} }
@ -594,31 +549,6 @@ class Interval
return true; return true;
} }
/**
* Validate month.
*
* @param array $array Element to validate
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function validateMonth(array $array) : bool
{
foreach ($array['month'] as $month) {
if ($month > 12 || $month < 1) {
return false;
}
}
if ($array['step'] > 12 || $array['step'] < 1) {
return false;
}
return true;
}
/** /**
* Validate day of week. * Validate day of week.
* *
@ -662,93 +592,123 @@ class Interval
/** /**
* Create string representation. * Create string representation.
* *
* @param array $time Time
* @param int $step Step for repetition
*
* @return string * @return string
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function __toString() : string public function serializeTime($time, $step)
{ {
/* Parsing minutes */ if (($count = count($time)) > 0) {
if (($count = count($this->minute['minutes'])) > 0) { $serialize = implode(',', $time);
$minute = implode(',', $this->minute['minutes']);
} else { } else {
$minute = '*'; $serialize = '*';
$count = 1; $count = 1;
} }
if ($count === 0 && $this->minute['step'] !== 0) { if ($count === 0 && $step !== 0) {
$minute .= '/' . $this->minute['step']; $serialize .= '/' . $step;
} }
/* Parsing hours */ return $serialize;
if (($count = count($this->hour['hours'])) > 0) { }
$hour = implode(',', $this->hour['hours']);
} else {
$hour = '*';
$count = 1;
}
if ($count === 0 && $this->hour['step'] !== 0) { /**
$hour .= '/' . $this->hour['step']; * Create string representation.
} *
* @return string
/* Parsing day of month */ *
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function serializeDayOfMonth()
{
if (($count = count($this->dayOfMonth['dayOfMonth'])) > 0) { if (($count = count($this->dayOfMonth['dayOfMonth'])) > 0) {
$dayOfMonth = implode(',', $this->dayOfMonth['dayOfMonth']); $serialize = implode(',', $this->dayOfMonth['dayOfMonth']);
} else { } else {
$dayOfMonth = '*'; $serialize = '*';
$count = 1; $count = 1;
} }
if ($count === 0 && $this->dayOfMonth['step'] !== 0) { if ($count === 0 && $this->dayOfMonth['step'] !== 0) {
$dayOfMonth .= '/' . $this->dayOfMonth['step']; $serialize .= '/' . $this->dayOfMonth['step'];
} }
if ($this->dayOfMonth['last']) { if ($this->dayOfMonth['last']) {
$dayOfMonth .= 'L'; $serialize .= 'L';
} }
/* Parsing month */ return $serialize;
if (($count = count($this->month['month'])) > 0) { }
$month = implode(',', $this->month['month']);
} else {
$month = '*';
$count = 1;
}
if ($count === 0 && $this->month['step'] !== 0) { /**
$month .= '/' . $this->month['step']; * Create string representation.
} *
* @return string
/* Parsing day of week */ *
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function serializeDayOfWeek()
{
if (($count = count($this->dayOfWeek['dayOfWeek'])) > 0) { if (($count = count($this->dayOfWeek['dayOfWeek'])) > 0) {
$dayOfWeek = implode(',', $this->dayOfWeek['dayOfWeek']); $serialize = implode(',', $this->dayOfWeek['dayOfWeek']);
} else { } else {
$dayOfWeek = '*'; $serialize = '*';
$count = 1; $count = 1;
} }
if ($count === 0 && $this->dayOfWeek['step'] !== 0) { if ($count === 0 && $this->dayOfWeek['step'] !== 0) {
$dayOfWeek .= '#' . $this->dayOfWeek['step']; $serialize .= '#' . $this->dayOfWeek['step'];
} }
if ($this->dayOfWeek['last']) { if ($this->dayOfWeek['last']) {
$dayOfWeek .= 'L'; $serialize .= 'L';
} }
/* Parsing year */ return $serialize;
if (($count = count($this->year['year'])) > 0) { }
$year = implode(',', $this->year['year']);
} else {
$year = '*';
$count = 1;
}
if ($count === 0 && $this->year['step'] !== 0) { /**
$year .= '/' . $this->year['step']; * Create string representation.
} *
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function serialize()
{
$minute = $this->serializeTime($this->minute['minutes'], $this->minute['step']);
$hour = $this->serializeTime($this->hour['hours'], $this->hour['step']);
$dayOfMonth = $this->serializeDayOfMonth();
$month = $this->serializeTime($this->month['month'], $this->month['step']);
$dayOfWeek = $this->serializeDayOfWeek();
$year = $this->serializeTime($this->year['year'], $this->year['step']);
return $minute . ' ' . $hour . ' ' . $dayOfMonth . ' ' . $month . ' ' . $dayOfWeek . ' ' . $year; return $minute . ' ' . $hour . ' ' . $dayOfMonth . ' ' . $month . ' ' . $dayOfWeek . ' ' . $year;
} }
/**
* Unserialize.
*
* @param string $serialized String to unserialize
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function unserialize($serialized)
{
$elements = explode(' ', trim($serialized));
$this->minute = $this->parseMinute($elements[0]);
$this->hour = $this->parseHour($elements[1]);
$this->dayOfMonth = $this->parseDayOfMonth($elements[2]);
$this->month = $this->parseMonth($elements[3]);
$this->dayOfWeek = $this->parseDayOfWeek($elements[4]);
$this->year = $this->parseYear($elements[5]);
}
} }