| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright (c) Nate Brunette. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Distributed under the MIT License (http://opensource.org/licenses/MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Tebru\Gson\Internal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Tebru\Gson\InstanceCreator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Tebru\Gson\Internal\ObjectConstructor\CreateFromInstanceCreator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Tebru\Gson\Internal\ObjectConstructor\CreateFromReflectionClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Tebru\Gson\Internal\ObjectConstructor\CreateWithoutArguments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Tebru\Gson\PhpType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class ConstructorConstructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * This class acts as an ObjectConstructor factory.  It takes in a map of instance creators and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * wraps object creation in an [@see ObjectConstructor].  This does expensive operations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * (like reflection) once and allows it to be cached for subsequent calls. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author Nate Brunette <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | final class ConstructorConstructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * An array of [@see InstanceCreator] objects that can be used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * for custom instantiation of a class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var InstanceCreator[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $instanceCreators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param InstanceCreator[] $instanceCreators | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 5 |  |     public function __construct(array $instanceCreators = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 5 |  |         $this->instanceCreators = $instanceCreators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Get the correct [@see ObjectConstructor] to use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param PhpType $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return ObjectConstructor | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 51 | 4 |  |     public function get(PhpType $type): ObjectConstructor | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 53 | 4 |  |         $class = $type->getType(); | 
            
                                                        
            
                                    
            
            
                | 54 | 4 |  |         foreach ($this->instanceCreators as $instanceCreatorClass => $creator) { | 
            
                                                        
            
                                    
            
            
                | 55 | 2 |  |             if ($type->isA($instanceCreatorClass)) { | 
            
                                                        
            
                                    
            
            
                | 56 | 2 |  |                 return new CreateFromInstanceCreator($creator, $type); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         try { | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |             // attempt to instantiate a new class without any arguments | 
            
                                                        
            
                                    
            
            
                | 62 | 2 |  |             new $class(); | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 | 1 |  |             return new CreateWithoutArguments($class); | 
            
                                                        
            
                                    
            
            
                | 65 | 1 |  |         } catch (Throwable $throwable) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 66 | 1 |  |             return new CreateFromReflectionClass($class); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  |