Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\SimpleParser::parse PHP Method

parse() public method

public parse ( string $value )
$value string
    public function parse(string $value)
    {
        $tokens = $this->lexer->lex($value);
        $parsedTokens = [];
        foreach ($tokens as $token) {
            $parsedTokens = $this->parseToken($parsedTokens, $this->tokenParser, $token);
        }
        return 1 === count($parsedTokens) ? $parsedTokens[0] : new ListValue($parsedTokens);
    }

Usage Example

Example #1
0
 public function testIfATokenIsParsedIntoANestedValueThenItsValuesAreMerged()
 {
     $value = 'foo';
     $lexerProphecy = $this->prophesize(LexerInterface::class);
     $lexerProphecy->lex($value)->willReturn([$token1 = new Token('foo', new TokenType(TokenType::STRING_TYPE)), $token2 = new Token('bar', new TokenType(TokenType::VARIABLE_TYPE)), $token3 = new Token('baz', new TokenType(TokenType::FUNCTION_TYPE))]);
     /** @var LexerInterface $lexer */
     $lexer = $lexerProphecy->reveal();
     $tokenParserProphecy = $this->prophesize(TokenParserInterface::class);
     $tokenParserProphecy->parse($token1)->willReturn('parsed_foo');
     $tokenParserProphecy->parse($token2)->willReturn(new NestedValue(['first', 'second']));
     $tokenParserProphecy->parse($token3)->willReturn('parsed_baz');
     /** @var TokenParserInterface $tokenParser */
     $tokenParser = $tokenParserProphecy->reveal();
     $expected = new ListValue(['parsed_foo', 'first', 'second', 'parsed_baz']);
     $parser = new SimpleParser($lexer, $tokenParser);
     $actual = $parser->parse($value);
     $this->assertEquals($expected, $actual);
     $lexerProphecy->lex(Argument::any())->shouldHaveBeenCalledTimes(1);
     $tokenParserProphecy->parse(Argument::any())->shouldHaveBeenCalledTimes(3);
 }