Beispiel #1
0
 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);
 }
Beispiel #2
0
    public function testSerialize()
    {
        $operation = new Operation('PUT', 'updatePet', 'Update an existing pet');
        $api = new Api('/foo', 'Foobar');
        $api->addOperation($operation);
        $content = <<<JSON
{
  "path": "/foo",
  "description": "Foobar",
  "operations": [{
    "method": "PUT",
    "nickname": "updatePet",
    "summary": "Update an existing pet",
    "parameters": [],
    "responseMessages": []
  }]
}
JSON;
        $this->assertRecordEqualsContent($api, $content);
        $this->assertEquals('/foo', $api->getPath());
        $this->assertEquals('Foobar', $api->getDescription());
        $this->assertEquals([$operation], $api->getOperations());
    }
Beispiel #3
0
 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;
         }
     }
 }
Beispiel #4
0
    public function testSerialize()
    {
        $operation = new Operation('PUT', 'updatePet', 'Update an existing pet');
        $api = new Api('/foo', 'Foobar');
        $api->addOperation($operation);
        $model = new Model('Order', 'Order object');
        $model->addProperty(new Property('id', 'integer'));
        $model->addProperty(new PropertyReference('customer', 'Customer'));
        $model->addProperty(new Property('petId', 'integer'));
        $model->addProperty(new Property('quantity', 'integer'));
        $model->addProperty(new Property('status', 'string'));
        $model->addProperty(new Property('shipDate', 'string'));
        $declaration = new Declaration('1.0.0', 'http://petstore.swagger.wordnik.com/api', '/store');
        $declaration->addApi($api);
        $declaration->addModel($model);
        $content = <<<'JSON'
{
  "apiVersion": "1.0.0",
  "swaggerVersion": "1.2",
  "basePath": "http://petstore.swagger.wordnik.com/api",
  "resourcePath": "/store",
  "apis": [{
    "path": "/foo",
    "description": "Foobar",
    "operations": [{
      "method": "PUT",
      "nickname": "updatePet",
      "summary": "Update an existing pet",
      "parameters": [],
      "responseMessages": []
    }]
  }],
  "models": {
    "Order": {
      "id": "Order",
      "description": "Order object",
      "properties": {
        "id": {
          "id": "id",
          "type": "integer"
        },
        "customer": {
          "id": "customer",
          "$ref": "Customer"
        },
        "petId": {
          "id": "petId",
          "type": "integer"
        },
        "quantity": {
          "id": "quantity",
          "type": "integer"
        },
        "status": {
          "id": "status",
          "type": "string"
        },
        "shipDate": {
          "id": "shipDate",
          "type": "string"
        }
      }
    }
  }
}
JSON;
        $this->assertRecordEqualsContent($declaration, $content);
    }