| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LegalThings\DataEnricher\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LegalThings\DataEnricher\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use LegalThings\DataEnricher\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Merge multiple object into one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Merge implements Processor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     use Processor\Implementation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * Apply processing to a single node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param Node $node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 12 |  |     public function applyToNode(Node $node) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 12 |  |         $instruction = $node->getInstruction($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 12 |  |         $list = $this->resolve($instruction); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 12 |  |         if (isset($list) && !is_array($list)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |             throw new \Exception("Unable to apply {$this->property} processing instruction:" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |                 . " Expected an array, got a " . (is_object($list) ? get_class($list) . ' ' : '') . gettype($list)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 11 |  |         $result = $this->execute((array)$list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 10 |  |         $node->setResult($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Resolve processing nodes in the instruction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param array $list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 12 |  |     public function resolve($list) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 12 |  |         if ($list instanceof Node) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |             $list = $list->getResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 12 |  |         if (is_array($list)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 11 |  |             foreach ($list as &$item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 10 |  |                 if ($item instanceof Node) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |                     $item = $item->getResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 10 |  |                 if ($item instanceof \Traversable) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |                     $item = iterator_to_array($item); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 1 |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 11 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 11 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 12 |  |         return $list; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Merge properties of an object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param array $list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return \stdClass|array|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 11 |  |     protected function execute(array $list) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 11 |  |         if (empty($list)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 10 |  |         $scalar = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 10 |  |         foreach ($list as $key => &$item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 10 |  |             if (!isset($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |                 unset($list[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 9 |  |             if (is_object($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |                 $item = get_object_vars($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |             $scalar[] = is_scalar($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 10 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 10 |  |         if (count(array_unique($scalar)) > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |             throw new \Exception("Unable to apply {$this->property} processing instruction:" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |                 . " Mixture of scalar and non-scalar values"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 9 |  |         if (count($scalar) > 0 && $scalar[0]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 2 |  |             $result = join('', $list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 7 |  |             if (empty($list)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |             $result = call_user_func_array('array_merge', $list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             // Is associative array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 6 |  |             if (array_keys($result) !== array_keys(array_keys($result))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 5 |  |                 $result = (object)$result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 5 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 8 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |  |