2014-06-17 17:05:00 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle.
|
|
|
|
*
|
|
|
|
* (c) Nelmio <hello@nelm.io>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\Formatter;
|
|
|
|
|
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
|
|
|
use Nelmio\ApiDocBundle\DataTypes;
|
2014-08-07 12:06:04 -07:00
|
|
|
use Nelmio\ApiDocBundle\Swagger\ModelRegistry;
|
2014-06-17 17:05:00 -07:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces Swagger-compliant resource lists and API declarations as defined here:
|
|
|
|
* https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md
|
|
|
|
*
|
|
|
|
* This formatter produces an array. Therefore output still needs to be `json_encode`d before passing on as HTTP response.
|
|
|
|
*
|
|
|
|
* @author Bezalel Hermoso <bezalelhermoso@gmail.com>
|
|
|
|
*/
|
|
|
|
class SwaggerFormatter implements FormatterInterface
|
|
|
|
{
|
|
|
|
protected $basePath;
|
|
|
|
|
|
|
|
protected $apiVersion;
|
|
|
|
|
|
|
|
protected $swaggerVersion;
|
|
|
|
|
|
|
|
protected $info = array();
|
|
|
|
|
|
|
|
protected $typeMap = array(
|
|
|
|
DataTypes::INTEGER => 'integer',
|
|
|
|
DataTypes::FLOAT => 'number',
|
|
|
|
DataTypes::STRING => 'string',
|
|
|
|
DataTypes::BOOLEAN => 'boolean',
|
|
|
|
DataTypes::FILE => 'string',
|
|
|
|
DataTypes::DATE => 'string',
|
|
|
|
DataTypes::DATETIME => 'string',
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $formatMap = array(
|
|
|
|
DataTypes::INTEGER => 'int32',
|
|
|
|
DataTypes::FLOAT => 'float',
|
|
|
|
DataTypes::FILE => 'byte',
|
|
|
|
DataTypes::DATE => 'date',
|
|
|
|
DataTypes::DATETIME => 'date-time',
|
|
|
|
);
|
|
|
|
|
2014-08-07 12:06:04 -07:00
|
|
|
/**
|
|
|
|
* @var \Nelmio\ApiDocBundle\Swagger\ModelRegistry
|
|
|
|
*/
|
|
|
|
protected $modelRegistry;
|
|
|
|
|
|
|
|
public function __construct($namingStategy)
|
|
|
|
{
|
|
|
|
$this->modelRegistry = new ModelRegistry($namingStategy);
|
|
|
|
}
|
|
|
|
|
2014-08-04 10:58:41 -07:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $authConfig = null;
|
|
|
|
|
|
|
|
public function setAuthenticationConfig(array $config)
|
|
|
|
{
|
|
|
|
$this->authConfig = $config;
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
/**
|
|
|
|
* Format a collection of documentation data.
|
|
|
|
*
|
|
|
|
* If resource is provided, an API declaration for that resource is produced. Otherwise, a resource listing is returned.
|
|
|
|
*
|
2015-03-06 11:19:08 +01:00
|
|
|
* @param array|ApiDoc[] $collection
|
|
|
|
* @param null|string $resource
|
2014-06-17 17:05:00 -07:00
|
|
|
* @return string|array
|
|
|
|
*/
|
|
|
|
public function format(array $collection, $resource = null)
|
|
|
|
{
|
|
|
|
if ($resource === null) {
|
|
|
|
return $this->produceResourceListing($collection);
|
|
|
|
} else {
|
|
|
|
return $this->produceApiDeclaration($collection, $resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats the collection into Swagger-compliant output.
|
|
|
|
*
|
2015-03-06 11:19:08 +01:00
|
|
|
* @param array $collection
|
2014-06-17 17:05:00 -07:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function produceResourceListing(array $collection)
|
|
|
|
{
|
|
|
|
$resourceList = array(
|
|
|
|
'swaggerVersion' => (string) $this->swaggerVersion,
|
|
|
|
'apis' => array(),
|
|
|
|
'apiVersion' => (string) $this->apiVersion,
|
|
|
|
'info' => $this->getInfo(),
|
|
|
|
'authorizations' => $this->getAuthorizations(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$apis = &$resourceList['apis'];
|
|
|
|
|
|
|
|
foreach ($collection as $item) {
|
|
|
|
|
|
|
|
/** @var $apiDoc ApiDoc */
|
|
|
|
$apiDoc = $item['annotation'];
|
|
|
|
$resource = $item['resource'];
|
|
|
|
|
|
|
|
if (!$apiDoc->isResource()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$subPath = $this->stripBasePath($resource);
|
|
|
|
$normalizedName = $this->normalizeResourcePath($subPath);
|
|
|
|
|
|
|
|
$apis[] = array(
|
|
|
|
'path' => '/' . $normalizedName,
|
|
|
|
'description' => $apiDoc->getResourceDescription(),
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resourceList;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getAuthorizations()
|
|
|
|
{
|
2014-08-04 10:58:41 -07:00
|
|
|
$auth = array();
|
|
|
|
|
|
|
|
if ($this->authConfig === null) {
|
|
|
|
return $auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = $this->authConfig;
|
|
|
|
|
|
|
|
if ($config['delivery'] === 'http') {
|
|
|
|
return $auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
$auth['apiKey'] = array(
|
|
|
|
'type' => 'apiKey',
|
|
|
|
'passAs' => $config['delivery'],
|
|
|
|
'keyname' => $config['name'],
|
|
|
|
);
|
|
|
|
|
|
|
|
return $auth;
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getInfo()
|
|
|
|
{
|
|
|
|
return $this->info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format documentation data for one route.
|
|
|
|
*
|
2015-03-06 11:19:08 +01:00
|
|
|
* @param ApiDoc $annotation
|
|
|
|
* return string|array
|
2014-06-17 17:05:00 -07:00
|
|
|
* @throws \BadMethodCallException
|
2014-06-17 17:05:00 -07:00
|
|
|
*/
|
|
|
|
public function formatOne(ApiDoc $annotation)
|
|
|
|
{
|
2014-06-17 17:05:00 -07:00
|
|
|
throw new \BadMethodCallException(sprintf('%s does not support formatting a single ApiDoc only.', __CLASS__));
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats collection to produce a Swagger-compliant API declaration for the given resource.
|
|
|
|
*
|
2015-03-06 11:19:08 +01:00
|
|
|
* @param array $collection
|
|
|
|
* @param string $resource
|
2014-06-17 17:05:00 -07:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-06-28 00:20:12 +00:00
|
|
|
protected function produceApiDeclaration(array $collection, $resource)
|
2014-06-17 17:05:00 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
$apiDeclaration = array(
|
|
|
|
'swaggerVersion' => (string) $this->swaggerVersion,
|
|
|
|
'apiVersion' => (string) $this->apiVersion,
|
|
|
|
'basePath' => $this->basePath,
|
|
|
|
'resourcePath' => $resource,
|
|
|
|
'apis' => array(),
|
|
|
|
'models' => array(),
|
|
|
|
'produces' => array(),
|
|
|
|
'consumes' => array(),
|
2014-08-04 10:58:41 -07:00
|
|
|
'authorizations' => $this->getAuthorizations(),
|
2014-06-17 17:05:00 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
$main = null;
|
|
|
|
|
|
|
|
$apiBag = array();
|
|
|
|
|
|
|
|
foreach ($collection as $item) {
|
|
|
|
|
|
|
|
/** @var $apiDoc ApiDoc */
|
|
|
|
$apiDoc = $item['annotation'];
|
|
|
|
$itemResource = $this->stripBasePath($item['resource']);
|
2014-08-07 15:32:39 -07:00
|
|
|
$input = $apiDoc->getInput();
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2014-08-07 15:32:39 -07:00
|
|
|
if (!is_array($input)) {
|
|
|
|
$input = array(
|
|
|
|
'class' => $input,
|
|
|
|
'paramType' => 'form',
|
|
|
|
);
|
|
|
|
} elseif (empty($input['paramType'])) {
|
|
|
|
$input['paramType'] = 'form';
|
|
|
|
}
|
2014-09-22 15:26:38 +02:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$route = $apiDoc->getRoute();
|
|
|
|
|
|
|
|
$itemResource = $this->normalizeResourcePath($itemResource);
|
|
|
|
|
|
|
|
if ('/' . $itemResource !== $resource) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$compiled = $route->compile();
|
|
|
|
|
|
|
|
$path = $this->stripBasePath($route->getPath());
|
|
|
|
|
|
|
|
if (!isset($apiBag[$path])) {
|
|
|
|
$apiBag[$path] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters = array();
|
|
|
|
$responseMessages = array();
|
|
|
|
|
|
|
|
foreach ($compiled->getPathVariables() as $paramValue) {
|
|
|
|
$parameter = array(
|
|
|
|
'paramType' => 'path',
|
|
|
|
'name' => $paramValue,
|
|
|
|
'type' => 'string',
|
|
|
|
'required' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($paramValue === '_format' && false != ($req = $route->getRequirement('_format'))) {
|
|
|
|
$parameter['enum'] = explode('|', $req);
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters[] = $parameter;
|
|
|
|
}
|
|
|
|
|
2014-09-22 15:26:38 +02:00
|
|
|
$data = $apiDoc->toArray();
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
if (isset($data['filters'])) {
|
|
|
|
$parameters = array_merge($parameters, $this->deriveQueryParameters($data['filters']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($data['parameters'])) {
|
2014-08-07 12:06:04 -07:00
|
|
|
$parameters = array_merge($parameters, $this->deriveParameters($data['parameters'], $input['paramType']));
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$responseMap = $apiDoc->getParsedResponseMap();
|
|
|
|
|
|
|
|
$statusMessages = isset($data['statusCodes']) ? $data['statusCodes'] : array();
|
|
|
|
|
|
|
|
foreach ($responseMap as $statusCode => $prop) {
|
|
|
|
|
|
|
|
if (isset($statusMessages[$statusCode])) {
|
|
|
|
$message = is_array($statusMessages[$statusCode]) ? implode('; ', $statusMessages[$statusCode]) : $statusCode[$statusCode];
|
|
|
|
} else {
|
|
|
|
$message = sprintf('See standard HTTP status code reason for %s', $statusCode);
|
|
|
|
}
|
|
|
|
|
2014-07-28 13:14:59 -07:00
|
|
|
$className = !empty($prop['type']['form_errors']) ? $prop['type']['class'] . '.ErrorResponse' : $prop['type']['class'];
|
|
|
|
|
2014-08-12 18:41:23 -07:00
|
|
|
if (isset($prop['type']['collection']) && $prop['type']['collection'] === true) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Without alias: Fully\Qualified\Class\Name[]
|
|
|
|
* With alias: Fully\Qualified\Class\Name[alias]
|
|
|
|
*/
|
|
|
|
$alias = $prop['type']['collectionName'];
|
|
|
|
|
2014-07-28 13:14:59 -07:00
|
|
|
$newName = sprintf('%s[%s]', $className, $alias);
|
2014-08-12 18:41:23 -07:00
|
|
|
$collId =
|
|
|
|
$this->registerModel(
|
|
|
|
$newName,
|
|
|
|
array(
|
|
|
|
$alias => array(
|
|
|
|
'dataType' => null,
|
2014-07-28 13:14:59 -07:00
|
|
|
'subType' => $className,
|
2014-08-12 18:41:23 -07:00
|
|
|
'actualType' => DataTypes::COLLECTION,
|
|
|
|
'required' => true,
|
|
|
|
'readonly' => true,
|
|
|
|
'description' => null,
|
|
|
|
'default' => null,
|
|
|
|
'children' => $prop['model'][$alias]['children'],
|
|
|
|
)
|
|
|
|
),
|
|
|
|
''
|
|
|
|
);
|
|
|
|
$responseModel = array(
|
|
|
|
'code' => $statusCode,
|
|
|
|
'message' => $message,
|
|
|
|
'responseModel' => $collId
|
|
|
|
);
|
|
|
|
} else {
|
2014-07-28 13:14:59 -07:00
|
|
|
|
2014-08-12 18:41:23 -07:00
|
|
|
$responseModel = array(
|
|
|
|
'code' => $statusCode,
|
|
|
|
'message' => $message,
|
2014-07-28 13:14:59 -07:00
|
|
|
'responseModel' => $this->registerModel($className, $prop['model'], ''),
|
2014-08-12 18:41:23 -07:00
|
|
|
);
|
|
|
|
}
|
2014-06-17 17:05:00 -07:00
|
|
|
$responseMessages[$statusCode] = $responseModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
$unmappedMessages = array_diff(array_keys($statusMessages), array_keys($responseMessages));
|
|
|
|
|
|
|
|
foreach ($unmappedMessages as $code) {
|
|
|
|
$responseMessages[$code] = array(
|
|
|
|
'code' => $code,
|
|
|
|
'message' => is_array($statusMessages[$code]) ? implode('; ', $statusMessages[$code]) : $statusMessages[$code],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-07-24 12:18:33 -07:00
|
|
|
$type = isset($responseMessages[200]['responseModel']) ? $responseMessages[200]['responseModel'] : null;
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
foreach ($apiDoc->getRoute()->getMethods() as $method) {
|
|
|
|
$operation = array(
|
|
|
|
'method' => $method,
|
|
|
|
'summary' => $apiDoc->getDescription(),
|
|
|
|
'nickname' => $this->generateNickname($method, $itemResource),
|
|
|
|
'parameters' => $parameters,
|
|
|
|
'responseMessages' => array_values($responseMessages),
|
|
|
|
);
|
|
|
|
|
2014-07-24 12:18:33 -07:00
|
|
|
if ($type !== null) {
|
|
|
|
$operation['type'] = $type;
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$apiBag[$path][] = $operation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$apiDeclaration['resourcePath'] = $resource;
|
|
|
|
|
|
|
|
foreach ($apiBag as $path => $operations) {
|
|
|
|
$apiDeclaration['apis'][] = array(
|
|
|
|
'path' => $path,
|
|
|
|
'operations' => $operations,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-08-07 12:06:04 -07:00
|
|
|
$apiDeclaration['models'] = $this->modelRegistry->getModels();
|
2014-08-07 12:23:55 -07:00
|
|
|
$this->modelRegistry->clear();
|
2014-06-17 17:05:00 -07:00
|
|
|
|
|
|
|
return $apiDeclaration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Slugify a URL path. Trims out path parameters wrapped in curly brackets.
|
|
|
|
*
|
|
|
|
* @param $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function normalizeResourcePath($path)
|
|
|
|
{
|
|
|
|
$path = preg_replace('/({.*?})/', '', $path);
|
|
|
|
$path = trim(preg_replace('/[^0-9a-zA-Z]/', '-', $path), '-');
|
|
|
|
$path = preg_replace('/-+/', '-', $path);
|
2015-03-06 11:19:08 +01:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $path
|
|
|
|
*/
|
|
|
|
public function setBasePath($path)
|
|
|
|
{
|
|
|
|
$this->basePath = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats query parameters to Swagger-compliant form.
|
|
|
|
*
|
2015-03-06 11:19:08 +01:00
|
|
|
* @param array $input
|
2014-06-17 17:05:00 -07:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function deriveQueryParameters(array $input)
|
|
|
|
{
|
|
|
|
$parameters = array();
|
|
|
|
|
|
|
|
foreach ($input as $name => $prop) {
|
2014-09-22 15:26:38 +02:00
|
|
|
if (!isset($prop['dataType'])) {
|
|
|
|
$prop['dataType'] = 'string';
|
|
|
|
}
|
2014-06-17 17:05:00 -07:00
|
|
|
$parameters[] = array(
|
|
|
|
'paramType' => 'query',
|
|
|
|
'name' => $name,
|
|
|
|
'type' => isset($this->typeMap[$prop['dataType']]) ? $this->typeMap[$prop['dataType']] : 'string',
|
2014-09-22 15:26:38 +02:00
|
|
|
'description' => isset($prop['description']) ? $prop['description'] : null,
|
2014-06-17 17:05:00 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parameters;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a Swagger-compliant parameter list from the provided parameter array. Models are built when necessary.
|
|
|
|
*
|
|
|
|
* @param array $input
|
|
|
|
* @param array $models
|
2014-08-07 15:32:39 -07:00
|
|
|
*
|
|
|
|
* @param string $paramType
|
|
|
|
*
|
2014-06-17 17:05:00 -07:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-08-07 12:06:04 -07:00
|
|
|
protected function deriveParameters(array $input, $paramType = 'form')
|
2014-06-17 17:05:00 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
$parameters = array();
|
|
|
|
|
|
|
|
foreach ($input as $name => $prop) {
|
|
|
|
|
|
|
|
$type = null;
|
|
|
|
$format = null;
|
|
|
|
$ref = null;
|
|
|
|
$enum = null;
|
|
|
|
$items = null;
|
|
|
|
|
2014-09-22 15:26:38 +02:00
|
|
|
if (!isset($prop['actualType'])) {
|
|
|
|
$prop['actualType'] = 'string';
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
if (isset ($this->typeMap[$prop['actualType']])) {
|
|
|
|
$type = $this->typeMap[$prop['actualType']];
|
|
|
|
} else {
|
|
|
|
switch ($prop['actualType']) {
|
|
|
|
case DataTypes::ENUM:
|
|
|
|
$type = 'string';
|
2014-06-25 14:23:19 -07:00
|
|
|
if (isset($prop['format'])) {
|
|
|
|
$enum = array_keys(json_decode($prop['format'], true));
|
|
|
|
}
|
2014-06-17 17:05:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DataTypes::MODEL:
|
|
|
|
$ref =
|
|
|
|
$this->registerModel(
|
|
|
|
$prop['subType'],
|
|
|
|
isset($prop['children']) ? $prop['children'] : null,
|
2014-08-07 12:06:04 -07:00
|
|
|
$prop['description'] ?: $prop['dataType']
|
2014-07-25 17:31:24 -07:00
|
|
|
);
|
2014-06-17 17:05:00 -07:00
|
|
|
break;
|
2014-08-15 09:47:35 -07:00
|
|
|
|
|
|
|
case DataTypes::COLLECTION:
|
|
|
|
$type = 'array';
|
2014-09-04 11:19:02 -07:00
|
|
|
if ($prop['subType'] === null) {
|
|
|
|
$items = array('type' => 'string');
|
2014-08-15 09:47:35 -07:00
|
|
|
} elseif (isset($this->typeMap[$prop['subType']])) {
|
2014-08-19 12:20:42 -07:00
|
|
|
$items = array('type' => $this->typeMap[$prop['subType']]);
|
2014-08-15 09:47:35 -07:00
|
|
|
} else {
|
2014-09-04 11:19:02 -07:00
|
|
|
$ref =
|
|
|
|
$this->registerModel(
|
|
|
|
$prop['subType'],
|
|
|
|
isset($prop['children']) ? $prop['children'] : null,
|
|
|
|
$prop['description'] ?: $prop['dataType']
|
|
|
|
);
|
|
|
|
$items = array(
|
|
|
|
'$ref' => $ref,
|
|
|
|
);
|
2014-08-15 09:47:35 -07:00
|
|
|
}
|
|
|
|
break;
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->formatMap[$prop['actualType']])) {
|
|
|
|
$format = $this->formatMap[$prop['actualType']];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $type && null === $ref) {
|
|
|
|
/* `type` or `$ref` is required. Continue to next of none of these was determined. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameter = array(
|
2014-08-07 15:32:39 -07:00
|
|
|
'paramType' => $paramType,
|
2014-06-17 17:05:00 -07:00
|
|
|
'name' => $name,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (null !== $type) {
|
|
|
|
$parameter['type'] = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $ref) {
|
|
|
|
$parameter['$ref'] = $ref;
|
|
|
|
$parameter['type'] = $ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $format) {
|
|
|
|
$parameter['format'] = $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($enum) && count($enum) > 0) {
|
|
|
|
$parameter['enum'] = $enum;
|
|
|
|
}
|
|
|
|
|
2014-09-22 15:26:38 +02:00
|
|
|
if (isset($prop['default'])) {
|
2014-07-25 17:31:24 -07:00
|
|
|
$parameter['defaultValue'] = $prop['default'];
|
|
|
|
}
|
|
|
|
|
2014-08-19 12:20:42 -07:00
|
|
|
if (isset($items)) {
|
|
|
|
$parameter['items'] = $items;
|
|
|
|
}
|
|
|
|
|
2014-09-22 15:26:38 +02:00
|
|
|
if (isset($prop['description'])) {
|
|
|
|
$parameter['description'] = $prop['description'];
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$parameters[] = $parameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a model into the model array. Returns a unique identifier for the model to be used in `$ref` properties.
|
|
|
|
*
|
2014-08-07 12:06:04 -07:00
|
|
|
* @param $className
|
|
|
|
* @param array $parameters
|
2014-06-17 17:05:00 -07:00
|
|
|
* @param string $description
|
2014-08-07 12:06:04 -07:00
|
|
|
*
|
|
|
|
* @internal param $models
|
2014-06-17 17:05:00 -07:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-08-07 12:06:04 -07:00
|
|
|
public function registerModel($className, array $parameters = null, $description = '')
|
2014-06-17 17:05:00 -07:00
|
|
|
{
|
2014-08-07 12:06:04 -07:00
|
|
|
return $this->modelRegistry->register($className, $parameters, $description);
|
2014-06-17 17:05:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $swaggerVersion
|
|
|
|
*/
|
|
|
|
public function setSwaggerVersion($swaggerVersion)
|
|
|
|
{
|
|
|
|
$this->swaggerVersion = $swaggerVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $apiVersion
|
|
|
|
*/
|
|
|
|
public function setApiVersion($apiVersion)
|
|
|
|
{
|
|
|
|
$this->apiVersion = $apiVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $info
|
|
|
|
*/
|
|
|
|
public function setInfo($info)
|
|
|
|
{
|
|
|
|
$this->info = $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strips the base path from a URL path.
|
|
|
|
*
|
2015-04-30 15:34:19 +02:00
|
|
|
* @param $basePath
|
2014-06-17 17:05:00 -07:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-04-30 15:34:19 +02:00
|
|
|
protected function stripBasePath($basePath)
|
2014-06-17 17:05:00 -07:00
|
|
|
{
|
2014-09-22 15:26:38 +02:00
|
|
|
if ('/' === $this->basePath) {
|
2015-04-30 15:34:19 +02:00
|
|
|
return $basePath;
|
2014-09-22 15:26:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 15:34:19 +02:00
|
|
|
$path = sprintf('#^%s#', preg_quote($this->basePath));
|
|
|
|
$subPath = preg_replace($path, '', $basePath);
|
2015-03-06 11:19:08 +01:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
return $subPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate nicknames based on support HTTP methods and the resource name.
|
|
|
|
*
|
|
|
|
* @param $method
|
|
|
|
* @param $resource
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function generateNickname($method, $resource)
|
|
|
|
{
|
|
|
|
$resource = preg_replace('#/^#', '', $resource);
|
|
|
|
$resource = $this->normalizeResourcePath($resource);
|
2015-03-06 11:19:08 +01:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
return sprintf('%s_%s', strtolower($method), $resource);
|
|
|
|
}
|
2015-03-06 11:19:08 +01:00
|
|
|
}
|