annotate vendor/phpdocumentor/reflection-docblock/src/DocBlock/Description.php @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents
children
rev   line source
Chris@2 1 <?php
Chris@2 2 /**
Chris@2 3 * This file is part of phpDocumentor.
Chris@2 4 *
Chris@2 5 * For the full copyright and license information, please view the LICENSE
Chris@2 6 * file that was distributed with this source code.
Chris@2 7 *
Chris@2 8 * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
Chris@2 9 * @license http://www.opensource.org/licenses/mit-license.php MIT
Chris@2 10 * @link http://phpdoc.org
Chris@2 11 */
Chris@2 12
Chris@2 13 namespace phpDocumentor\Reflection\DocBlock;
Chris@2 14
Chris@2 15 use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
Chris@2 16 use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
Chris@2 17 use Webmozart\Assert\Assert;
Chris@2 18
Chris@2 19 /**
Chris@2 20 * Object representing to description for a DocBlock.
Chris@2 21 *
Chris@2 22 * A Description object can consist of plain text but can also include tags. A Description Formatter can then combine
Chris@2 23 * a body template with sprintf-style placeholders together with formatted tags in order to reconstitute a complete
Chris@2 24 * description text using the format that you would prefer.
Chris@2 25 *
Chris@2 26 * Because parsing a Description text can be a verbose process this is handled by the {@see DescriptionFactory}. It is
Chris@2 27 * thus recommended to use that to create a Description object, like this:
Chris@2 28 *
Chris@2 29 * $description = $descriptionFactory->create('This is a {@see Description}', $context);
Chris@2 30 *
Chris@2 31 * The description factory will interpret the given body and create a body template and list of tags from them, and pass
Chris@2 32 * that onto the constructor if this class.
Chris@2 33 *
Chris@2 34 * > The $context variable is a class of type {@see \phpDocumentor\Reflection\Types\Context} and contains the namespace
Chris@2 35 * > and the namespace aliases that apply to this DocBlock. These are used by the Factory to resolve and expand partial
Chris@2 36 * > type names and FQSENs.
Chris@2 37 *
Chris@2 38 * If you do not want to use the DescriptionFactory you can pass a body template and tag listing like this:
Chris@2 39 *
Chris@2 40 * $description = new Description(
Chris@2 41 * 'This is a %1$s',
Chris@2 42 * [ new See(new Fqsen('\phpDocumentor\Reflection\DocBlock\Description')) ]
Chris@2 43 * );
Chris@2 44 *
Chris@2 45 * It is generally recommended to use the Factory as that will also apply escaping rules, while the Description object
Chris@2 46 * is mainly responsible for rendering.
Chris@2 47 *
Chris@2 48 * @see DescriptionFactory to create a new Description.
Chris@2 49 * @see Description\Formatter for the formatting of the body and tags.
Chris@2 50 */
Chris@2 51 class Description
Chris@2 52 {
Chris@2 53 /** @var string */
Chris@2 54 private $bodyTemplate;
Chris@2 55
Chris@2 56 /** @var Tag[] */
Chris@2 57 private $tags;
Chris@2 58
Chris@2 59 /**
Chris@2 60 * Initializes a Description with its body (template) and a listing of the tags used in the body template.
Chris@2 61 *
Chris@2 62 * @param string $bodyTemplate
Chris@2 63 * @param Tag[] $tags
Chris@2 64 */
Chris@2 65 public function __construct($bodyTemplate, array $tags = [])
Chris@2 66 {
Chris@2 67 Assert::string($bodyTemplate);
Chris@2 68
Chris@2 69 $this->bodyTemplate = $bodyTemplate;
Chris@2 70 $this->tags = $tags;
Chris@2 71 }
Chris@2 72
Chris@2 73 /**
Chris@2 74 * Returns the tags for this DocBlock.
Chris@2 75 *
Chris@2 76 * @return Tag[]
Chris@2 77 */
Chris@2 78 public function getTags()
Chris@2 79 {
Chris@2 80 return $this->tags;
Chris@2 81 }
Chris@2 82
Chris@2 83 /**
Chris@2 84 * Renders this description as a string where the provided formatter will format the tags in the expected string
Chris@2 85 * format.
Chris@2 86 *
Chris@2 87 * @param Formatter|null $formatter
Chris@2 88 *
Chris@2 89 * @return string
Chris@2 90 */
Chris@2 91 public function render(Formatter $formatter = null)
Chris@2 92 {
Chris@2 93 if ($formatter === null) {
Chris@2 94 $formatter = new PassthroughFormatter();
Chris@2 95 }
Chris@2 96
Chris@2 97 $tags = [];
Chris@2 98 foreach ($this->tags as $tag) {
Chris@2 99 $tags[] = '{' . $formatter->format($tag) . '}';
Chris@2 100 }
Chris@2 101
Chris@2 102 return vsprintf($this->bodyTemplate, $tags);
Chris@2 103 }
Chris@2 104
Chris@2 105 /**
Chris@2 106 * Returns a plain string representation of this description.
Chris@2 107 *
Chris@2 108 * @return string
Chris@2 109 */
Chris@2 110 public function __toString()
Chris@2 111 {
Chris@2 112 return $this->render();
Chris@2 113 }
Chris@2 114 }