| 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 MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\BooleanValidationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Implementation of PostgreSQL JSONB_SET(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Returns the target jsonb with the section designated by path replaced by the new value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * or with the new value added if create_missing is true (default is true) and the item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * designated by path does not exist. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * As with the path orientated operators, negative integers that appear in path count from the end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * of JSON arrays. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @see https://www.postgresql.org/docs/16/functions-json.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @since 0.10 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Martin Georgiev <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @example Using it in DQL with path and value: "SELECT JSONB_SET(e.jsonbData, '{address,city}', '\"Sofia\"') FROM Entity e" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @example Using it in DQL with create_if_missing flag: "SELECT JSONB_SET(e.jsonbData, '{address,city}', '\"Sofia\"', false) FROM Entity e" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class JsonbSet extends BaseVariadicFunction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     use BooleanValidationTrait; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 4 |  |     protected function getNodeMappingPattern(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 4 |  |         return ['StringPrimary']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 4 |  |     protected function customizeFunction(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 4 |  |         $this->setFunctionPrototype('jsonb_set(%s)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 4 |  |     protected function validateArguments(Node ...$arguments): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 4 |  |         $argumentCount = \count($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  |         if ($argumentCount < 3 || $argumentCount > 4) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |             throw InvalidArgumentForVariadicFunctionException::between('jsonb_set', 3, 4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         // Validate that the fourth parameter is a valid boolean if provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |         if ($argumentCount === 4) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |             $this->validateBoolean($arguments[3], 'JSONB_SET'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  |