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