| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace ElevenLabs\Api\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use ElevenLabs\Api\Definition\RequestDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ElevenLabs\Api\Definition\Parameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ElevenLabs\Api\Definition\Parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ElevenLabs\Api\Definition\RequestDefinitions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ElevenLabs\Api\Definition\ResponseDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ElevenLabs\Api\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use ElevenLabs\Api\JsonSchema\Uri\YamlUriRetriever; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use JsonSchema\SchemaStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use JsonSchema\Uri\UriResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use JsonSchema\Uri\UriRetriever; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Create a schema definition from a Swagger file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class SwaggerSchemaFactory implements SchemaFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param string $schemaFile (must start with a scheme: file://, http:// or https://) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return Schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 5 |  |     public function createSchema($schemaFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 5 |  |         $schema = $this->resolveSchemaFile($schemaFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |         $host = isset($schema->host) ? $schema->host : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |         $basePath = isset($schema->basePath) ? $schema->basePath : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |         $schemes = isset($schema->schemes) ? $schema->schemes : ['http']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |         return new Schema( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |             $this->createRequestDefinitions($schema), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |             $basePath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             $host, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |             $schemes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $schemaFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |     protected function resolveSchemaFile($schemaFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |         $extension = pathinfo($schemaFile, PATHINFO_EXTENSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |         switch ($extension) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |             case 'yml': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 5 |  |             case 'yaml': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 if (!class_exists(Yaml::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     // @codeCoverageIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     throw new \InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                         'You need to require the "symfony/yaml" component in order to parse yml files' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     // @codeCoverageIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $uriRetriever = new YamlUriRetriever(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 5 |  |             case 'json': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  |                 $uriRetriever = new UriRetriever(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |                 throw new \InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |                     sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |                         'file "%s" does not provide a supported extension choose either json, yml or yaml', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |                         $schemaFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |         $schemaStorage = new SchemaStorage( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 4 |  |             $uriRetriever, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |             new UriResolver() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |         $schema = $schemaStorage->getSchema($schemaFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // JsonSchema normally defers resolution of $ref values until validation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         // That does not work for us, because we need to have the complete schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // to build definitions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |         $this->expandSchemaReferences($schema, $schemaStorage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 3 |  |         return $schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 4 |  |     private function expandSchemaReferences(\stdClass &$schema, SchemaStorage $schemaStorage) | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 4 |  |         foreach ($schema as &$member) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 4 |  |             if (is_object($member) && property_exists($member, '$ref') && is_string($member->{'$ref'})) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                 $member = $schemaStorage->resolveRef($member->{'$ref'}); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 4 |  |             if (is_object($member) || is_array($member)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 4 |  |                 $this->expandSchemaReferences($member, $schemaStorage); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param \stdClass $schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return RequestDefinitions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 3 |  |     protected function createRequestDefinitions(\stdClass $schema) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |         $definitions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |         $defaultConsumedContentTypes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |         $defaultProducedContentTypes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 3 |  |         if (isset($schema->consumes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $defaultConsumedContentTypes = $schema->consumes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  |         if (isset($schema->produces)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $defaultProducedContentTypes = $schema->produces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 3 |  |         $basePath = isset($schema->basePath) ? $schema->basePath : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 3 |  |         foreach ($schema->paths as $pathTemplate => $methods) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 3 |  |             foreach ($methods as $method => $definition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 3 |  |                 $method = strtoupper($method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 3 |  |                 $contentTypes = $defaultConsumedContentTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 3 |  |                 if (isset($definition->consumes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     $contentTypes = $definition->consumes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 3 |  |                 if (!isset($definition->operationId)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |                     throw new \LogicException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |                         sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |                             'You need to provide an operationId for %s %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |                             $method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |                             $pathTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 2 |  |                 if (empty($contentTypes) && $this->containsBodyParametersLocations($definition)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                     $contentTypes = $this->guessSupportedContentTypes($definition, $pathTemplate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  |                 if (!isset($definition->responses)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 1 |  |                     throw new \LogicException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 1 |  |                         sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |                             'You need to specify at least one response for %s %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 1 |  |                             $method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 1 |  |                             $pathTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |                 if (!isset($definition->parameters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |                     $definition->parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |                 $requestParameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 1 |  |                 foreach ($definition->parameters as $parameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                     $requestParameters[] = $this->createParameter($parameter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |                 $responseContentTypes = $defaultProducedContentTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 1 |  |                 if (isset($definition->produces)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                     $responseContentTypes = $definition->produces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 1 |  |                 $responseDefinitions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 1 |  |                 foreach ($definition->responses as $statusCode => $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 1 |  |                     $responseDefinitions[] = $this->createResponseDefinition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 1 |  |                         $statusCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 1 |  |                         $responseContentTypes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 1 |  |                         $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 1 |  |                 $definitions[] = new RequestDefinition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 1 |  |                     $method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 1 |  |                     $definition->operationId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 1 |  |                     $basePath.$pathTemplate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 1 |  |                     new Parameters($requestParameters), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 1 |  |                     $contentTypes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 1 |  |                     $responseDefinitions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 1 |  |         return new RequestDefinitions($definitions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 2 |  |     private function containsBodyParametersLocations(\stdClass $definition) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 2 |  |         if (!isset($definition->parameters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 2 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         foreach ($definition->parameters as $parameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             if (isset($parameter->in) && \in_array($parameter->in, Parameter::BODY_LOCATIONS, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      * @param \stdClass $definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |      * @param string $pathTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |     private function guessSupportedContentTypes(\stdClass $definition, $pathTemplate) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         if (!isset($definition->parameters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         $bodyLocations = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         foreach ($definition->parameters as $parameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |             if (isset($parameter->in) && \in_array($parameter->in, Parameter::BODY_LOCATIONS, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |                 $bodyLocations[] = $parameter->in; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         if (count($bodyLocations) > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             throw new \LogicException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |                     'Parameters cannot have %s locations at the same time in %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |                     implode(' and ', $bodyLocations), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |                     $pathTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |         if (count($bodyLocations) === 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |             return [Parameter::BODY_LOCATIONS_TYPES[current($bodyLocations)]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 1 |  |     protected function createResponseDefinition($statusCode, array $defaultProducedContentTypes, \stdClass $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 | 1 |  |         $allowedContentTypes = $defaultProducedContentTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 | 1 |  |         $parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 | 1 |  |         if (isset($response->schema)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |             $parameters[] = $this->createParameter((object) [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |                 'in' => 'body', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |                 'name' => 'body', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |                 'required' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |                 'schema' => $response->schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 | 1 |  |         if (isset($response->headers)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |             foreach ($response->headers as $headerName => $schema) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |                 $schema->in = 'header'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |                 $schema->name = $headerName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |                 $schema->required = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |                 $parameters[] = $this->createParameter($schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 | 1 |  |         return new ResponseDefinition($statusCode, $allowedContentTypes, new Parameters($parameters)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |      * Create a Parameter from a swagger parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |      * @param \stdClass $parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |      * @return Parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |     protected function createParameter(\stdClass $parameter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |         $parameter = get_object_vars($parameter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |         $location = $parameter['in']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         $name = $parameter['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |         $schema = isset($parameter['schema']) ? $parameter['schema'] : new \stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         $required = isset($parameter['required']) ? $parameter['required'] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |         unset($parameter['in']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |         unset($parameter['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |         unset($parameter['required']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         unset($parameter['schema']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         // Every remaining parameter may be json schema properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         foreach ($parameter as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |             $schema->{$key} = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |         // It's not relevant to validate file type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |         if (isset($schema->format) && $schema->format === 'file') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |             $schema = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |         return new Parameter($location, $name, $required, $schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 302 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 303 |  |  |  |