Esempio n. 1
0
 /**
  * Parses expression given in literal form, builds an Expression object and returns it.
  * 
  * @param string $string
  * @return Expression
  * @throws \InvalidArgumentException
  */
 public static function create($string)
 {
     $months = implode('|', array_keys(self::$_synonyms[self::MONTH]));
     $daysOfWeek = implode('|', array_keys(self::$_synonyms[self::DAY_OF_WEEK]));
     $shorthands = implode('|', array_keys(self::$_shorthands));
     $pattern = "/\n            (?:^\n                (?P<minute>[\\d\\*\\/\\,\\-\\%]+)\n                \\s\n                (?P<hour>[\\d\\*\\/\\,\\-\\%]+)\n                \\s\n                (?P<dayOfMonth>[\\d\\*\\/\\,\\-\\%]+)\n                \\s\n                (?P<month>[\\d\\*\\/\\,\\-\\%]+|{$months})\n                \\s\n                (?P<dayOfWeek>[\\d\\*\\/\\,\\-\\%]+|{$daysOfWeek})\n            \$) | (?:^\n                (?P<shorthand>{$shorthands})\n            \$)\n        \$/ix";
     if (preg_match($pattern, $string, $matches)) {
         if (isset($matches['shorthand']) && $matches['shorthand']) {
             return self::create(self::$_shorthands[$matches['shorthand']]);
         }
         $expression = new Expression();
         foreach (array('minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek') as $part) {
             // Expand lists
             $matches[$part] = explode(',', $matches[$part]);
             // Separate range and step
             foreach ($matches[$part] as $timeUnit) {
                 $shards = explode('/', $timeUnit);
                 // Do we have a range (or asterisk) *and* a step?
                 if (count($shards) > 1) {
                     // Do we have an asterisk?
                     if ($shards[0] == '*') {
                         $expression->addPart($part, array('scalar' => '*', 'step' => $shards[1]));
                         // OK, clearly we have a range
                     } else {
                         $rangeLimits = explode('-', $shards[0]);
                         $expression->addPart($part, array('min' => $rangeLimits[0], 'max' => $rangeLimits[1], 'step' => $shards[1]));
                     }
                     // OK, we have just a range
                 } else {
                     $rangeLimits = explode('-', $shards[0]);
                     // Do we have a range *or* a number?
                     if (count($rangeLimits) > 1) {
                         $expression->addPart($part, array('min' => $rangeLimits[0], 'max' => $rangeLimits[1]));
                         // OK, we have just a number
                     } else {
                         $expression->addPart($part, $rangeLimits[0]);
                     }
                 }
             }
         }
         return $expression;
     }
     throw new \InvalidArgumentException(sprintf('Expression "%s" is not supported', $string));
 }