Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\TokenParser\TokenParserRegistry::withParser PHP Method

withParser() public method

public withParser ( Nelmio\Alice\FixtureBuilder\ExpressionLanguage\ParserInterface $parser ) : self
$parser Nelmio\Alice\FixtureBuilder\ExpressionLanguage\ParserInterface
return self
    public function withParser(ParserInterface $parser) : self
    {
        $parsers = [];
        foreach ($this->parsers as $tokenParser) {
            $parsers[] = $tokenParser instanceof ParserAwareInterface ? $tokenParser->withParser($parser) : $tokenParser;
        }
        return new self($parsers);
    }

Usage Example

Example #1
0
 public function testWithersReturnNewModifiedInstance()
 {
     $parser = new FakeParser();
     $parserAwareProphecy = $this->prophesize(ParserAwareInterface::class);
     $parserAwareProphecy->withParser($parser)->willReturn($returnedParser = new FakeChainableTokenParser());
     /** @var ParserAwareInterface $parserAware */
     $parserAware = $parserAwareProphecy->reveal();
     $tokenParser = new TokenParserRegistry([$parser1 = new FakeChainableTokenParser(), $parser2 = new ProphecyChainableTokenParserAware(new FakeChainableTokenParser(), $parserAware)]);
     $newTokenParser = $tokenParser->withParser($parser);
     $this->assertInstanceOf(TokenParserRegistry::class, $newTokenParser);
     $this->assertSame([$parser1, $parser2], $this->parsersRefl->getValue($tokenParser));
     $newTokenParserParsers = $this->parsersRefl->getValue($newTokenParser);
     $this->assertCount(2, $newTokenParserParsers);
     $this->assertEquals([$parser1, $returnedParser], $newTokenParserParsers);
 }