NelmioApiDocBundle/Parser/JmsMetadataParser.php

159 lines
4.3 KiB
PHP
Raw Normal View History

<?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\Parser;
use Metadata\MetadataFactoryInterface;
/**
* Uses the JMS metadata factory to extract input/output model information
*/
class JmsMetadataParser implements ParserInterface
{
2012-08-07 21:57:36 -04:00
/**
* Constructor, requires JMS Metadata factory
*/
public function __construct(MetadataFactoryInterface $factory)
{
$this->factory = $factory;
}
2012-08-07 21:57:36 -04:00
/**
* {@inheritdoc}
*/
public function supports($input)
{
try {
if ($meta = $this->factory->getMetadataForClass($input)) {
return true;
2012-08-27 13:25:03 -04:00
}
} catch (\ReflectionException $e) {
}
2012-08-27 13:25:03 -04:00
return false;
}
2012-08-07 21:57:36 -04:00
/**
* {@inheritdoc}
*/
public function parse($input)
{
$meta = $this->factory->getMetadataForClass($input);
2012-08-08 10:21:56 -04:00
if (null === $meta) {
throw new \InvalidArgumentException(sprintf("No metadata found for class %s", $input));
}
2012-08-07 21:57:36 -04:00
$params = array();
2012-08-07 21:57:36 -04:00
//iterate over property metadata
foreach ($meta->propertyMetadata as $item) {
2012-08-07 21:57:36 -04:00
if (!is_null($item->type)) {
$name = isset($item->serializedName) ? $item->serializedName : $item->name;
2012-08-07 21:57:36 -04:00
$dataType = $this->processDataType($item->type);
2012-08-07 21:57:36 -04:00
$params[$name] = array(
'dataType' => $dataType['normalized'],
'required' => false, //TODO: can't think of a good way to specify this one, JMS doesn't have a setting for this
'description' => $this->getDescription($input, $item->name),
'readonly' => $item->readOnly
);
2012-08-24 11:12:36 -04:00
2012-08-27 12:25:18 -04:00
//check for nested classes with JMS metadata
if ($dataType['class'] && null !== $this->factory->getMetadataForClass($dataType['class'])) {
$params[$name]['children'] = $this->parse($dataType['class']);
}
}
}
2012-08-07 21:57:36 -04:00
return $params;
}
2012-08-07 21:57:36 -04:00
/**
* Figure out a normalized data type (for documentation), and get a
* nested class name, if available.
*
2012-08-24 11:12:36 -04:00
* @param string $type
* @return array
*/
protected function processDataType($type)
{
//could be basic type
if ($this->isPrimitive($type)) {
return array(
'normalized' => $type,
'class' => null
);
}
2012-08-24 11:12:36 -04:00
//check for a type inside something that could be treated as an array
if ($nestedType = $this->getNestedTypeInArray($type)) {
if ($this->isPrimitive($nestedType)) {
return array(
'normalized' => sprintf("array of %ss", $nestedType),
'class' => null
);
}
$exp = explode("\\", $nestedType);
return array(
'normalized' => sprintf("array of objects (%s)", end($exp)),
'class' => $nestedType
);
}
2012-08-24 11:12:36 -04:00
//if we got this far, it's a general class name
$exp = explode("\\", $type);
2012-08-24 11:12:36 -04:00
return array(
'normalized' => sprintf("object (%s)", end($exp)),
'class' => $type
);
}
protected function isPrimitive($type)
{
return in_array($type, array('boolean', 'integer', 'string', 'double', 'array', 'DateTime'));
}
2012-08-24 11:12:36 -04:00
/**
* Check the various ways JMS describes values in arrays, and
* get the value type in the array
*
* @param string $type
2012-08-24 13:32:52 -04:00
* @return string|null
*/
protected function getNestedTypeInArray($type)
{
//could be some type of array with <V>, or <K,V>
$regEx = "/\<([A-Za-z0-9\\\]*)(\,?\s?(.*))?\>/";
if (preg_match($regEx, $type, $matches)) {
return (!empty($matches[3])) ? $matches[3] : $matches[1];
}
2012-08-24 11:12:36 -04:00
2012-08-24 13:32:52 -04:00
return null;
}
2012-08-24 11:12:36 -04:00
protected function getDescription($className, $propertyName)
{
$description = "No description.";
//TODO: abstract docblock parsing utility and implement here
2012-08-28 14:50:40 -04:00
return $description;
}
}