From eb4ab2c7206b3ac97289da72416efac31ddac50a Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 26 May 2013 22:36:31 +0200 Subject: [PATCH] Move serializer to seperate class --- src/phpDocumentor/Reflection/DocBlock.php | 40 ++---- .../Reflection/DocBlock/Serializer.php | 122 ++++++++++++++++++ 2 files changed, 132 insertions(+), 30 deletions(-) create mode 100644 src/phpDocumentor/Reflection/DocBlock/Serializer.php diff --git a/src/phpDocumentor/Reflection/DocBlock.php b/src/phpDocumentor/Reflection/DocBlock.php index 62fab1b..3a9f524 100644 --- a/src/phpDocumentor/Reflection/DocBlock.php +++ b/src/phpDocumentor/Reflection/DocBlock.php @@ -229,12 +229,12 @@ class DocBlock implements \Reflector $this->tags = $result; } - public function getDescription(){ + public function getText(){ $short = $this->getShortDescription(); $long = $this->getLongDescription()->getContents(); if($long){ - return $short . "\n" . $long; + return $short . "\n\n" . $long; }else{ return $short; } @@ -243,11 +243,11 @@ class DocBlock implements \Reflector /** * Set the short and long description. * - * @param $docblock + * @param string $docblock * @return $this */ - public function setDescription($docblock){ - list($short, $long) = $this->splitDocBlock($docblock); + public function setText($comment){ + list($short, $long) = $this->splitDocBlock($comment); $this->short_description = $short; $this->long_description = new DocBlock\Description($long, $this); return $this; @@ -344,14 +344,14 @@ class DocBlock implements \Reflector return false; } - + /** * Appends a tag at the end of the list of tags. - * + * * @param Tag $tag The tag to add. - * + * * @return Tag The newly added tag. - * + * * @throws \LogicException When the tag belongs to a different DocBlock. */ public function appendTag(Tag $tag) @@ -359,7 +359,7 @@ class DocBlock implements \Reflector if (null === $tag->getDocBlock()) { $tag->setDocBlock($this); } - + if ($tag->getDocBlock() === $this) { $this->tags[] = $tag; } else { @@ -371,26 +371,6 @@ class DocBlock implements \Reflector return $tag; } - /** - * Generate a DocBlock Comment - * - * @return string - */ - public function getDocComment($indentation = ''){ - - $description = str_replace("\n", "\n$indentation * ", $this->getDescription()); - - $comment = "$indentation/**\n$indentation * $description\n$indentation *\n"; - - /** @var Tag $tag */ - foreach ($this->getTags() as $tag) { - $comment .= $indentation.' * @'. $tag->getName() . " " . $tag->getContent() . "\n"; - } - - $comment .= $indentation.' */'; - - return $comment; - } /** * Builds a string representation of this object. diff --git a/src/phpDocumentor/Reflection/DocBlock/Serializer.php b/src/phpDocumentor/Reflection/DocBlock/Serializer.php new file mode 100644 index 0000000..5e81269 --- /dev/null +++ b/src/phpDocumentor/Reflection/DocBlock/Serializer.php @@ -0,0 +1,122 @@ + + * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com) + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link http://phpdoc.org + */ + +namespace phpDocumentor\Reflection\DocBlock; + +use phpDocumentor\Reflection\DocBlock; + +/** + * Serializes a DocBlock instance + * + * @author Barry vd. Heuvel + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link http://phpdoc.org + */ +class Serializer +{ + + /** @var string The string to indent the comment with */ + protected $indentString; + + /** @var int The number of times $indentString is repeated */ + protected $indent; + + /** @var bool Indent the first line */ + protected $indentFirstLine; + + /** @var int The max length of a description line. */ + protected $lineLength = null; + + /** + * Create a Serializer instance. + * + * @param string $indentString + * @param int $indent + * @param bool $indentFirstLine + * @internal param string $indentationString The indentation string. + */ + public function __construct($indentString = ' ', $indent = 4, $indentFirstLine = true) + { + $this->indentString = $indentString; + $this->indent = $indent; + $this->indentFirstLine = $indentFirstLine; + } + + /** + * @param $indentationString + * @return $this + */ + public function setIndentationString($indentationString) + { + $this->indentation = $indentationString; + return $this; + } + + /** + * @param $indent + * @return $this + */ + public function setIndent($indent){ + $this->indent = $indent; + return $this; + } + + /** + * @param $indentFirstLine + * @return $this + */ + public function setIndentFirstLine($indentFirstLine){ + $this->indentFirstLine = $indentFirstLine; + return $this; + } + + /** + * @param $lineLength + * @return $this + */ + public function setLineLength($lineLength){ + $this->lineLength = $lineLength; + return $this; + } + + /** + * Generate a DocBlock Comment + * + * @param DocBlock The DocBlock to serialize + * @return string + */ + public function getDocComment($phpdoc){ + + $indent = ''; + for($i=0;$i<$this->indent;$i++){ + $indent .= $this->indentString; + } + $firstIndent = $this->indentFirstLine ? $indent : ''; + + $description = $phpdoc->getText(); + if($this->lineLength){ + $description = wordwrap($description, $this->lineLength); + } + $description = str_replace("\n", "\n$indent * ", $description); + + $comment = "$firstIndent/**\n$indent * $description\n$indent *\n"; + + /** @var Tag $tag */ + foreach ($phpdoc->getTags() as $tag) { + $comment .= $indent.' * @'. $tag->getName() . " " . $tag->getContent() . "\n"; + } + + $comment .= $indent.' */'; + + return $comment; + } +}