Crontab\Variable::parse PHP Method

parse() public static method

Parse a variable line
public static parse ( string $varLine ) : Variable
$varLine string
return Variable
    public static function parse($varLine)
    {
        $parts = explode('=', $varLine, 2);
        if (!$parts || count($parts) !== 2) {
            throw new \InvalidArgumentException("Line does not appear to contain a variable");
        }
        $variable = new Variable();
        $variable->setName(trim(array_shift($parts)))->setValue(trim(array_shift($parts)));
        return $variable;
    }

Usage Example

Beispiel #1
0
 /**
  * Returns an array of Cron Jobs based on the contents of a file.
  *
  * @param string $input
  *
  * @return array of Variable and Job instances
  */
 protected function parseString($input)
 {
     $elements = array();
     $lines = array_filter(explode(PHP_EOL, $input), function ($line) {
         return '' != trim($line);
     });
     foreach ($lines as $line) {
         $trimmed = trim($line);
         // if line is not a comment, convert it to a cron
         if (0 !== \strpos($trimmed, '#')) {
             if (preg_match('/^[^\\s]+\\s?=/', $line)) {
                 $elements[] = Variable::parse($line);
             } else {
                 $elements[] = Job::parse($line);
             }
         }
     }
     return $elements;
 }