2012-04-11 20:00:21 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
2012-04-12 18:37:42 +02:00
|
|
|
|
namespace Nelmio\ApiDocBundle\Formatter;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
|
2012-04-12 18:37:42 +02:00
|
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
|
|
|
|
use Nelmio\ApiDocBundle\Parser\FormTypeParser;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
|
|
2012-04-12 01:28:36 +02:00
|
|
|
|
abstract class AbstractFormatter implements FormatterInterface
|
2012-04-11 20:00:21 +02:00
|
|
|
|
{
|
|
|
|
|
/**
|
2012-04-12 18:37:42 +02:00
|
|
|
|
* @var \Nelmio\ApiDocBundle\Parser\FormTypeParser
|
2012-04-11 20:00:21 +02:00
|
|
|
|
*/
|
|
|
|
|
protected $parser;
|
|
|
|
|
|
|
|
|
|
public function __construct(FormTypeParser $parser)
|
|
|
|
|
{
|
|
|
|
|
$this->parser = $parser;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 12:48:36 +02:00
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function formatOne(ApiDoc $apiDoc, Route $route)
|
2012-04-12 01:28:36 +02:00
|
|
|
|
{
|
2012-04-12 12:48:36 +02:00
|
|
|
|
return $this->renderOne($this->getData($apiDoc, $route));
|
2012-04-12 01:28:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 12:48:36 +02:00
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function format(array $collection)
|
|
|
|
|
{
|
|
|
|
|
$array = array();
|
|
|
|
|
foreach ($collection as $coll) {
|
2012-04-12 17:24:38 +02:00
|
|
|
|
$resource = $coll['resource'];
|
|
|
|
|
if (!isset($array[$resource])) {
|
|
|
|
|
$array[$resource] = array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array[$resource][] = $this->getData($coll['annotation'], $coll['route']);
|
2012-04-12 12:48:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->render($array);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 17:24:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* Format a single array of data
|
|
|
|
|
*
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string|array
|
|
|
|
|
*/
|
2012-04-12 12:48:36 +02:00
|
|
|
|
protected abstract function renderOne(array $data);
|
|
|
|
|
|
2012-04-12 17:24:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* Format a set of data for a given resource.
|
|
|
|
|
*
|
|
|
|
|
* @param string $resource A resource name.
|
|
|
|
|
* @param array $arrayOfData A set of data.
|
|
|
|
|
* @return string|array
|
|
|
|
|
*/
|
|
|
|
|
protected abstract function renderResourceSection($resource, array $arrayOfData);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format a set of resource sections.
|
|
|
|
|
*
|
|
|
|
|
* @param array $collection
|
|
|
|
|
* @return string|array
|
|
|
|
|
*/
|
2012-04-12 12:48:36 +02:00
|
|
|
|
protected abstract function render(array $collection);
|
2012-04-12 01:28:36 +02:00
|
|
|
|
|
2012-04-12 17:24:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param ApiDoc $apiDoc
|
|
|
|
|
* @param Route $route
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2012-04-12 19:17:03 +02:00
|
|
|
|
protected function getData(ApiDoc $apiDoc, Route $route)
|
2012-04-11 20:00:21 +02:00
|
|
|
|
{
|
|
|
|
|
$method = $route->getRequirement('_method');
|
|
|
|
|
$data = array(
|
2012-04-12 17:24:38 +02:00
|
|
|
|
'method' => $method ?: 'ANY',
|
2012-04-11 20:00:21 +02:00
|
|
|
|
'uri' => $route->compile()->getPattern(),
|
|
|
|
|
'requirements' => $route->compile()->getRequirements(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
unset($data['requirements']['_method']);
|
|
|
|
|
|
|
|
|
|
if (null !== $formType = $apiDoc->getFormType()) {
|
|
|
|
|
$data['parameters'] = $this->parser->parse(new $formType());
|
|
|
|
|
|
|
|
|
|
if ('PUT' === $method) {
|
|
|
|
|
// All parameters are optional with PUT (update)
|
|
|
|
|
array_walk($data['parameters'], function($val, $key) use (&$data) {
|
2012-04-12 17:24:38 +02:00
|
|
|
|
$data['parameters'][$key]['required'] = false;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($filters = $apiDoc->getFilters()) {
|
|
|
|
|
$data['filters'] = $filters;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 17:24:38 +02:00
|
|
|
|
if ($description = $apiDoc->getDescription()) {
|
|
|
|
|
$data['description'] = $description;
|
2012-04-11 20:00:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
}
|