public function testSerialize() { $parameter = new Parameter('query', 'count', 'Count parameter'); $responseMessage = new ResponseMessage(200, 'Return', 'News'); $operation = new Operation('PUT', 'updatePet', 'Update an existing pet'); $operation->setNotes('foobar'); $operation->addParameter($parameter); $operation->addResponseMessage($responseMessage); $content = <<<JSON { "method": "PUT", "nickname": "updatePet", "summary": "Update an existing pet", "notes": "foobar", "parameters": [{ "paramType": "query", "name": "count", "description": "Count parameter" }], "responseMessages": [{ "code": 200, "message": "Return", "responseModel": "News" }] } JSON; $this->assertRecordEqualsContent($operation, $content); $this->assertEquals('PUT', $operation->getMethod()); $this->assertEquals('updatePet', $operation->getNickname()); $this->assertEquals('Update an existing pet', $operation->getSummary()); $this->assertEquals('foobar', $operation->getNotes()); $this->assertEquals([$parameter], $operation->getParameters()); $this->assertEquals([$responseMessage], $operation->getResponseMessages()); }
private function addOperationByComment(Api $api, DocBlock $doc, $httpMethod, array &$models) { $summary = $doc->getFirstAnnotation('summary'); $nickname = uniqid($doc->getFirstAnnotation('nickname') . '_'); $response = $doc->getFirstAnnotation('responseClass'); $operation = new Operation($httpMethod, $nickname, $response, $summary); $params = $doc->getAnnotation('parameter'); $dataTypes = array(); foreach ($params as $dfn) { if (substr($dfn, 0, 1) == '[') { $dfn = ltrim($dfn, '['); $dfn = rtrim($dfn, ']'); $required = false; } else { $required = true; } $parts = explode(' ', $dfn, 4); $type = isset($parts[0]) ? $parts[0] : null; $name = isset($parts[1]) ? $parts[1] : null; $dataType = isset($parts[2]) ? $parts[2] : null; $desc = isset($parts[3]) ? $parts[3] : null; switch (strtolower($type)) { case 'body': $parameter = new Parameter\Body($name, $desc, $dataType, $required); break; case 'header': $parameter = new Parameter\Header($name, $desc, $dataType, $required); break; case 'path': $parameter = new Parameter\Path($name, $desc, $dataType, $required); break; case 'query': $parameter = new Parameter\Query($name, $desc, $dataType, $required); break; } if ($parameter instanceof ParameterAbstract) { $operation->addParameter($parameter); } // if the datatype is not scalar add the model to the api if (!ParameterAbstract::isScalar($dataType)) { $dataTypes[] = $dataType; } } $api->addOperation($operation); $dataTypes = array_unique($dataTypes); if (!empty($dataTypes)) { foreach ($dataTypes as $dataType) { $models[] = $dataType; } } }
protected function getApis(Resource $resource) { $api = new Api(ApiGeneration::transformRoutePlaceholder($resource->getPath())); $description = $resource->getDescription(); $methods = $resource->getMethods(); if (!empty($description)) { $api->setDescription($description); } foreach ($methods as $method) { // get operation name $request = $method->getRequest(); $response = $this->getSuccessfulResponse($method); $description = $method->getDescription(); $entityName = ''; if ($request instanceof SchemaInterface) { $entityName = $request->getDefinition()->getName(); } elseif ($response instanceof SchemaInterface) { $entityName = $response->getDefinition()->getName(); } // create new operation $operation = new Operation($method->getName(), strtolower($method->getName()) . ucfirst($entityName)); if (!empty($description)) { $operation->setSummary($description); } // path parameter $parameters = $resource->getPathParameters()->getDefinition(); foreach ($parameters as $parameter) { $param = new Parameter('path', $parameter->getName(), $parameter->getDescription(), $parameter->isRequired()); $this->setParameterType($parameter, $param); $operation->addParameter($param); } // query parameter $parameters = $method->getQueryParameters()->getDefinition(); foreach ($parameters as $parameter) { $param = new Parameter('query', $parameter->getName(), $parameter->getDescription(), $parameter->isRequired()); $this->setParameterType($parameter, $param); $operation->addParameter($param); } // request body if ($request instanceof SchemaInterface) { $description = $request->getDefinition()->getDescription(); $type = strtolower($method->getName()) . 'Request'; $parameter = new Parameter('body', 'body', $description, true); $parameter->setType($type); $operation->addParameter($parameter); } // response body $responses = $method->getResponses(); foreach ($responses as $statusCode => $response) { $type = strtolower($method->getName()) . 'Response'; $message = $response->getDefinition()->getDescription() ?: 'Response'; $operation->addResponseMessage(new ResponseMessage($statusCode, $message, $type)); } $api->addOperation($operation); } return array($api); }