Yosymfony\Toml\Parser::parse PHP Méthode

parse() public méthode

Parses TOML string into a PHP value.
public parse ( string $value ) : mixed
$value string A TOML string
Résultat mixed A PHP value
    public function parse($value)
    {
        $this->lexer = new Lexer($value);
        $this->lexer->getToken();
        while ($this->lexer->getCurrentToken()->getType() !== Lexer::TOKEN_EOF) {
            switch ($this->lexer->getCurrentToken()->getType()) {
                case Lexer::TOKEN_HASH:
                    $this->processComment();
                    // #comment
                    break;
                case Lexer::TOKEN_LBRANK:
                    $this->processTables();
                    // [table] or [[array of tables]]
                    break;
                case Lexer::TOKEN_LITERAL:
                case Lexer::TOKEN_QUOTES:
                    $this->processKeyValue();
                    // key = value or "key name" = value
                    break;
                case Lexer::TOKEN_NEWLINE:
                    $this->currentLine++;
                    if ($this->inlineTableCounter > 0) {
                        throw new ParseException('Syntax error: unexpected newline inside a inline table', $this->currentLine, $this->lexer->getCurrentToken()->getValue());
                    }
                    break;
                case Lexer::TOKEN_RKEY:
                    if (0 === $this->inlineTableCounter) {
                        throw new ParseException('Syntax error: unexpected token', $this->currentLine, $this->lexer->getCurrentToken()->getValue());
                    }
                    --$this->inlineTableCounter;
                    array_pop($this->inlineTableNameStack);
                    break;
                case Lexer::TOKEN_COMMA:
                    if ($this->inlineTableCounter > 0) {
                        break;
                    } else {
                        throw new ParseException('Syntax error: unexpected token', $this->currentLine, $this->lexer->getCurrentToken()->getValue());
                    }
                default:
                    throw new ParseException('Syntax error: unexpected token', $this->currentLine, $this->lexer->getCurrentToken()->getValue());
            }
            $this->lexer->getToken();
        }
        return empty($this->result) ? null : $this->result;
    }

Usage Example

Exemple #1
0
 /**
  * Parse TOML into a PHP array.
  *  
  * Usage:
  * <code>
  *  $array = Toml::parse('config.toml');
  *  print_r($array);
  *  
  *  $array = Toml::parse('key = "[1,2,3]"');
  *  print_r($array);
  * </code>
  * 
  * @return array The TOML converted to a PHP array
  */
 public static function parse($input)
 {
     $file = '';
     if (is_file($input)) {
         if (!is_readable($input)) {
             throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
         }
         $file = $input;
         $input = file_get_contents($input);
     }
     $parser = new Parser();
     try {
         return $parser->parse($input);
     } catch (ParseException $e) {
         if ($file) {
             $e->setParsedFile($file);
         }
         throw $e;
     }
 }
All Usage Examples Of Yosymfony\Toml\Parser::parse