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