7 * @author Barry vd. Heuvel <barryvdh@gmail.com>
8 * @copyright 2013 Mike van Riel / Naenius (http://www.naenius.com)
9 * @license http://www.opensource.org/licenses/mit-license.php MIT
10 * @link http://phpdoc.org
13 namespace phpDocumentor\Reflection\DocBlock;
15 use phpDocumentor\Reflection\DocBlock;
18 * Serializes a DocBlock instance.
20 * @author Barry vd. Heuvel <barryvdh@gmail.com>
21 * @license http://www.opensource.org/licenses/mit-license.php MIT
22 * @link http://phpdoc.org
27 /** @var string The string to indent the comment with. */
28 protected $indentString = ' ';
30 /** @var int The number of times the indent string is repeated. */
31 protected $indent = 0;
33 /** @var bool Whether to indent the first line. */
34 protected $isFirstLineIndented = true;
36 /** @var int|null The max length of a line. */
37 protected $lineLength = null;
40 * Create a Serializer instance.
42 * @param int $indent The number of times the indent string is
44 * @param string $indentString The string to indent the comment with.
45 * @param bool $indentFirstLine Whether to indent the first line.
46 * @param int|null $lineLength The max length of a line or NULL to
47 * disable line wrapping.
49 public function __construct(
52 $indentFirstLine = true,
55 $this->setIndentationString($indentString);
56 $this->setIndent($indent);
57 $this->setIsFirstLineIndented($indentFirstLine);
58 $this->setLineLength($lineLength);
62 * Sets the string to indent comments with.
64 * @param string $indentationString The string to indent comments with.
66 * @return $this This serializer object.
68 public function setIndentationString($indentString)
70 $this->indentString = (string)$indentString;
75 * Gets the string to indent comments with.
77 * @return string The indent string.
79 public function getIndentationString()
81 return $this->indentString;
85 * Sets the number of indents.
87 * @param int $indent The number of times the indent string is repeated.
89 * @return $this This serializer object.
91 public function setIndent($indent)
93 $this->indent = (int)$indent;
98 * Gets the number of indents.
100 * @return int The number of times the indent string is repeated.
102 public function getIndent()
104 return $this->indent;
108 * Sets whether or not the first line should be indented.
110 * Sets whether or not the first line (the one with the "/**") should be
113 * @param bool $indentFirstLine The new value for this setting.
115 * @return $this This serializer object.
117 public function setIsFirstLineIndented($indentFirstLine)
119 $this->isFirstLineIndented = (bool)$indentFirstLine;
124 * Gets whether or not the first line should be indented.
126 * @return bool Whether or not the first line should be indented.
128 public function isFirstLineIndented()
130 return $this->isFirstLineIndented;
134 * Sets the line length.
136 * Sets the length of each line in the serialization. Content will be
137 * wrapped within this limit.
139 * @param int|null $lineLength The length of each line. NULL to disable line
140 * wrapping altogether.
142 * @return $this This serializer object.
144 public function setLineLength($lineLength)
146 $this->lineLength = null === $lineLength ? null : (int)$lineLength;
151 * Gets the line length.
153 * @return int|null The length of each line or NULL if line wrapping is
156 public function getLineLength()
158 return $this->lineLength;
162 * Generate a DocBlock comment.
164 * @param DocBlock The DocBlock to serialize.
166 * @return string The serialized doc block.
168 public function getDocComment(DocBlock $docblock)
170 $indent = str_repeat($this->indentString, $this->indent);
171 $firstIndent = $this->isFirstLineIndented ? $indent : '';
173 $text = $docblock->getText();
174 if ($this->lineLength) {
175 //3 === strlen(' * ')
176 $wrapLength = $this->lineLength - strlen($indent) - 3;
177 $text = wordwrap($text, $wrapLength);
179 $text = str_replace("\n", "\n{$indent} * ", $text);
181 $comment = "{$firstIndent}/**\n{$indent} * {$text}\n{$indent} *\n";
184 foreach ($docblock->getTags() as $tag) {
185 $tagText = (string) $tag;
186 if ($this->lineLength) {
187 $tagText = wordwrap($tagText, $wrapLength);
189 $tagText = str_replace("\n", "\n{$indent} * ", $tagText);
191 $comment .= "{$indent} * {$tagText}\n";
194 $comment .= $indent . ' */';