NelmioApiDocBundle/Parser/JmsMetadataParser.php

185 lines
5.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;
use Nelmio\ApiDocBundle\Util\DocCommentExtractor;
2012-12-03 16:32:15 +01:00
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
/**
* Uses the JMS metadata factory to extract input/output model information
*/
class JmsMetadataParser implements ParserInterface
{
2012-08-07 21:57:36 -04:00
/**
* @var \Metadata\MetadataFactoryInterface
*/
private $factory;
/**
* @var \Nelmio\ApiDocBundle\Util\DocCommentExtractor
*/
private $commentExtractor;
private $parsedClasses = array();
/**
* Constructor, requires JMS Metadata factory
*/
public function __construct(MetadataFactoryInterface $factory, DocCommentExtractor $commentExtractor)
{
$this->factory = $factory;
$this->commentExtractor = $commentExtractor;
}
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
2012-11-26 12:37:44 -06:00
$dataType = $this->processDataType(is_string($item->type) ? $item->type : $item->type['name']);
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),
'readonly' => $item->readOnly
);
2012-08-24 11:12:36 -04:00
// if class already parsed, continue, to avoid infinite recursion
if (in_array($dataType['class'], $this->parsedClasses)) {
continue;
}
2012-08-27 12:25:18 -04:00
//check for nested classes with JMS metadata
if ($dataType['class'] && null !== $this->factory->getMetadataForClass($dataType['class'])) {
$this->parsedClasses[] = $dataType['class'];
$params[$name]['children'] = $this->parse($dataType['class']);
}
}
}
$this->parsedClasses = array();
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, PropertyMetadata $item)
{
$ref = new \ReflectionClass($className);
if ($item instanceof VirtualPropertyMetadata) {
$extracted = $this->commentExtractor->getDocCommentText($ref->getMethod($item->getter));
2012-10-24 15:36:21 +02:00
} else {
$extracted = $this->commentExtractor->getDocCommentText($ref->getProperty($item->name));
}
2012-09-10 09:46:52 -04:00
return !empty($extracted) ? $extracted : "No description.";
}
}