NelmioApiDocBundle/Formatter/AbstractFormatter.php

144 lines
4.1 KiB
PHP
Raw Normal View History

2012-04-11 20:00:21 +02:00
<?php
2012-04-13 11:03:05 +02:00
/*
* 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.
*/
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;
}
/**
* {@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) {
$resource = $coll['resource'];
if (!isset($array[$resource])) {
$array[$resource] = array();
}
$array[$resource][] = $this->getData($coll['annotation'], $coll['route'], $coll['requirements']);
}
return $this->render($array);
}
/**
* Format a single array of data
*
2012-05-23 00:33:01 +02:00
* @param array $data
* @return string|array
*/
2012-05-23 00:33:01 +02:00
abstract protected function renderOne(array $data);
/**
* Format a set of resource sections.
*
2012-05-23 00:33:01 +02:00
* @param array $collection
* @return string|array
*/
2012-05-23 00:33:01 +02:00
abstract protected function render(array $collection);
2012-04-12 01:28:36 +02:00
/**
2012-05-23 00:33:01 +02:00
* @param ApiDoc $apiDoc
* @param Route $route
* @param array $requirements
* @return array
*/
protected function getData(ApiDoc $apiDoc, Route $route, array $requirements = array())
2012-04-11 20:00:21 +02:00
{
$method = $route->getRequirement('_method');
$data = array(
'method' => $method ?: 'ANY',
'uri' => $route->compile()->getPattern(),
2012-04-11 20:00:21 +02:00
);
foreach ($route->compile()->getRequirements() as $name => $value) {
if ('_method' !== $name) {
$requirements[$name] = array(
'requirement' => $value,
'type' => '',
'description' => '',
);
}
}
if (null !== $paramDocs = $route->getOption('_paramDocs')) {
$regexp = '{(\w*) *\$%s *(.*)}i';
foreach ($route->compile()->getVariables() as $var) {
$found = false;
foreach ($paramDocs as $paramDoc) {
if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
$requirements[$var]['type'] = isset($matches[1]) ? $matches[1] : '';
$requirements[$var]['description'] = $matches[2];
if (!isset($requirements[$var]['requirement'])) {
$requirements[$var]['requirement'] = '';
}
$found = true;
break;
}
}
if (!isset($requirements[$var]) && false === $found) {
$requirements[$var] = array('requirement' => '', 'type' => '', 'description' => '');
}
}
}
$data['requirements'] = $requirements;
2012-04-11 20:00:21 +02:00
if (null !== $formType = $apiDoc->getFormType()) {
$data['parameters'] = $this->parser->parse($formType);
2012-04-11 20:00:21 +02:00
if ('PUT' === $method) {
// All parameters are optional with PUT (update)
array_walk($data['parameters'], function($val, $key) use (&$data) {
$data['parameters'][$key]['required'] = false;
2012-04-11 20:00:21 +02:00
});
}
}
if ($filters = $apiDoc->getFilters()) {
$data['filters'] = $filters;
}
if ($description = $apiDoc->getDescription()) {
$data['description'] = $description;
2012-04-11 20:00:21 +02:00
}
return $data;
}
}