NelmioApiDocBundle/ModelDescriber/Annotations/PropertyPhpDocReader.php

64 lines
1.8 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\ModelDescriber\Annotations;
2017-12-22 17:42:18 +00:00
use EXSyst\Component\Swagger\Schema;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use phpDocumentor\Reflection\DocBlockFactory;
/**
2017-12-19 08:41:24 +01:00
* Extract information about properties of a model from the DocBlock comment.
*
* @internal
*/
class PropertyPhpDocReader
{
private $docBlockFactory;
public function __construct()
{
$this->docBlockFactory = DocBlockFactory::createInstance();
}
/**
2017-12-19 08:41:24 +01:00
* Update the Swagger information with information from the DocBlock comment.
*/
public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property)
{
try {
$docBlock = $this->docBlockFactory->create($reflectionProperty);
} catch (\Exception $e) {
// ignore
2017-12-19 08:41:24 +01:00
return;
}
if (!$title = $docBlock->getSummary()) {
/** @var Var_ $var */
foreach ($docBlock->getTagsByName('var') as $var) {
2017-12-19 23:14:57 +01:00
if (!$description = $var->getDescription()) {
continue;
}
2017-12-19 08:41:24 +01:00
$title = $description->render();
2017-12-22 17:42:18 +00:00
if ($title) {
break;
}
2017-12-19 08:41:24 +01:00
}
}
2017-12-22 17:42:18 +00:00
if (null === $property->getTitle() && $title) {
2017-12-19 08:41:24 +01:00
$property->setTitle($title);
}
2017-12-22 17:42:18 +00:00
if (null === $property->getDescription() && $docBlock->getDescription() && $docBlock->getDescription()->render()) {
2017-12-19 08:41:24 +01:00
$property->setDescription($docBlock->getDescription()->render());
}
}
}