$upperBound) { throw new \InvalidArgumentException('An element in the list is higher than allowed.', 1291470170); } $fieldValues = implode(',', $fieldList); } return $fieldValues; } /** * Convert a range of integers to a list: 4-6 results in a string '4,5,6' * * @param string $range integer-integer * @throws \InvalidArgumentException If range can not be converted to list */ protected static function convertRangeToListOfValues(string $range): string { if ($range === '') { throw new \InvalidArgumentException('Unable to convert range to list of values with empty string.', 1291234985); } $rangeArray = explode('-', $range); // Sanitize fields and cast to integer foreach ($rangeArray as $fieldNumber => $fieldValue) { if (!MathUtility::canBeInterpretedAsInteger($fieldValue)) { throw new \InvalidArgumentException('Unable to convert value to integer.', 1291237668); } $rangeArray[$fieldNumber] = (int)$fieldValue; } $rangeArrayCount = count($rangeArray); if ($rangeArrayCount === 1) { $resultList = $rangeArray[0]; } elseif ($rangeArrayCount === 2) { $left = $rangeArray[0]; $right = $rangeArray[1]; if ($left > $right) { throw new \InvalidArgumentException('Unable to convert range to list: Left integer must not be greater than right integer.', 1291237145); } $resultListArray = []; for ($i = $left; $i <= $right; $i++) { $resultListArray[] = $i; } $resultList = implode(',', $resultListArray); } else { throw new \InvalidArgumentException('Unable to convert range to list of values.', 1291234986); } return (string)$resultList; } /** * Reduce a given list of values by step value. * Following a range with ``/'' specifies skips of the number's value through the range. * 1-5/2 -> 1,3,5 * 2-10/3 -> 2,5,8 * * @return string comma-separated list of valid values * @throws \InvalidArgumentException if step value is invalid or if resulting list is empty */ protected static function reduceListOfValuesByStepValue(string $stepExpression): string { if ($stepExpression === '') { throw new \InvalidArgumentException('Unable to convert step values.', 1291234987); } $stepValuesAndStepArray = explode('/', $stepExpression); $stepValuesAndStepArrayCount = count($stepValuesAndStepArray); if ($stepValuesAndStepArrayCount > 2) { throw new \InvalidArgumentException('Unable to convert step values: Multiple slashes found.', 1291242168); } $left = $stepValuesAndStepArray[0]; $right = $stepValuesAndStepArray[1] ?? ''; if ($left === '') { throw new \InvalidArgumentException('Unable to convert step values: Left part of / is empty.', 1291414955); } if ($right === '') { throw new \InvalidArgumentException('Unable to convert step values: Right part of / is empty.', 1291414956); } if (!MathUtility::canBeInterpretedAsInteger($right)) { throw new \InvalidArgumentException('Unable to convert step values: Right part must be a single integer.', 1291414957); } $right = (int)$right; $leftArray = explode(',', $left); $validValues = []; $currentStep = $right; foreach ($leftArray as $leftValue) { if (!MathUtility::canBeInterpretedAsInteger($leftValue)) { throw new \InvalidArgumentException('Unable to convert step values: Left part must be a single integer or comma separated list of integers.', 1291414958); } if ($currentStep === 0) { $currentStep = $right; } if ($currentStep === $right) { $validValues[] = (int)$leftValue; } $currentStep--; } if (empty($validValues)) { throw new \InvalidArgumentException('Unable to convert step values: Result value list is empty.', 1291414959); } return implode(',', $validValues); } /** * Dispatcher method for normalizeMonth and normalizeWeekday */ protected static function normalizeMonthAndWeekday(string $expression, bool $isMonth = true): string { $expression = $isMonth ? self::normalizeMonth($expression) : self::normalizeWeekday($expression); return (string)$expression; } /** * Accept a string representation or integer number of a month like * 'jan', 'February', 01, ... and convert to normalized integer value between 1 and 12 * * @throws \InvalidArgumentException If month string can not be converted to integer */ protected static function normalizeMonth(string $month): int { $timestamp = strtotime('2010-' . $month . '-01'); // timestamp must be >= 2010-01-01 and <= 2010-12-01 if (!$timestamp || $timestamp < strtotime('2010-01-01') || $timestamp > strtotime('2010-12-01')) { throw new \InvalidArgumentException('Unable to convert given month name.', 1291083486); } return (int)date('n', $timestamp); } /** * Accept a string representation or integer number of a weekday like * 'mon', 'Friday', 3, ... and convert to normalized integer value between 1 and 7 * * @throws \InvalidArgumentException If weekday string can not be converted */ protected static function normalizeWeekday(string $weekday): int { $normalizedWeekday = false; // 0 (sunday) -> 7 if ($weekday === '0') { $weekday = 7; } if ($weekday >= 1 && $weekday <= 7) { $normalizedWeekday = (int)$weekday; } if (!$normalizedWeekday) { // Convert string representation like 'sun' to integer $timestamp = strtotime('next ' . $weekday, (int)mktime(0, 0, 0, 1, 1, 2010)); if (!$timestamp || $timestamp < strtotime('2010-01-01') || $timestamp > strtotime('2010-01-08')) { throw new \InvalidArgumentException('Unable to convert given weekday name.', 1291163589); } $normalizedWeekday = (int)date('N', $timestamp); } return $normalizedWeekday; } }