|
4 | 4 | *
|
5 | 5 | * PHP Version 5.3
|
6 | 6 | *
|
7 |
| - * @author Mike van Riel <mike.vanriel@naenius.com> |
8 |
| - * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com) |
| 7 | + * @author Barry vd. Heuvel <barryvdh@gmail.com> |
| 8 | + * @copyright 2013 Mike van Riel / Naenius (http://www.naenius.com) |
9 | 9 | * @license http://www.opensource.org/licenses/mit-license.php MIT
|
10 | 10 | * @link http://phpdoc.org
|
11 | 11 | */
|
|
15 | 15 | use phpDocumentor\Reflection\DocBlock;
|
16 | 16 |
|
17 | 17 | /**
|
18 |
| - * Serializes a DocBlock instance |
| 18 | + * Serializes a DocBlock instance. |
19 | 19 | *
|
20 | 20 | * @author Barry vd. Heuvel <[email protected]>
|
21 | 21 | * @license http://www.opensource.org/licenses/mit-license.php MIT
|
|
24 | 24 | class Serializer
|
25 | 25 | {
|
26 | 26 |
|
27 |
| - /** @var string The string to indent the comment with */ |
28 |
| - protected $indentString; |
| 27 | + /** @var string The string to indent the comment with. */ |
| 28 | + protected $indentString = ' '; |
29 | 29 |
|
30 |
| - /** @var int The number of times $indentString is repeated */ |
31 |
| - protected $indent; |
| 30 | + /** @var int The number of times the indent string is repeated. */ |
| 31 | + protected $indent = 0; |
32 | 32 |
|
33 |
| - /** @var bool Indent the first line */ |
34 |
| - protected $indentFirstLine; |
| 33 | + /** @var bool Whether to indent the first line. */ |
| 34 | + protected $isFirstLineIndented = true; |
35 | 35 |
|
36 |
| - /** @var int The max length of a description line. */ |
| 36 | + /** @var int The max length of a line. */ |
37 | 37 | protected $lineLength = null;
|
38 | 38 |
|
39 | 39 | /**
|
40 | 40 | * Create a Serializer instance.
|
41 | 41 | *
|
42 |
| - * @param string $indentString |
43 |
| - * @param int $indent |
44 |
| - * @param bool $indentFirstLine |
45 |
| - * @internal param string $indentationString The indentation string. |
| 42 | + * @param int $indent The number of times the indent string is |
| 43 | + * repeated. |
| 44 | + * @param string $indentString The string to indent the comment with. |
| 45 | + * @param bool $indentFirstLine Whether to indent the first line. |
| 46 | + * @param int $lineLength The max length of a line. |
46 | 47 | */
|
47 |
| - public function __construct($indentString = ' ', $indent = 4, $indentFirstLine = true) |
48 |
| - { |
49 |
| - $this->indentString = $indentString; |
50 |
| - $this->indent = $indent; |
51 |
| - $this->indentFirstLine = $indentFirstLine; |
| 48 | + public function __construct( |
| 49 | + $indent = 0, |
| 50 | + $indentString = ' ', |
| 51 | + $indentFirstLine = true, |
| 52 | + $lineLength = null |
| 53 | + ) { |
| 54 | + $this->setIndentationString($indentString); |
| 55 | + $this->setIndent($indent); |
| 56 | + $this->setIsFirstLineIndented($indentFirstLine); |
| 57 | + $this->setLineLength($lineLength); |
52 | 58 | }
|
53 | 59 |
|
54 | 60 | /**
|
55 |
| - * @param $indentationString |
56 |
| - * @return $this |
| 61 | + * Sets the string to indent comments with. |
| 62 | + * |
| 63 | + * @param string $indentationString The string to indent comments with. |
| 64 | + * |
| 65 | + * @return $this This serializer object. |
57 | 66 | */
|
58 |
| - public function setIndentationString($indentationString) |
| 67 | + public function setIndentationString($indentString) |
59 | 68 | {
|
60 |
| - $this->indentation = $indentationString; |
| 69 | + $this->indentString = (string)$indentString; |
61 | 70 | return $this;
|
62 | 71 | }
|
63 | 72 |
|
64 | 73 | /**
|
65 |
| - * @param $indent |
66 |
| - * @return $this |
| 74 | + * Gets the string to indent comments with. |
| 75 | + * |
| 76 | + * @return string The indent string. |
67 | 77 | */
|
68 |
| - public function setIndent($indent){ |
69 |
| - $this->indent = $indent; |
70 |
| - return $this; |
| 78 | + public function getIndentationString() |
| 79 | + { |
| 80 | + return $this->indentString; |
71 | 81 | }
|
72 | 82 |
|
73 | 83 | /**
|
74 |
| - * @param $indentFirstLine |
75 |
| - * @return $this |
| 84 | + * Sets the number of indents. |
| 85 | + * |
| 86 | + * @param int $indent The number of times the indent string is repeated. |
| 87 | + * |
| 88 | + * @return $this This serializer object. |
76 | 89 | */
|
77 |
| - public function setIndentFirstLine($indentFirstLine){ |
78 |
| - $this->indentFirstLine = $indentFirstLine; |
| 90 | + public function setIndent($indent) |
| 91 | + { |
| 92 | + $this->indent = (int)$indent; |
79 | 93 | return $this;
|
80 | 94 | }
|
81 | 95 |
|
82 | 96 | /**
|
83 |
| - * @param $lineLength |
84 |
| - * @return $this |
| 97 | + * Gets the number of indents. |
| 98 | + * |
| 99 | + * @return int The number of times the indent string is repeated. |
| 100 | + */ |
| 101 | + public function getIndent() |
| 102 | + { |
| 103 | + return $this->indent; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Sets whether or not the first line should be indented. |
| 108 | + * |
| 109 | + * Sets whether or not the first line (the one with the "/**") should be |
| 110 | + * indented. |
| 111 | + * |
| 112 | + * @param bool $indentFirstLine The new value for this setting. |
| 113 | + * |
| 114 | + * @return $this This serializer object. |
85 | 115 | */
|
86 |
| - public function setLineLength($lineLength){ |
87 |
| - $this->lineLength = $lineLength; |
| 116 | + public function setIsFirstLineIndented($indentFirstLine) |
| 117 | + { |
| 118 | + $this->isFirstLineIndented = (bool)$indentFirstLine; |
88 | 119 | return $this;
|
89 | 120 | }
|
90 | 121 |
|
91 | 122 | /**
|
92 |
| - * Generate a DocBlock Comment |
93 |
| - * |
94 |
| - * @param DocBlock The DocBlock to serialize |
95 |
| - * @return string |
| 123 | + * Gets whether or not the first line should be indented. |
| 124 | + * |
| 125 | + * @return bool Whether or not the first line should be indented. |
96 | 126 | */
|
97 |
| - public function getDocComment($phpdoc){ |
| 127 | + public function isFirstLineIndented() |
| 128 | + { |
| 129 | + return $this->isFirstLineIndented; |
| 130 | + } |
98 | 131 |
|
99 |
| - $indent = ''; |
100 |
| - for($i=0;$i<$this->indent;$i++){ |
101 |
| - $indent .= $this->indentString; |
102 |
| - } |
103 |
| - $firstIndent = $this->indentFirstLine ? $indent : ''; |
| 132 | + /** |
| 133 | + * Sets the line length. |
| 134 | + * |
| 135 | + * Sets the length of each line in the serialization. Content will be |
| 136 | + * wrapped within this limit. |
| 137 | + * |
| 138 | + * @param int $lineLength The length of each line. NULL to disable line |
| 139 | + * wrapping altogether. |
| 140 | + * |
| 141 | + * @return $this This serializer object. |
| 142 | + */ |
| 143 | + public function setLineLength($lineLength) |
| 144 | + { |
| 145 | + $this->lineLength = null === $lineLength ? null : (int)$lineLength; |
| 146 | + return $this; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Gets the line length. |
| 151 | + * |
| 152 | + * @return int The length of each line or NULL if line wrapping is disabled. |
| 153 | + */ |
| 154 | + public function getLineLength() |
| 155 | + { |
| 156 | + return $this->lineLength; |
| 157 | + } |
104 | 158 |
|
105 |
| - $description = $phpdoc->getText(); |
106 |
| - if($this->lineLength){ |
107 |
| - $description = wordwrap($description, $this->lineLength); |
| 159 | + /** |
| 160 | + * Generate a DocBlock comment. |
| 161 | + * |
| 162 | + * @param DocBlock The DocBlock to serialize. |
| 163 | + * |
| 164 | + * @return string The serialized doc block. |
| 165 | + */ |
| 166 | + public function getDocComment(DocBlock $docblock) |
| 167 | + { |
| 168 | + $indent = str_repeat($this->indentString, $this->indent); |
| 169 | + $firstIndent = $this->isFirstLineIndented ? $indent : ''; |
| 170 | + |
| 171 | + $text = $docblock->getText(); |
| 172 | + if ($this->lineLength) { |
| 173 | + $text = wordwrap( |
| 174 | + $text, |
| 175 | + $this->lineLength - strlen($indent) - 3/*strlen(' * ')*/ |
| 176 | + ); |
108 | 177 | }
|
109 |
| - $description = str_replace("\n", "\n$indent * ", $description); |
| 178 | + $text = str_replace("\n", "\n$indent * ", $text); |
110 | 179 |
|
111 |
| - $comment = "$firstIndent/**\n$indent * $description\n$indent *\n"; |
| 180 | + $comment = "$firstIndent/**\n$indent * $text\n$indent *\n"; |
112 | 181 |
|
113 | 182 | /** @var Tag $tag */
|
114 |
| - foreach ($phpdoc->getTags() as $tag) { |
115 |
| - $comment .= $indent.' * @'. $tag->getName() . " " . $tag->getContent() . "\n"; |
| 183 | + foreach ($docblock->getTags() as $tag) { |
| 184 | + $tagName = $tag->getName(); |
| 185 | + $prefixLength = 1/*strlen('@')*/ + strlen($tagName); |
| 186 | + |
| 187 | + //Added to take the first line of the tag into account. |
| 188 | + $tagContent = str_repeat(' ', $prefixLength) . $tag->getContent(); |
| 189 | + |
| 190 | + if ($this->lineLength) { |
| 191 | + $tagContent = wordwrap( |
| 192 | + $tagContent, |
| 193 | + $this->lineLength - strlen($indent) - 3/*strlen(' * ')*/ |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + //Clean up the prefix. |
| 198 | + substr_replace($tagContent, '', 0, $prefixLength); |
| 199 | + |
| 200 | + $tagContent = str_replace("\n", "\n$indent * ", $tagContent); |
| 201 | + |
| 202 | + $comment .= "$indent * @{$tagName} {$tagContent}\n"; |
116 | 203 | }
|
117 | 204 |
|
118 |
| - $comment .= $indent.' */'; |
| 205 | + $comment .= $indent . ' */'; |
119 | 206 |
|
120 | 207 | return $comment;
|
121 | 208 | }
|
|
0 commit comments