add virtual property support for jms parser

This commit is contained in:
Klein Florian 2012-10-24 15:09:36 +02:00
parent 28f059fff0
commit d129db9a53

View File

@ -13,6 +13,8 @@ namespace Nelmio\ApiDocBundle\Parser;
use Metadata\MetadataFactoryInterface; use Metadata\MetadataFactoryInterface;
use Nelmio\ApiDocBundle\Util\DocCommentExtractor; use Nelmio\ApiDocBundle\Util\DocCommentExtractor;
use JMS\SerializerBundle\Metadata\PropertyMetadata;
use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
/** /**
* Uses the JMS metadata factory to extract input/output model information * Uses the JMS metadata factory to extract input/output model information
@ -78,7 +80,7 @@ class JmsMetadataParser implements ParserInterface
$params[$name] = array( $params[$name] = array(
'dataType' => $dataType['normalized'], '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 '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), 'description' => $this->getDescription($input, $item),
'readonly' => $item->readOnly 'readonly' => $item->readOnly
); );
@ -158,10 +160,15 @@ class JmsMetadataParser implements ParserInterface
return null; return null;
} }
protected function getDescription($className, $propertyName) protected function getDescription($className, PropertyMetadata $item)
{ {
$ref = new \ReflectionClass($className); $ref = new \ReflectionClass($className);
$extracted = $this->commentExtractor->getDocCommentText($ref->getProperty($propertyName)); if ($item instanceof VirtualPropertyMetadata) {
$extracted = $this->commentExtractor->getDocCommentText($ref->getMethod($item->getter));
}
else {
$extracted = $this->commentExtractor->getDocCommentText($ref->getProperty($item->name));
}
return !empty($extracted) ? $extracted : "No description."; return !empty($extracted) ? $extracted : "No description.";
} }