| 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 JsonSchema\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author John Kleijn <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class RequestValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var OperationObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $operationObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param OperationObject $operationObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(OperationObject $operationObject = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         if ($operationObject) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             $this->setOperationObject($operationObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param OperationObject $operationObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function setOperationObject(OperationObject $operationObject) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $this->operationObject = $operationObject; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @throws InvalidParametersException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @throws UnsupportedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function validateRequest(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $validator = new Validator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $validator->check( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->assembleParameterDataForValidation($request), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $this->operationObject->getRequestSchema() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if (!$validator->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             throw new InvalidParametersException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 "Parameters incompatible with operation schema: " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 . implode(', ', $validator->getErrors()[0]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $validator->getErrors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return \stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @throws UnsupportedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     private function assembleParameterDataForValidation(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |          * TODO Hack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |          * @see https://github.com/kleijnweb/swagger-bundle/issues/24 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $content = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if ($request->getContent()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $content = json_decode($request->getContent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             //TODO UT this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $content = (is_array($content) && isset($content[0])) ? $content : (object)$content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $parameters = new \stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         foreach ($this->operationObject->getDefinition()->parameters as $paramDefinition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $paramName = $paramDefinition->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             if (!$request->attributes->has($paramName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             if ($paramDefinition->in === 'body' && $content !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $parameters->$paramName = $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $parameters->$paramName = $request->attributes->get($paramName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |              * If value already coerced into \DateTime object, get the raw value for validation instead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |              * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |              * TODO Keep raw value of attributes around | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             if ($parameters->$paramName instanceof \DateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 if ($paramDefinition->in === 'query') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     $parameters->$paramName = $request->query->get($paramName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 } elseif ($paramDefinition->in === 'header') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     $parameters->$paramName = $request->headers->get($paramName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 } elseif ($paramDefinition->in === 'path') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     if ($paramDefinition->format === 'date') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                         $parameters->$paramName = $parameters->$paramName->format('Y-m-d'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     if ($paramDefinition->format === 'date-time') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                         $parameters->$paramName = $parameters->$paramName->format(\DateTime::W3C); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return $parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  |