42 $cronCommand = trim($cronCommand);
43 $cronCommand = self::convertKeywordsToCronCommand($cronCommand);
44 $cronCommand = self::normalizeFields($cronCommand);
56 switch ($cronCommand) {
60 $cronCommand =
'0 0 1 1 *';
63 $cronCommand =
'0 0 1 * *';
66 $cronCommand =
'0 0 * * 0';
71 $cronCommand =
'0 0 * * *';
74 $cronCommand =
'0 * * * *';
87 $fieldArray = self::splitFields($cronCommand);
88 $fieldArray[0] = self::normalizeIntegerField($fieldArray[0], 0, 59);
89 $fieldArray[1] = self::normalizeIntegerField($fieldArray[1], 0, 23);
90 $fieldArray[2] = self::normalizeIntegerField($fieldArray[2], 1, 31);
91 $fieldArray[3] = self::normalizeMonthAndWeekdayField($fieldArray[3], TRUE);
92 $fieldArray[4] = self::normalizeMonthAndWeekdayField($fieldArray[4], FALSE);
93 $normalizedCronCommand = implode(
' ', $fieldArray);
94 return $normalizedCronCommand;
105 $fields = explode(
' ', $cronCommand);
106 if (count($fields) !== 5) {
107 throw new \InvalidArgumentException(
'Unable to split given cron command to five fields.', 1291227373);
120 if ((
string) $expression ===
'*') {
124 $listOfCommaValues = explode(
',', $expression);
125 $fieldArray = array();
126 foreach ($listOfCommaValues as $listElement) {
127 if (strpos($listElement,
'/') !== FALSE) {
128 list($left, $right) = explode(
'/', $listElement);
129 if (strpos($left,
'-') !== FALSE) {
130 list($leftBound, $rightBound) = explode(
'-', $left);
131 $leftBound = self::normalizeMonthAndWeekday($leftBound, $isMonthField);
132 $rightBound = self::normalizeMonthAndWeekday($rightBound, $isMonthField);
133 $left = $leftBound .
'-' . $rightBound;
135 if ((
string) $left !==
'*') {
136 $left = self::normalizeMonthAndWeekday($left, $isMonthField);
139 $fieldArray[] = $left .
'/' . $right;
140 } elseif (strpos($listElement,
'-') !== FALSE) {
141 list($left, $right) = explode(
'-', $listElement);
142 $left = self::normalizeMonthAndWeekday($left, $isMonthField);
143 $right = self::normalizeMonthAndWeekday($right, $isMonthField);
144 $fieldArray[] = $left .
'-' . $right;
146 $fieldArray[] = self::normalizeMonthAndWeekday($listElement, $isMonthField);
149 $fieldValues = implode(
',', $fieldArray);
151 return $isMonthField ? self::normalizeIntegerField($fieldValues, 1, 12) : self::normalizeIntegerField($fieldValues, 1, 7);
164 if ((
string) $expression ===
'*') {
167 $listOfCommaValues = explode(
',', $expression);
168 $fieldArray = array();
169 foreach ($listOfCommaValues as $listElement) {
170 if (strpos($listElement,
'/') !== FALSE) {
171 list($left, $right) = explode(
'/', $listElement);
172 if ((
string) $left ===
'*') {
173 $leftList = self::convertRangeToListOfValues($lowerBound .
'-' . $upperBound);
175 $leftList = self::convertRangeToListOfValues($left);
177 $fieldArray[] = self::reduceListOfValuesByStepValue($leftList .
'/' . $right);
178 } elseif (strpos($listElement,
'-') !== FALSE) {
179 $fieldArray[] = self::convertRangeToListOfValues($listElement);
181 $fieldArray[] = $listElement;
183 throw new \InvalidArgumentException(
'Unable to normalize integer field.', 1291429389);
186 $fieldValues = implode(
',', $fieldArray);
188 if (strlen($fieldValues) === 0) {
189 throw new \InvalidArgumentException(
'Unable to convert integer field to list of values: Result list empty.', 1291422012);
191 if ((
string) $fieldValues !==
'*') {
192 $fieldList = explode(
',', $fieldValues);
194 $fieldList = array_unique($fieldList);
195 if (current($fieldList) < $lowerBound) {
196 throw new \InvalidArgumentException(
'Lowest element in list is smaller than allowed.', 1291470084);
198 if (end($fieldList) > $upperBound) {
199 throw new \InvalidArgumentException(
'An element in the list is higher than allowed.', 1291470170);
201 $fieldValues = implode(
',', $fieldList);
203 return (
string) $fieldValues;
214 if (strlen($range) === 0) {
215 throw new \InvalidArgumentException(
'Unable to convert range to list of values with empty string.', 1291234985);
217 $rangeArray = explode(
'-', $range);
219 foreach ($rangeArray as $fieldNumber => $fieldValue) {
221 throw new \InvalidArgumentException(
'Unable to convert value to integer.', 1291237668);
223 $rangeArray[$fieldNumber] = (int)$fieldValue;
226 if (count($rangeArray) === 1) {
227 $resultList = $rangeArray[0];
228 } elseif (count($rangeArray) === 2) {
229 $left = $rangeArray[0];
230 $right = $rangeArray[1];
231 if ($left > $right) {
232 throw new \InvalidArgumentException(
'Unable to convert range to list: Left integer must not be greater than right integer.', 1291237145);
234 $resultListArray = array();
235 for ($i = $left; $i <= $right; $i++) {
236 $resultListArray[] = $i;
238 $resultList = implode(
',', $resultListArray);
240 throw new \InvalidArgumentException(
'Unable to convert range to list of values.', 1291234986);
242 return (
string) $resultList;
256 if (strlen($stepExpression) === 0) {
257 throw new \InvalidArgumentException(
'Unable to convert step values.', 1291234987);
259 $stepValuesAndStepArray = explode(
'/', $stepExpression);
260 if (count($stepValuesAndStepArray) < 1 || count($stepValuesAndStepArray) > 2) {
261 throw new \InvalidArgumentException(
'Unable to convert step values: Multiple slashes found.', 1291242168);
263 $left = $stepValuesAndStepArray[0];
264 $right = $stepValuesAndStepArray[1];
265 if (strlen($stepValuesAndStepArray[0]) === 0) {
266 throw new \InvalidArgumentException(
'Unable to convert step values: Left part of / is empty.', 1291414955);
268 if (strlen($stepValuesAndStepArray[1]) === 0) {
269 throw new \InvalidArgumentException(
'Unable to convert step values: Right part of / is empty.', 1291414956);
272 throw new \InvalidArgumentException(
'Unable to convert step values: Right part must be a single integer.', 1291414957);
274 $right = (int)$right;
275 $leftArray = explode(
',', $left);
276 $validValues = array();
277 $currentStep = $right;
278 foreach ($leftArray as $leftValue) {
280 throw new \InvalidArgumentException(
'Unable to convert step values: Left part must be a single integer or comma separated list of integers.', 1291414958);
282 if ($currentStep === 0) {
283 $currentStep = $right;
285 if ($currentStep === $right) {
286 $validValues[] = (int)$leftValue;
290 if (count($validValues) === 0) {
291 throw new \InvalidArgumentException(
'Unable to convert step values: Result value list is empty.', 1291414959);
293 return implode(
',', $validValues);
304 $expression = $isMonth ? self::normalizeMonth($expression) : self::normalizeWeekday($expression);
305 return (
string) $expression;
317 $timestamp = strtotime(
'2010-' . $month .
'-01');
319 if (!$timestamp || $timestamp < strtotime(
'2010-01-01') || $timestamp > strtotime(
'2010-12-01')) {
320 throw new \InvalidArgumentException(
'Unable to convert given month name.', 1291083486);
322 return (
int)date(
'n', $timestamp);
334 $normalizedWeekday = FALSE;
336 if ((
string) $weekday ===
'0') {
339 if ($weekday >= 1 && $weekday <= 7) {
340 $normalizedWeekday = (int)$weekday;
342 if (!$normalizedWeekday) {
344 $timestamp = strtotime(
'next ' . $weekday, mktime(0, 0, 0, 1, 1, 2010));
345 if (!$timestamp || $timestamp < strtotime(
'2010-01-01') || $timestamp > strtotime(
'2010-01-08')) {
346 throw new \InvalidArgumentException(
'Unable to convert given weekday name.', 1291163589);
348 $normalizedWeekday = (int)date(
'N', $timestamp);
350 return $normalizedWeekday;
static convertRangeToListOfValues($range)
static canBeInterpretedAsInteger($var)
static normalizeIntegerField($expression, $lowerBound=0, $upperBound=59)
static normalize($cronCommand)
static reduceListOfValuesByStepValue($stepExpression)
static normalizeMonth($month)
static normalizeMonthAndWeekdayField($expression, $isMonthField=TRUE)
static normalizeMonthAndWeekday($expression, $isMonth=TRUE)
static convertKeywordsToCronCommand($cronCommand)
static normalizeFields($cronCommand)
static normalizeWeekday($weekday)
static splitFields($cronCommand)