<commit_before><?php

namespace Opsway\Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;

class RegexpReplace extends FunctionNode
{
    private $expr1;
    private $expr2;
    private $expr3;

    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->expr1 = $parser->StringPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->expr2 = $parser->StringPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->expr3 = $parser->StringPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(SqlWalker $sqlWalker)
    {
        return sprintf(
            "regexp_replace(%s, %s, %s)",
            $this->expr1->dispatch($sqlWalker),
            $this->expr2->dispatch($sqlWalker),
            $this->expr3->dispatch($sqlWalker)
        );
    }
}
<commit_msg>Make RegexpReplace support optional flags<commit_after><?php

namespace Opsway\Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;

class RegexpReplace extends FunctionNode
{
    /**
     * @var Node
     */
    private $text;

    /**
     * @var Node
     */
    private $pattern;

    /**
     * @var Node
     */
    private $replacement;

    /**
     * @var Node|null
     */
    private $flags;

    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);

        $this->text = $parser->StringPrimary();

        $parser->match(Lexer::T_COMMA);

        $this->pattern = $parser->StringPrimary();

        $parser->match(Lexer::T_COMMA);

        $this->replacement = $parser->StringPrimary();

        $this->flags = null;
        if (Lexer::T_COMMA === $parser->getLexer()->lookahead['type']) {
            $parser->match(Lexer::T_COMMA);
            $this->flags = $parser->StringPrimary();
        }

        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(SqlWalker $sqlWalker)
    {
        $arguments = [
            $this->text->dispatch($sqlWalker),
            $this->pattern->dispatch($sqlWalker),
            $this->replacement->dispatch($sqlWalker),
        ];

        if (null !== $this->flags) {
            $arguments[] = $this->flags->dispatch($sqlWalker);
        }

        return sprintf(
            'regexp_replace(%s)',
            join(', ', $arguments)
        );
    }
}

{'fraction_non_alphanumeric': 0.15487217487958505, 'fraction_numerical': 0.0033345683586513525, 'mean_word_length': 2.4928848641655885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5008127', 'n_tokens_mistral': 899, 'n_tokens_neox': 853, 'n_words': 124}