hollodotme / crontab-validator
A validator for crontab expressions.
Installs: 122 660
Dependents: 2
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 4
Open Issues: 1
Requires
- php: >=7.1
Requires (Dev)
- tm/tooly-composer-script: ^1.0
This package is auto-updated.
Last update: 2024-11-05 08:09:48 UTC
README
CrontabValidator
A validator for crontab expressions.
Sources used to determine the allowed expressions:
Features
- Validation of crontab expressions like 6,21,36,51 7-23/1 * FEB-NOV/2 *.
Requirements
- PHP >= 7.1
Installation
composer require "hollodotme/crontab-validator"
Usage
Boolean validation
<?php declare(strict_types=1); namespace MyVendor\MyProject; use hollodotme\CrontabValidator\CrontabValidator; $validator = new CrontabValidator(); if ( $validator->isExpressionValid( '6,21,36,51 7-23/1 * FEB-NOV/2 *' ) ) { echo 'Expression is valid.'; } else { echo 'Expression is invalid.'; }
Guarding
<?php declare(strict_types=1); namespace MyVendor\MyProject; use hollodotme\CrontabValidator\CrontabValidator; use hollodotme\CrontabValidator\Exceptions\InvalidExpressionException; $validator = new CrontabValidator(); try { # => All fine, execution continues $validator->guardExpressionIsValid( '6,21,36,51 7-23/1 * FEB-NOV/2 *' ); # => This will raise an InvalidExpressionException $validator->guardExpressionIsValid( 'this is not a valid interval' ); } catch ( InvalidExpressionException $e ) { echo $e->getMessage(); }
Prints:
Invalid crontab expression: "this is not a valid interval"
Feedback and contributions welcome!