| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the KleijnWeb\SwaggerBundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace KleijnWeb\SwaggerBundle\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use KleijnWeb\SwaggerBundle\Document\OperationObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use KleijnWeb\SwaggerBundle\Exception\UnsupportedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use KleijnWeb\SwaggerBundle\Exception\MalformedContentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author John Kleijn <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | class RequestCoercer | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @var ContentDecoder | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     private $contentDecoder; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @param ContentDecoder $contentDecoder | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct(ContentDecoder $contentDecoder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->contentDecoder = $contentDecoder; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @param Request         $request | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @param OperationObject $operationObject | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @throws MalformedContentException | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @throws UnsupportedException | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function coerceRequest(Request $request, OperationObject $operationObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $content = $this->contentDecoder->decodeContent($request, $operationObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $paramBagMapping = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'query'  => 'query', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'path'   => 'attributes', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'header' => 'headers' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         foreach ($operationObject->getDefinition()->parameters as $paramDefinition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $paramName = $paramDefinition->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if ($paramDefinition->in === 'body') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 if ($content !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     $request->attributes->set($paramName, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $paramBagName = $paramBagMapping[$paramDefinition->in]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             if (!$request->$paramBagName->has($paramName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $request->attributes->set( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $paramName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 ParameterCoercer::coerceParameter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     $paramDefinition, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     $request->$paramBagName->get($paramName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |