| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Softonic\GraphQL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Softonic\GraphQL\Config\MutationTypeConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Softonic\GraphQL\Mutation\Collection as MutationCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Softonic\GraphQL\Mutation\Item as MutationItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Softonic\GraphQL\Mutation\MutationObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Softonic\GraphQL\Query\Collection as QueryCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Softonic\GraphQL\Query\Item as QueryItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Softonic\GraphQL\Query\ReadObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class MutationBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const SOURCE_ROOT_PATH = '.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var ReadObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var MutationTypeConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $mutationTypeConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param array<MutationTypeConfig> $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 68 |  |     public function __construct(array $config, ReadObject $source) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 68 |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 68 |  |         $this->source = $source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 68 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 68 |  |     public function build(): MutationObject | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 68 |  |         $mutationVariables = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 68 |  |         foreach ($this->config as $variableName => $mutationTypeConfig) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 68 |  |             $this->mutationTypeConfig = $mutationTypeConfig; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 68 |  |             $path                     = self::SOURCE_ROOT_PATH; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 68 |  |             $config                   = $this->mutationTypeConfig->get($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 68 |  |             if ($config->type === MutationCollection::class) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 6 |  |                 $arguments = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 6 |  |                 foreach ($this->source as $sourceItem) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 6 |  |                     $mutationItemArguments = $this->generateMutationArguments($sourceItem, $path); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 6 |  |                     $arguments[] = new MutationItem($mutationItemArguments, $config->children); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 6 |  |                 $mutationVariables[$variableName] = new $config->type($arguments, $config->children); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 62 |  |                 $arguments = $this->generateMutationArguments($this->source, $path); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 65 |  |                 $mutationVariables[$variableName] = new $config->type($arguments, $config->children); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 68 |  |         return new MutationItem($mutationVariables, $this->config); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 68 |  |     private function generateMutationArguments(QueryItem $source, string $path): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 68 |  |         $arguments = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 68 |  |         foreach ($source as $sourceKey => $sourceValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             // Having a property that is an item is not handled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 68 |  |             if ($sourceValue instanceof QueryCollection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 50 |  |                 $childPath   = $this->createPathFromParent($path, $sourceKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 50 |  |                 $childConfig = $this->mutationTypeConfig->get($childPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 50 |  |                 $arguments[$sourceKey] = $this->mutateChild($childConfig, $sourceValue, $childPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 68 |  |                 $arguments[$sourceKey] = $sourceValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 68 |  |         return $arguments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 50 |  |     private function createPathFromParent(string $parent, string $child): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 50 |  |         return ('.' === $parent) ? ".{$child}" : "{$parent}.{$child}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 50 |  |     private function mutateChild( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         MutationTypeConfig $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         QueryCollection $sourceCollection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     ): MutationObject { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 50 |  |         $arguments = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 50 |  |         if (is_null($config->linksTo)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 44 |  |             foreach ($config->children as $key => $childConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 44 |  |                 $childPath       = $this->createPathFromParent($path, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 44 |  |                 $arguments[$key] = $this->mutateChild($childConfig, $sourceCollection, $childPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 50 |  |             foreach ($sourceCollection as $sourceItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 48 |  |                 $itemArguments = $this->generateMutationArguments($sourceItem, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 48 |  |                 $arguments[] = new MutationItem($itemArguments, $config->children); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 50 |  |         return new $config->type($arguments, $config->children); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |  |