| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace CCT\Component\ODMElasticsearch\Transformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use CCT\Component\ODMElasticsearch\Transformer\Exception\TransformationFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use CCT\Component\ODMElasticsearch\Transformer\Visitor\ReverseVisitorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use CCT\Component\ODMElasticsearch\Transformer\Visitor\VisitorInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class ElasticsearchTransformer implements DataTransformerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * @var DataNavigatorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     protected $dataNavigator; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @var VisitorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected $visitor; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @var ReverseVisitorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected $reverseVisitor; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * ElasticsearchTransformer constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param DataNavigatorInterface $dataNavigator | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @param VisitorInterface $visitor | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param ReverseVisitorInterface $reverseVisitor | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         DataNavigatorInterface $dataNavigator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         VisitorInterface $visitor, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ReverseVisitorInterface $reverseVisitor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->dataNavigator = $dataNavigator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->visitor = $visitor; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |         $this->reverseVisitor = $reverseVisitor; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * Transforms a value from the original representation to a transformed representation. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param mixed $value The value in the original representation | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return mixed The value in the transformed representation | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @throws TransformationFailedException When the transformation fails. | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function transform($value) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->visitor->setDataNavigator($this->dataNavigator); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         return $this->dataNavigator->navigate($value, $this->visitor, null); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * Transforms a value from the transformed representation to its original | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * representation. | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @param mixed $value The value in the transformed representation | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param object $object Transform values into this object | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return mixed The value in the original representation | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @throws TransformationFailedException When the transformation fails. | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function reverseTransform($value, $object = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (null === $object || false === \is_object($object)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             throw new TransformationFailedException('You must pass an object to the reverseTransform'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $config = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'type' => 'object', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'class' => \get_class($object), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'params' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 'populate_object' => $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->reverseVisitor->setDataNavigator($this->dataNavigator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |         return $this->dataNavigator->navigate($value, $this->reverseVisitor, $config); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |