Skip to content

Commit

Permalink
Merge pull request #49 from charnad/issue-47
Browse files Browse the repository at this point in the history
Fixed regexp for day of week
  • Loading branch information
mtdowling committed Apr 22, 2014
2 parents bbd1b74 + fb8caa4 commit 11a4457
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/Cron/DayOfWeekField.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ public function isSatisfiedBy(\DateTime $date, $value)
}

// Convert text day of the week values to integers
$value = str_ireplace(
array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'),
range(0, 6),
$value
);
$value = $this->convertLiterals($value);

$currentYear = $date->format('Y');
$currentMonth = $date->format('m');
Expand Down Expand Up @@ -110,6 +106,16 @@ public function increment(\DateTime $date, $invert = false)

public function validate($value)
{
return (bool) preg_match('/[\*,\/\-0-9A-Z]+/', $value);
$value = $this->convertLiterals($value);
return (bool) preg_match('/^(\*|[0-7](L?|#[1-5]))([\/\,\-][0-7]+)*$/', $value);
}

private function convertLiterals($string)
{
return str_ireplace(
array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'),
range(0, 6),
$string
);
}
}
16 changes: 15 additions & 1 deletion tests/Cron/DayOfWeekFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DayOfWeekFieldTest extends \PHPUnit_Framework_TestCase
/**
* @covers Cron\DayOfWeekField::validate
*/
public function testValdatesField()
public function testValidatesField()
{
$f = new DayOfWeekField();
$this->assertTrue($f->validate('1'));
Expand Down Expand Up @@ -77,4 +77,18 @@ public function testHandlesZeroAndSevenDayOfTheWeekValues()
$this->assertTrue($f->isSatisfiedBy(new DateTime('2011-09-04 00:00:00'), '0-2'));
$this->assertTrue($f->isSatisfiedBy(new DateTime('2011-09-04 00:00:00'), '6-0'));
}

/**
* @see https://github.com/mtdowling/cron-expression/issues/47
*/
public function testIssue47() {
$f = new DayOfWeekField();
$this->assertFalse($f->validate('mon,'));
$this->assertFalse($f->validate('mon-'));
$this->assertFalse($f->validate('*/2,'));
$this->assertFalse($f->validate('-mon'));
$this->assertFalse($f->validate(',1'));
$this->assertFalse($f->validate('*-'));
$this->assertFalse($f->validate(',-'));
}
}

0 comments on commit 11a4457

Please sign in to comment.