Skip to content

Commit

Permalink
refactor: rename array of time parts to times
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Aug 15, 2018
1 parent f0425da commit ee2cc96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ public static function isDue($expr, $time = null)
*/
public function isCronDue($expr, $time = null)
{
list($expr, $time) = $this->process($expr, $time);
list($expr, $times) = $this->process($expr, $time);

$checker = new SegmentChecker;
foreach ($expr as $pos => $segment) {
if ($segment === '*' || $segment === '?') {
continue;
}

if (!$checker->checkDue($segment, $pos, $time)) {
if (!$checker->checkDue($segment, $pos, $times)) {
return false;
}
}
Expand Down Expand Up @@ -127,11 +127,10 @@ protected function process($expr, $time)
);
}

$time = static::normalizeTime($time);
$time = static::normalizeTime($time);
$times = \array_map('intval', \explode(' ', \date('i G j n w Y t d m N', $time)));

$time = \array_map('intval', \explode(' ', \date('i G j n w Y t d m N', $time)));

return [$expr, $time];
return [$expr, $times];
}

protected function normalizeTime($time)
Expand Down
24 changes: 12 additions & 12 deletions src/SegmentChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public function __construct()
*
* @param string $segment
* @param int $pos
* @param int $time
* @param array $times
*
* @return bool
*/
public function checkDue($segment, $pos, $time)
public function checkDue($segment, $pos, $times)
{
$isDue = true;
$offsets = \explode(',', \trim($segment));

foreach ($offsets as $offset) {
if (null === $isDue = $this->isOffsetDue($offset, $pos, $time)) {
if (null === $isDue = $this->isOffsetDue($offset, $pos, $times)) {
throw new \UnexpectedValueException(
sprintf('Invalid offset value at segment #%d: %s', $pos, $offset)
);
Expand All @@ -62,37 +62,37 @@ public function checkDue($segment, $pos, $time)
*
* @param string $offset
* @param int $pos
* @param array $time
* @param array $times
*
* @return bool|null
*/
protected function isOffsetDue($offset, $pos, $time)
protected function isOffsetDue($offset, $pos, $times)
{
if (\strpos($offset, '/') !== false) {
return $this->validator->inStep($time[$pos], $offset);
return $this->validator->inStep($times[$pos], $offset);
}

if (\strpos($offset, '-') !== false) {
return $this->validator->inRange($time[$pos], $offset);
return $this->validator->inRange($times[$pos], $offset);
}

if (\is_numeric($offset)) {
return $time[$pos] == $offset;
return $times[$pos] == $offset;
}

return $this->checkModifier($offset, $pos, $time);
return $this->checkModifier($offset, $pos, $times);
}

protected function checkModifier($offset, $pos, $time)
protected function checkModifier($offset, $pos, $times)
{
$isModifier = \strpbrk($offset, 'LCW#');

if ($pos === 2 && $isModifier) {
return $this->validator->isValidMonthDay($offset, $time);
return $this->validator->isValidMonthDay($offset, $times);
}

if ($pos === 4 && $isModifier) {
return $this->validator->isValidWeekDay($offset, $time);
return $this->validator->isValidWeekDay($offset, $times);
}

return null;
Expand Down

0 comments on commit ee2cc96

Please sign in to comment.