| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright © Vaimo Group. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * See LICENSE_VAIMO.txt for license details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Vaimo\ComposerPatches\Patch; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class ListNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var \Vaimo\ComposerPatches\Patch\Definition\Exploder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $definitionExploder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var \Vaimo\ComposerPatches\Patch\Definition\Normalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $definitionNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var \Vaimo\ComposerPatches\Patch\DefinitionList\Sanitizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $patchListSanitizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param \Vaimo\ComposerPatches\Patch\Definition\Exploder $definitionExploder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param \Vaimo\ComposerPatches\Patch\Definition\Normalizer $definitionNormalizer | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         \Vaimo\ComposerPatches\Patch\Definition\Exploder $definitionExploder, | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         \Vaimo\ComposerPatches\Patch\Definition\Normalizer $definitionNormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->definitionExploder = $definitionExploder; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->definitionNormalizer = $definitionNormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $this->patchListSanitizer = new \Vaimo\ComposerPatches\Patch\DefinitionList\Sanitizer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function normalize(array $list, array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $result = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $sanitizedList = $this->patchListSanitizer->getSanitizedList($list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         foreach ($sanitizedList as $target => $packagePatches) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $result[$target] = $this->createNormalizedList($target, $packagePatches, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return array_filter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             array_map('array_filter', $result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private function createNormalizedList($target, $patches, $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $result = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         foreach ($patches as $patchLabel => $patchConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $definitionItems = $this->definitionExploder->process( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $patchLabel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $patchConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             foreach ($definitionItems as $patchItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 list($label, $data) = $patchItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $result[] = $this->definitionNormalizer->process( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     $target, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     $label, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |