| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\ORM\Query\AST\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\ORM\Query\Lexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\ORM\Query\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Doctrine\ORM\Query\SqlWalker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Doctrine\ORM\Query\TokenType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\ParserException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use MartinGeorgiev\Utils\DoctrineLexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use MartinGeorgiev\Utils\DoctrineOrm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author Martin Georgiev <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | abstract class BaseVariadicFunction extends BaseFunction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 124 |  |     protected function customizeFunction(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 124 |  |         $this->setFunctionPrototype(\sprintf('%s(%%s)', $this->getFunctionName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     abstract protected function getFunctionName(): string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return array<string> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     abstract protected function getNodeMappingPattern(): array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     abstract protected function getMinArgumentCount(): int; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     abstract protected function getMaxArgumentCount(): int; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 130 |  |     protected function feedParserWithNodes(Parser $parser): void | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 130 |  |         foreach ($this->getNodeMappingPattern() as $nodeMappingPattern) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             try { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 130 |  |                 $this->feedParserWithNodesForNodeMappingPattern($parser, $nodeMappingPattern); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 59 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 71 |  |             } catch (ParserException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 // swallow and continue with next pattern | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @throws InvalidArgumentForVariadicFunctionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @throws ParserException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 158 |  |     private function feedParserWithNodesForNodeMappingPattern(Parser $parser, string $nodeMappingPattern): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 158 |  |         $nodeMapping = \explode(',', $nodeMappingPattern); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 158 |  |         $lexer = $parser->getLexer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 158 |  |             $lookaheadType = DoctrineLexer::getLookaheadType($lexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 158 |  |             if ($lookaheadType === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 throw InvalidArgumentForVariadicFunctionException::atLeast($this->getFunctionName(), $this->getMinArgumentCount()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 158 |  |             $this->nodes[] = $parser->{$nodeMapping[0]}(); // @phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 30 |  |         } catch (\Throwable $throwable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 30 |  |             throw ParserException::withThrowable($throwable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 128 |  |         $shouldUseLexer = DoctrineOrm::isPre219(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 128 |  |         $isNodeMappingASimplePattern = \count($nodeMapping) === 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 128 |  |         $nodeIndex = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 128 |  |         while (($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS) !== $lookaheadType) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 128 |  |             if (($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA) === $lookaheadType) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 96 |  |                 $parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 // Check if we're about to exceed the maximum number of arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 // nodeIndex starts at 1 and counts up for each argument after the first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 // So when nodeIndex=1, we're about to add the 2nd argument (total: 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 // When nodeIndex=2, we're about to add the 3rd argument (total: 3) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 96 |  |                 $foundMoreNodesThanMappingExpected = ($nodeIndex + 1) > $this->getMaxArgumentCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 96 |  |                 if ($foundMoreNodesThanMappingExpected) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 26 |  |                     if ($this->getMinArgumentCount() === $this->getMaxArgumentCount()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |                         throw InvalidArgumentForVariadicFunctionException::exactCount($this->getFunctionName(), $this->getMinArgumentCount()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 25 |  |                     throw InvalidArgumentForVariadicFunctionException::between($this->getFunctionName(), $this->getMinArgumentCount(), $this->getMaxArgumentCount()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 96 |  |                 $expectedNodeIndex = $isNodeMappingASimplePattern ? 0 : $nodeIndex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 96 |  |                 $argumentCountExceedsMappingPatternExpectation = !\array_key_exists($expectedNodeIndex, $nodeMapping); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 96 |  |                 if ($argumentCountExceedsMappingPatternExpectation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     throw InvalidArgumentForVariadicFunctionException::unsupportedCombination( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                         $this->getFunctionName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                         \count($this->nodes) + 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                         'implementation defines fewer node mappings than the actually provided argument count' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 96 |  |                 $this->nodes[] = $parser->{$nodeMapping[$expectedNodeIndex]}(); // @phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 96 |  |                 $nodeIndex++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 128 |  |             $lookaheadType = DoctrineLexer::getLookaheadType($lexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         // Final validation ensures all arguments meet requirements, including any special rules in subclass implementations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 102 |  |         $this->validateArguments(...$this->nodes); // @phpstan-ignore-line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @throws InvalidArgumentForVariadicFunctionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 158 |  |     protected function validateArguments(Node ...$arguments): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 158 |  |         $minArgumentCount = $this->getMinArgumentCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 158 |  |         $maxArgumentCount = $this->getMaxArgumentCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 158 |  |         $argumentCount = \count($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 158 |  |         if ($minArgumentCount === $maxArgumentCount && $argumentCount !== $minArgumentCount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |             throw InvalidArgumentForVariadicFunctionException::exactCount($this->getFunctionName(), $this->getMinArgumentCount()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 157 |  |         if ($argumentCount < $minArgumentCount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 51 |  |             throw InvalidArgumentForVariadicFunctionException::atLeast($this->getFunctionName(), $this->getMinArgumentCount()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 106 |  |         if ($argumentCount > $maxArgumentCount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 28 |  |             throw InvalidArgumentForVariadicFunctionException::between($this->getFunctionName(), $this->getMinArgumentCount(), $this->getMaxArgumentCount()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 61 |  |     public function getSql(SqlWalker $sqlWalker): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 61 |  |         $dispatched = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 61 |  |         foreach ($this->nodes as $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 59 |  |             $dispatched[] = $node instanceof Node ? $node->dispatch($sqlWalker) : 'null'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 61 |  |         return \sprintf($this->functionPrototype, \implode(', ', $dispatched)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |