mtdowling / cron-expression

CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due
http://mtdowling.com/blog/2012/06/03/cron-expressions-in-php/
MIT License
4.89k stars 339 forks source link

Request: Simple validation function #77

Closed aensley closed 9 years ago

aensley commented 9 years ago

Thank you for this useful library. It's really great. I have a small feature request. Could you add a validateCronExpression() or isValidCronExpression() static function to CronExpression?

The reason I need this is that there will be situations where schedules are created programatically, and I want to make sure an invalid schedule isn't saved for later usage, causing errors on every run.

Right now, I'm doing this to accomplish what I need:

try {
    \Cron\CronExpression::factory($cronExpression);
} catch (\InvalidArgumentException $e) {
    LogUtil::error('Invalid cron expression "' . $cronExpression . '"');
    return false;
}

// $cronExpression is valid. Proceed.

It would be nice if I could do this:

if (!\Cron\CronExpression::isValidCronExpression($cronExpression)) {
    LogUtil::error('Invalid cron expression "' . $cronExpression . '"');
    return false;
} else {
    // $cronExpression is valid. Proceed.
}

It's a very small change as far as the code I write, but I feel like my current solution is a bit ugly and hack-ish since the underlying code might change in how invalid expressions are handled.

mtdowling commented 9 years ago

Sounds good to me.

aensley commented 9 years ago

Closing since #87 was merged.