Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\TokenParser\Chainable\OptionalTokenParser::parse PHP Метод

parse() публичный Метод

public parse ( Token $token ) : OptionalValue
$token Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Token
Результат Nelmio\Alice\Definition\Value\OptionalValue
    public function parse(Token $token) : OptionalValue
    {
        parent::parse($token);
        if (1 !== preg_match(self::REGEX, $token->getValue(), $matches)) {
            throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
        }
        return new OptionalValue($this->parser->parse($matches['quantifier']), $this->parser->parse(trim($matches['first_member'])), array_key_exists('second_member', $matches) ? $this->parser->parse($matches['second_member']) : null);
    }

Usage Example

Пример #1
0
 public function testReturnsAnOptionalValueIfCanParseToken()
 {
     $token = new Token('60%? foo: bar', new TokenType(TokenType::OPTIONAL_TYPE));
     $anotherToken = new Token('80%? baz', new TokenType(TokenType::OPTIONAL_TYPE));
     $decoratedParserProphecy = $this->prophesize(ParserInterface::class);
     $decoratedParserProphecy->parse('60')->willReturn('parsed_quantifier');
     $decoratedParserProphecy->parse('foo')->willReturn('parsed_first_member');
     $decoratedParserProphecy->parse('bar')->willReturn('parsed_second_member');
     $decoratedParserProphecy->parse('80')->willReturn('parsed_80');
     $decoratedParserProphecy->parse('baz')->willReturn('parsed_baz');
     /** @var ParserInterface $decoratedParser */
     $decoratedParser = $decoratedParserProphecy->reveal();
     $expected0 = new OptionalValue('parsed_quantifier', 'parsed_first_member', 'parsed_second_member');
     $expected1 = new OptionalValue('parsed_80', 'parsed_baz');
     $parser = new OptionalTokenParser($decoratedParser);
     $actual0 = $parser->parse($token);
     $actual1 = $parser->parse($anotherToken);
     $this->assertEquals($expected0, $actual0);
     $this->assertEquals($expected1, $actual1);
     $decoratedParserProphecy->parse(Argument::any())->shouldHaveBeenCalledTimes(5);
 }
OptionalTokenParser