Allow to specify a custom tag formatter.

This commit is contained in:
Jan Schneider
2017-06-04 21:54:23 +02:00
committed by Jaap van Otterdijk
parent d455663710
commit d52fdc6640
+8 -3
View File
@@ -32,6 +32,9 @@ class Serializer
/** @var int|null The max length of a line. */ /** @var int|null The max length of a line. */
protected $lineLength = null; protected $lineLength = null;
/** @var DocBlock\Tags\Formatter A custom tag formatter. */
protected $tagFormatter = null;
/** /**
* Create a Serializer instance. * Create a Serializer instance.
* *
@@ -39,18 +42,21 @@ class Serializer
* @param string $indentString The string to indent the comment with. * @param string $indentString The string to indent the comment with.
* @param bool $indentFirstLine Whether to indent the first line. * @param bool $indentFirstLine Whether to indent the first line.
* @param int|null $lineLength The max length of a line or NULL to disable line wrapping. * @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
* @param DocBlock\Tags\Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
*/ */
public function __construct($indent = 0, $indentString = ' ', $indentFirstLine = true, $lineLength = null) public function __construct($indent = 0, $indentString = ' ', $indentFirstLine = true, $lineLength = null, $tagFormatter = null)
{ {
Assert::integer($indent); Assert::integer($indent);
Assert::string($indentString); Assert::string($indentString);
Assert::boolean($indentFirstLine); Assert::boolean($indentFirstLine);
Assert::nullOrInteger($lineLength); Assert::nullOrInteger($lineLength);
Assert::nullOrIsInstanceOf($tagFormatter, 'phpDocumentor\Reflection\DocBlock\Tags\Formatter');
$this->indent = $indent; $this->indent = $indent;
$this->indentString = $indentString; $this->indentString = $indentString;
$this->isFirstLineIndented = $indentFirstLine; $this->isFirstLineIndented = $indentFirstLine;
$this->lineLength = $lineLength; $this->lineLength = $lineLength;
$this->tagFormatter = $tagFormatter ?: new DocBlock\Tags\Formatter\PassthroughFormatter();
} }
/** /**
@@ -128,8 +134,7 @@ class Serializer
private function addTagBlock(DocBlock $docblock, $wrapLength, $indent, $comment) private function addTagBlock(DocBlock $docblock, $wrapLength, $indent, $comment)
{ {
foreach ($docblock->getTags() as $tag) { foreach ($docblock->getTags() as $tag) {
$formatter = new DocBlock\Tags\Formatter\PassthroughFormatter(); $tagText = $this->tagFormatter->format($tag);
$tagText = $formatter->format($tag);
if ($wrapLength !== null) { if ($wrapLength !== null) {
$tagText = wordwrap($tagText, $wrapLength); $tagText = wordwrap($tagText, $wrapLength);
} }