| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright (c) Nate Brunette. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Distributed under the MIT License (http://opensource.org/licenses/MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Tebru\Gson\TypeAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Tebru\Gson\Internal\TypeAdapterProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Tebru\Gson\ReaderContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Tebru\Gson\TypeAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Tebru\Gson\WriterContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Tebru\PhpType\TypeToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Class WildcardTypeAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Nate Brunette <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class WildcardTypeAdapter extends TypeAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var TypeAdapterProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $typeAdapterProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param TypeAdapterProvider $typeAdapterProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct(TypeAdapterProvider $typeAdapterProvider) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 18 |  |         $this->typeAdapterProvider = $typeAdapterProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 18 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Read the next value, convert it to its type and return it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param ReaderContext $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function read($value, ReaderContext $context) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 10 |  |         $adapter = $this->typeAdapterProvider->getAdapter(TypeToken::createFromVariable($value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 10 |  |         return $adapter->read($value, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 9 |  |      * Write the value to the writer for the type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 8 |  |      * @param WriterContext $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 7 |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 | 2 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 2 |  |     public function write($value, WriterContext $context) | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 6 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 2 |  |         $adapter = $this->typeAdapterProvider->getAdapter(TypeToken::createFromVariable($value)); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 4 |  |         return $adapter->write($value, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  |