| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Victoire\Bundle\BusinessEntityBundle\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\PropertyAccess\PropertyAccessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessProperty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Parameter Converter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * ref: victoire_business_entity.converter.parameter_converter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class ParameterConverter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Replace the code string with the value of the entity attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param string           $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param BusinessProperty $businessProperty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param object           $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return string The updated string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function convertFromEntity($string, BusinessProperty $businessProperty, $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         //test parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if ($entity === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             throw new \Exception('The parameter entity can not be null'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         //the attribute to set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $entityProperty = $businessProperty->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         //the value of the attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $accessor = new PropertyAccessor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $attributeValue = $accessor->getValue($entity, $entityProperty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return $this->convert($string, 'item.'.$entityProperty, $attributeValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Replace the code string with the value of the entity attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $entityProperty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $attributeValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return string The updated string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |     public function convert($string, $entityProperty, $attributeValue) | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         //the string to replace | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         $stringToReplace = '{{'.$entityProperty.'}}'; | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         //we provide a default value | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         if ($attributeValue === null) { | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             $attributeValue = ''; | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         //we replace the string | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         return str_replace($stringToReplace, $attributeValue, $string); | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  |