7 * @author Mike van Riel <mike.vanriel@naenius.com>
8 * @copyright 2010-2011 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 * Parses a Description of a DocBlock or tag.
20 * @author Mike van Riel <mike.vanriel@naenius.com>
21 * @license http://www.opensource.org/licenses/mit-license.php MIT
22 * @link http://phpdoc.org
24 class Description implements \Reflector
27 protected $contents = '';
29 /** @var array The contents, as an array of strings and Tag objects. */
30 protected $parsedContents = null;
32 /** @var DocBlock The DocBlock which this description belongs to. */
33 protected $docblock = null;
36 * Populates the fields of a description.
38 * @param string $content The description's conetnts.
39 * @param DocBlock $docblock The DocBlock which this description belongs to.
41 public function __construct($content, DocBlock $docblock = null)
43 $this->setContent($content)->setDocBlock($docblock);
47 * Gets the text of this description.
51 public function getContents()
53 return $this->contents;
57 * Sets the text of this description.
59 * @param string $content The new text of this description.
63 public function setContent($content)
65 $this->contents = trim($content);
67 $this->parsedContents = null;
72 * Returns the parsed text of this description.
74 * @return array An array of strings and tag objects, in the order they
75 * occur within the description.
77 public function getParsedContents()
79 if (null === $this->parsedContents) {
80 $this->parsedContents = preg_split(
82 # "{@}" is not a valid inline tag. This ensures that
83 # we do not treat it as one, but treat it literally.
85 # We want to capture the whole tag line, but without the
86 # inline tag delimiters.
88 # Match everything up to the next delimiter.
90 # Nested inline tag content should not be captured, or
91 # it will appear in the result separately.
93 # Match nested inline tags.
95 # Because we did not catch the tag delimiters
96 # earlier, we must be explicit with them here.
97 # Notice that this also matches "{}", as a way
98 # to later introduce it as an escape sequence.
101 # Make sure we match hanging "{".
104 # Match content after the nested inline tag.
106 )* # If there are more inline tags, match them as well.
107 # We use "*" since there may not be any nested inline
113 PREG_SPLIT_DELIM_CAPTURE
116 $count = count($this->parsedContents);
117 for ($i=1; $i<$count; $i += 2) {
118 $this->parsedContents[$i] = Tag::createInstance(
119 $this->parsedContents[$i],
124 //In order to allow "literal" inline tags, the otherwise invalid
125 //sequence "{@}" is changed to "@", and "{}" is changed to "}".
126 //See unit tests for examples.
127 for ($i=0; $i<$count; $i += 2) {
128 $this->parsedContents[$i] = str_replace(
131 $this->parsedContents[$i]
135 return $this->parsedContents;
139 * Return a formatted variant of the Long Description using MarkDown.
141 * @todo this should become a more intelligent piece of code where the
142 * configuration contains a setting what format long descriptions are.
144 * @codeCoverageIgnore Will be removed soon, in favor of adapters at
145 * PhpDocumentor itself that will process text in various formats.
149 public function getFormattedContents()
151 $result = $this->contents;
153 // if the long description contains a plain HTML <code> element, surround
154 // it with a pre element. Please note that we explicitly used str_replace
155 // and not preg_replace to gain performance
156 if (strpos($result, '<code>') !== false) {
157 $result = str_replace(
158 array('<code>', "<code>\r\n", "<code>\n", "<code>\r", '</code>'),
159 array('<pre><code>', '<code>', '<code>', '<code>', '</code></pre>'),
164 if (class_exists('Parsedown')) {
165 $markdown = \Parsedown::instance();
166 $result = $markdown->parse($result);
167 } elseif (class_exists('dflydev\markdown\MarkdownExtraParser')) {
168 $markdown = new \dflydev\markdown\MarkdownExtraParser();
169 $result = $markdown->transformMarkdown($result);
172 return trim($result);
176 * Gets the docblock this tag belongs to.
178 * @return DocBlock The docblock this description belongs to.
180 public function getDocBlock()
182 return $this->docblock;
186 * Sets the docblock this tag belongs to.
188 * @param DocBlock $docblock The new docblock this description belongs to.
189 * Setting NULL removes any association.
193 public function setDocBlock(DocBlock $docblock = null)
195 $this->docblock = $docblock;
201 * Builds a string representation of this object.
203 * @todo determine the exact format as used by PHP Reflection
207 * @codeCoverageIgnore Not yet implemented
209 public static function export()
211 throw new \Exception('Not yet implemented');
215 * Returns the long description as a string.
219 public function __toString()
221 return $this->getContents();