. */ declare(strict_types=1); namespace App\Doctrine\Functions; use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\ORM\Query\AST\Functions\FunctionNode; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; use Doctrine\ORM\Query\TokenType; /** * A platform invariant version of the case-insensitive LIKE operation. * On MySQL and SQLite this is the normal LIKE, but on PostgreSQL it is the ILIKE operator. */ class ILike extends FunctionNode { public $value = null; public $expr = null; public function parse(Parser $parser): void { $parser->match(TokenType::T_IDENTIFIER); $parser->match(TokenType::T_OPEN_PARENTHESIS); $this->value = $parser->StringPrimary(); $parser->match(TokenType::T_COMMA); $this->expr = $parser->StringExpression(); $parser->match(TokenType::T_CLOSE_PARENTHESIS); } public function getSql(SqlWalker $sqlWalker): string { $platform = $sqlWalker->getConnection()->getDatabasePlatform(); // if ($platform instanceof AbstractMySQLPlatform || $platform instanceof SQLitePlatform) { $operator = 'LIKE'; } elseif ($platform instanceof PostgreSQLPlatform) { //Use the case-insensitive operator, to have the same behavior as MySQL $operator = 'ILIKE'; } else { throw new \RuntimeException('Platform ' . gettype($platform) . ' does not support case insensitive like expressions.'); } return '(' . $this->value->dispatch($sqlWalker) . ' ' . $operator . ' ' . $this->expr->dispatch($sqlWalker) . ')'; } }