| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace MartinGeorgiev\Doctrine\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 |  |  | use Doctrine\ORM\Query\TokenType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use MartinGeorgiev\Utils\DoctrineLexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use MartinGeorgiev\Utils\DoctrineOrm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Implementation of PostgreSQL CAST(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @see https://www.postgresql.org/docs/17/sql-createcast.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @see https://github.com/beberlei/DoctrineExtensions/blob/f3536d881637f6ddc7ca1d6595d18c15e06eb1d9/src/Query/Mysql/Cast.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @since 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author Mathieu Piot <https://github.com/mpiot> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | class Cast extends FunctionNode | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public Node $sourceType; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public string $targetType; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 1 |  |     public function parse(Parser $parser): void | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 1 |  |         $shouldUseLexer = DoctrineOrm::isPre219(); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 1 |  |         $parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 1 |  |         $parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |         $this->sourceType = $parser->SimpleArithmeticExpression(); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |         $parser->match($shouldUseLexer ? Lexer::T_AS : TokenType::T_AS); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |         $parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |         $lexer = $parser->getLexer(); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |         $type = DoctrineLexer::getTokenValue($lexer); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |         if (!\is_string($type)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         // Handle parameterized types (e.g., DECIMAL(10, 2)) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |         if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |             $parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |             $parameter = $parser->Literal(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 1 |  |             $parameters = [$parameter->value]; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |             if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |                 while ($lexer->isNextToken($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 1 |  |                     $parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |                     $parameter = $parser->Literal(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |                     $parameters[] = $parameter->value; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |             $parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 1 |  |             $type .= '('.\implode(', ', $parameters).')'; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         // Handle array types by checking if the next token is '[' | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         // Since brackets are not recognized as specific tokens, we need to check the token value | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 1 |  |         $nextTokenValue = DoctrineLexer::getLookaheadValue($lexer); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |         if ($nextTokenValue === '[') { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             // Consume the '[' token | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  |             $parser->match($shouldUseLexer ? Lexer::T_NONE : TokenType::T_NONE); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             // Check for the closing ']' token | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 1 |  |             $nextTokenValue = DoctrineLexer::getLookaheadValue($lexer); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |             if ($nextTokenValue === ']') { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 1 |  |                 $parser->match($shouldUseLexer ? Lexer::T_NONE : TokenType::T_NONE); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  |                 $type .= '[]'; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  |         $this->targetType = $type; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 1 |  |         $parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |     public function getSql(SqlWalker $sqlWalker): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 | 1 |  |         return \sprintf('cast(%s as %s)', $this->sourceType->dispatch($sqlWalker), $this->targetType); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |