| 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_INSERT(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Inserts a new value into a JSONB field at the specified path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * If the path already exists, the value is not changed unless the last parameter is true. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @see https://www.postgresql.org/docs/16/functions-json.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @since 0.10 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Martin Georgiev <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @example Using it in DQL with path and value: "SELECT JSONB_INSERT(e.jsonbData, '{country}', '{\"iso_3166_a3_code\":\"BGR\"}') FROM Entity e" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @example Using it in DQL with create_if_missing flag: "SELECT JSONB_INSERT(e.jsonbData, '{country}', '{\"iso_3166_a3_code\":\"BGR\"}', true) FROM Entity e" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class JsonbInsert extends BaseVariadicFunction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     use BooleanValidationTrait; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 4 |  |     protected function customizeFunction(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 4 |  |         $this->setFunctionPrototype('jsonb_insert(%s)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 4 |  |     protected function validateArguments(Node ...$arguments): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 4 |  |         $argumentCount = \count($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 4 |  |         if ($argumentCount < 3 || $argumentCount > 4) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |             throw InvalidArgumentForVariadicFunctionException::between('jsonb_insert', 3, 4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         // Validate that the fourth parameter is a valid boolean if provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |         if ($argumentCount === 4) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |             $this->validateBoolean($arguments[3], 'JSONB_INSERT'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |  |