|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Pfilsx\PostgreSQLDoctrine\ORM\Query\AST\Functions; |
| 6 | + |
| 7 | +use Doctrine\ORM\Query\AST\Functions\FunctionNode; |
| 8 | +use Doctrine\ORM\Query\AST\Node; |
| 9 | +use Doctrine\ORM\Query\Lexer; |
| 10 | +use Doctrine\ORM\Query\Parser; |
| 11 | +use Doctrine\ORM\Query\SqlWalker; |
| 12 | + |
| 13 | +/** |
| 14 | + * Implementation of PostgreSql ILIKE() function. |
| 15 | + * |
| 16 | + * @see https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-LIKE |
| 17 | + * |
| 18 | + * @example ILIKE(entity.field, 'text') |
| 19 | + * @example ILIKE(entity.field, entity2.field) |
| 20 | + * @example ILIKE(entity.field, ANY ARRAY(:texts)) |
| 21 | + */ |
| 22 | +class ILike extends FunctionNode |
| 23 | +{ |
| 24 | + private Node $field; |
| 25 | + |
| 26 | + private Node $text; |
| 27 | + |
| 28 | + private bool $multiple = false; |
| 29 | + |
| 30 | + public function parse(Parser $parser): void |
| 31 | + { |
| 32 | + $parser->match(Lexer::T_IDENTIFIER); |
| 33 | + $parser->match(Lexer::T_OPEN_PARENTHESIS); |
| 34 | + $this->field = $parser->StringPrimary(); |
| 35 | + $parser->match(Lexer::T_COMMA); |
| 36 | + |
| 37 | + $lexer = $parser->getLexer(); |
| 38 | + |
| 39 | + if ($lexer->isNextToken(Lexer::T_ANY)) { |
| 40 | + $parser->match(Lexer::T_ANY); |
| 41 | + $this->multiple = true; |
| 42 | + } |
| 43 | + |
| 44 | + $this->text = $parser->StringPrimary(); |
| 45 | + $parser->match(Lexer::T_CLOSE_PARENTHESIS); |
| 46 | + } |
| 47 | + |
| 48 | + public function getSql(SqlWalker $sqlWalker): string |
| 49 | + { |
| 50 | + return \sprintf( |
| 51 | + '(%s ILIKE %s)', |
| 52 | + $this->field->dispatch($sqlWalker), |
| 53 | + $this->multiple ? "ANY ({$this->text->dispatch($sqlWalker)})" : $this->text->dispatch($sqlWalker), |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
0 commit comments