GraphQL\Language\Parser::parseValueLiteral PHP Method

parseValueLiteral() public method

BooleanValue : one of true false NullValue : null EnumValue : Name but not true, false or null
public parseValueLiteral ( $isConst ) : BooleanValueNode | EnumValueNode | FloatValueNode | IntValueNode | StringValueNode | VariableNode | ListValueNode | ObjectValueNode | NullValueNode
$isConst
return GraphQL\Language\AST\BooleanValueNode | GraphQL\Language\AST\EnumValueNode | GraphQL\Language\AST\FloatValueNode | GraphQL\Language\AST\IntValueNode | GraphQL\Language\AST\StringValueNode | GraphQL\Language\AST\VariableNode | GraphQL\Language\AST\ListValueNode | GraphQL\Language\AST\ObjectValueNode | GraphQL\Language\AST\NullValueNode
    function parseValueLiteral($isConst)
    {
        $token = $this->lexer->token;
        switch ($token->kind) {
            case Token::BRACKET_L:
                return $this->parseArray($isConst);
            case Token::BRACE_L:
                return $this->parseObject($isConst);
            case Token::INT:
                $this->lexer->advance();
                return new IntValueNode(['value' => $token->value, 'loc' => $this->loc($token)]);
            case Token::FLOAT:
                $this->lexer->advance();
                return new FloatValueNode(['value' => $token->value, 'loc' => $this->loc($token)]);
            case Token::STRING:
                $this->lexer->advance();
                return new StringValueNode(['value' => $token->value, 'loc' => $this->loc($token)]);
            case Token::NAME:
                if ($token->value === 'true' || $token->value === 'false') {
                    $this->lexer->advance();
                    return new BooleanValueNode(['value' => $token->value === 'true', 'loc' => $this->loc($token)]);
                } else {
                    if ($token->value === 'null') {
                        $this->lexer->advance();
                        return new NullValueNode(['loc' => $this->loc($token)]);
                    } else {
                        $this->lexer->advance();
                        return new EnumValueNode(['value' => $token->value, 'loc' => $this->loc($token)]);
                    }
                }
                break;
            case Token::DOLLAR:
                if (!$isConst) {
                    return $this->parseVariable();
                }
                break;
        }
        throw $this->unexpected();
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
  * that value.
  * Throws GraphQLError if a syntax error is encountered.
  *
  * This is useful within tools that operate upon GraphQL Values directly and
  * in isolation of complete GraphQL documents.
  *
  * Consider providing the results to the utility function: valueFromAST().
  *
  * @param Source|string $source
  * @param array $options
  * @return BooleanValue|EnumValue|FloatValue|IntValue|ListValue|ObjectValue|StringValue|Variable
  */
 public static function parseValue($source, array $options = [])
 {
     $sourceObj = $source instanceof Source ? $source : new Source($source);
     $parser = new Parser($sourceObj, $options);
     $parser->expect(Token::SOF);
     $value = $parser->parseValueLiteral(false);
     $parser->expect(Token::EOF);
     return $value;
 }