| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Sonata\AdminBundle\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sonata\AdminBundle\Admin\FieldDescriptionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sonata\AdminBundle\Form\DataTransformer\BooleanToStringTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sonata\AdminBundle\Model\ModelManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sonata\AdminBundle\Templating\TemplateRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Form\DataTransformerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @author Peter Gribanov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | final class DataTransformerResolver implements DataTransformerResolverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var array<string, DataTransformerInterface> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $globalCustomTransformers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param array<string, DataTransformerInterface> $customGlobalTransformers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function __construct(array $customGlobalTransformers = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         foreach ($customGlobalTransformers as $fieldType => $dataTransformer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $this->addCustomGlobalTransformer($fieldType, $dataTransformer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     public function addCustomGlobalTransformer(string $fieldType, DataTransformerInterface $dataTransformer): void | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->globalCustomTransformers[$fieldType] = $dataTransformer; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function resolve( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         FieldDescriptionInterface $fieldDescription, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ModelManagerInterface $modelManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     ): ?DataTransformerInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $dataTransformer = $fieldDescription->getOption('data_transformer'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // allow override predefined transformers for 'date', 'boolean' and 'choice' field types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if ($dataTransformer instanceof DataTransformerInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             return $dataTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $fieldType = (string) $fieldDescription->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         // allow override predefined transformers on a global level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (\array_key_exists($fieldType, $this->globalCustomTransformers)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return $this->globalCustomTransformers[$fieldType]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // Handle date type has setter expect a DateTime object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (TemplateRegistry::TYPE_DATE === $fieldType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $this->globalCustomTransformers[$fieldType] = new DateTimeToStringTransformer(null, null, 'Y-m-d'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             return $this->globalCustomTransformers[$fieldType]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // Handle datetime type has setter expect a DateTime object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (TemplateRegistry::TYPE_DATETIME === $fieldType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->globalCustomTransformers[$fieldType] = new DateTimeToStringTransformer(null, null, 'Y-m-d H:i:s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return $this->globalCustomTransformers[$fieldType]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // Handle boolean type transforming the value into a boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (TemplateRegistry::TYPE_BOOLEAN === $fieldType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $this->globalCustomTransformers[$fieldType] = new BooleanToStringTransformer('1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $this->globalCustomTransformers[$fieldType]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // Handle entity choice association type, transforming the value into entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if (TemplateRegistry::TYPE_CHOICE === $fieldType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $className = $fieldDescription->getOption('class'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             if (null !== $className && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 // NEXT_MAJOR: Replace this call with "$className === $fieldDescription->getTargetModel()". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $this->hasFieldDescriptionAssociationWithClass($fieldDescription, $className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 return new ModelToIdTransformer($modelManager, $className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     private function hasFieldDescriptionAssociationWithClass( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         FieldDescriptionInterface $fieldDescription, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         string $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     ): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             method_exists($fieldDescription, 'getTargetModel') && $class === $fieldDescription->getTargetModel() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         ) || $class === $fieldDescription->getTargetEntity(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |  | 
            
                        
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: