NelmioApiDocBundle/Formatter/AbstractFormatter.php

79 lines
2.0 KiB
PHP
Raw Normal View History

2012-04-11 20:00:21 +02:00
<?php
namespace Nelmio\ApiBundle\Formatter;
use Nelmio\ApiBundle\Annotation\ApiDoc;
use Nelmio\ApiBundle\Parser\FormTypeParser;
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
{
/**
* @var \Nelmio\ApiBundle\Parser\FormTypeParser
*/
protected $parser;
public function __construct(FormTypeParser $parser)
{
$this->parser = $parser;
}
/**
* {@inheritdoc}
*/
public function formatOne(ApiDoc $apiDoc, Route $route)
2012-04-12 01:28:36 +02:00
{
return $this->renderOne($this->getData($apiDoc, $route));
2012-04-12 01:28:36 +02:00
}
/**
* {@inheritdoc}
*/
public function format(array $collection)
{
$array = array();
foreach ($collection as $coll) {
$array[] = $this->getData($coll['annotation'], $coll['route']);
}
return $this->render($array);
}
protected abstract function renderOne(array $data);
protected abstract function render(array $collection);
2012-04-12 01:28:36 +02:00
private function getData(ApiDoc $apiDoc, Route $route)
2012-04-11 20:00:21 +02:00
{
$method = $route->getRequirement('_method');
$data = array(
'method' => $method,
'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) {
$data['parameters'][$key]['is_required'] = false;
});
}
}
if ($filters = $apiDoc->getFilters()) {
$data['filters'] = $filters;
}
if ($comment = $apiDoc->getComment()) {
$data['comment'] = $comment;
}
return $data;
}
}