| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Thunder\Serializard\Normalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author Tomasz Kowalczyk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | final class JmsSerializerNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $path; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct($path) | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $this->path = $path; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __invoke($var) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $data = file_get_contents($this->path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         if(!$data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             throw new \RuntimeException(sprintf('Failed to read file at path %s!', $this->path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $yaml = Yaml::parse($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $keys = array_keys($yaml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $yaml = $yaml[$keys[0]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         if(!$yaml) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             throw new \InvalidArgumentException('No config at '.$this->path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $hasPolicy = array_key_exists('exclusion_policy', $yaml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         if(!$hasPolicy || ($hasPolicy && 'ALL' !== $yaml['exclusion_policy'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             throw new \RuntimeException(sprintf('This serializer supports only ALL, %s given!', $yaml['exclusion_policy'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $ref = new \ReflectionObject($var); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         foreach($yaml['properties'] as $key => $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             if($config['expose'] !== true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $name = array_key_exists('serialized_name', $config) ? $config['serialized_name'] : $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             if($ref->hasProperty($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $prop = $ref->getProperty($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $prop->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 $result[$name] = $prop->getValue($var); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $method = 'get'.ucfirst($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             if(method_exists($var, $method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 $result[$name] = call_user_func_array([$var, $method], []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             // no method or property found, skip current key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  |