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