| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LegalThings\DataEnricher\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LegalThings\DataEnricher\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use LegalThings\DataEnricher\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use LegalThings\DataEnricher\Processor\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Mustache_Engine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Process string as Mustache template | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | class Mustache implements Processor | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     use Processor\Implementation, | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         Helper\GetByReference | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         Helper\GetByReference::withSourceAndTarget insteadof Processor\Implementation; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * Apply processing to a single node | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @param Node $node | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 4 |  |     public function applyToNode(Node $node) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 4 |  |         $template = $node->getInstruction($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 4 |  |         if (!is_string($template) && !is_array($template) && !is_object($template)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             return trigger_error("Unable to parse given template of type: " . gettype($template), E_WARNING); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 4 |  |         $result = $this->getParsedResult($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 4 |  |         $node->setResult($result); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 | 4 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * Parse a template by mustache if possible and return the result | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @param mixed $template | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @return mixed $result | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 4 |  |     protected function getParsedResult($template) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 4 |  |         if (is_string($template)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 3 |  |             return $this->parse($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |         } elseif (is_array($template)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             return array_map([$this, 'parse'], $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |         } elseif (is_object($template)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |             return $this->parseObject($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $template; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * Parse an object with mustache | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @param  object $template | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @return object $result | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |     protected function parseObject($template) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |         $result = new \stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |         foreach ($template as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |             $parsedKey = $this->parse($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |             $parsedValue = $this->getParsedResult($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |             $result->$parsedKey = $parsedValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * Parse as mustache template | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param string $template | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |     protected function parse($template) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  |         $data = get_object_vars($this->source) + ['@' => $this->target]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |         $mustache = new Mustache_Engine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  |         return $mustache->render($template, $data); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |