| 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\Retrofit\Internal\AnnotationHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Tebru\AnnotationReader\AbstractAnnotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Tebru\Retrofit\Annotation\Encodable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Tebru\Retrofit\Annotation\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Tebru\Retrofit\AnnotationHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Tebru\Retrofit\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Tebru\Retrofit\Internal\ParameterHandler\FieldParamHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Tebru\Retrofit\ServiceMethodBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Tebru\Retrofit\StringConverter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Class FieldAnnotHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Nate Brunette <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | final class FieldAnnotHandler implements AnnotationHandler | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Set the content type to form encoded and adds a parameter handler for individual fields | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param Field|AbstractAnnotation $annotation The annotation to handle | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @param ServiceMethodBuilder $serviceMethodBuilder Used to construct a [@see ServiceMethod] | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param Converter|StringConverter $converter Converter used to convert types before sending to service method | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param int|null $index The position of the parameter or null if annotation does not reference parameter | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 4 |  |     public function handle( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         AbstractAnnotation $annotation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ServiceMethodBuilder $serviceMethodBuilder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ?Converter $converter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ?int $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 4 |  |         if (!$annotation instanceof Encodable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |             throw new InvalidArgumentException('Retrofit: Annotation must be encodable'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 3 |  |         if (!$converter instanceof StringConverter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |             throw new InvalidArgumentException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |                 'Retrofit: Converter must be a StringConverter, %s found', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |                 \gettype($converter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |         $serviceMethodBuilder->setIsFormUrlEncoded(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |         $serviceMethodBuilder->addParameterHandler( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |             $index, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |             new FieldParamHandler($converter, $annotation->getValue(), $annotation->isEncoded()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 | 2 |  |     } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  |