1 | <?php |
||
19 | final class DocBlock |
||
20 | { |
||
21 | /** @var string The opening line for this docblock. */ |
||
22 | private $summary = ''; |
||
23 | |||
24 | /** @var DocBlock\Description The actual description for this docblock. */ |
||
25 | private $description = null; |
||
26 | |||
27 | /** @var Tag[] An array containing all the tags in this docblock; except inline. */ |
||
28 | private $tags = array(); |
||
29 | |||
30 | /** @var Context Information about the context of this DocBlock. */ |
||
31 | private $context = null; |
||
32 | |||
33 | /** @var Location Information about the location of this DocBlock. */ |
||
34 | private $location = null; |
||
35 | |||
36 | /** @var bool Is this DocBlock (the start of) a template? */ |
||
37 | private $isTemplateStart = false; |
||
38 | |||
39 | /** @var bool Does this DocBlock signify the end of a DocBlock template? */ |
||
40 | private $isTemplateEnd = false; |
||
41 | |||
42 | /** |
||
43 | * @param string $summary |
||
44 | * @param DocBlock\Description $description |
||
45 | * @param DocBlock\Tag[] $tags |
||
46 | * @param Context $context The context in which the DocBlock occurs. |
||
47 | * @param Location $location The location within the file that this DocBlock occurs in. |
||
48 | * @param bool $isTemplateStart |
||
49 | * @param bool $isTemplateEnd |
||
50 | */ |
||
51 | 15 | public function __construct( |
|
52 | $summary = '', |
||
53 | DocBlock\Description $description = null, |
||
54 | array $tags = [], |
||
55 | Context $context = null, |
||
56 | Location $location = null, |
||
57 | $isTemplateStart = false, |
||
58 | $isTemplateEnd = false |
||
59 | ) |
||
|
|||
60 | { |
||
61 | 15 | Assert::string($summary); |
|
62 | 14 | Assert::boolean($isTemplateStart); |
|
63 | 13 | Assert::boolean($isTemplateEnd); |
|
64 | 12 | Assert::allIsInstanceOf($tags, Tag::class); |
|
65 | |||
66 | 11 | $this->summary = $summary; |
|
67 | 11 | $this->description = $description ?: new DocBlock\Description(''); |
|
68 | 11 | foreach ($tags as $tag) { |
|
69 | 3 | $this->addTag($tag); |
|
70 | 11 | } |
|
71 | |||
72 | 11 | $this->context = $context; |
|
73 | 11 | $this->location = $location; |
|
74 | |||
75 | 11 | $this->isTemplateEnd = $isTemplateEnd; |
|
76 | 11 | $this->isTemplateStart = $isTemplateStart; |
|
77 | 11 | } |
|
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | 1 | public function getSummary() |
|
86 | |||
87 | /** |
||
88 | * @return DocBlock\Description |
||
89 | */ |
||
90 | 1 | public function getDescription() |
|
94 | |||
95 | /** |
||
96 | * Returns the current context. |
||
97 | * |
||
98 | * @return Context |
||
99 | */ |
||
100 | 1 | public function getContext() |
|
104 | |||
105 | /** |
||
106 | * Returns the current location. |
||
107 | * |
||
108 | * @return Location |
||
109 | */ |
||
110 | 1 | public function getLocation() |
|
114 | |||
115 | /** |
||
116 | * Returns whether this DocBlock is the start of a Template section. |
||
117 | * |
||
118 | * A Docblock may serve as template for a series of subsequent DocBlocks. This is indicated by a special marker |
||
119 | * (`#@+`) that is appended directly after the opening `/**` of a DocBlock. |
||
120 | * |
||
121 | * An example of such an opening is: |
||
122 | * |
||
123 | * ``` |
||
124 | * /**#@+ |
||
125 | * * My DocBlock |
||
126 | * * / |
||
127 | * ``` |
||
128 | * |
||
129 | * The description and tags (not the summary!) are copied onto all subsequent DocBlocks and also applied to all |
||
130 | * elements that follow until another DocBlock is found that contains the closing marker (`#@-`). |
||
131 | * |
||
132 | * @see self::isTemplateEnd() for the check whether a closing marker was provided. |
||
133 | * |
||
134 | * @return boolean |
||
135 | */ |
||
136 | 1 | public function isTemplateStart() |
|
140 | |||
141 | /** |
||
142 | * Returns whether this DocBlock is the end of a Template section. |
||
143 | * |
||
144 | * @see self::isTemplateStart() for a more complete description of the Docblock Template functionality. |
||
145 | * |
||
146 | * @return boolean |
||
147 | */ |
||
148 | 1 | public function isTemplateEnd() |
|
152 | |||
153 | /** |
||
154 | * Returns the tags for this DocBlock. |
||
155 | * |
||
156 | * @return Tag[] |
||
157 | */ |
||
158 | 1 | public function getTags() |
|
162 | |||
163 | /** |
||
164 | * Returns an array of tags matching the given name. If no tags are found |
||
165 | * an empty array is returned. |
||
166 | * |
||
167 | * @param string $name String to search by. |
||
168 | * |
||
169 | * @return Tag[] |
||
170 | */ |
||
171 | 2 | public function getTagsByName($name) |
|
172 | { |
||
173 | 2 | Assert::string($name); |
|
174 | |||
175 | 1 | $result = array(); |
|
176 | |||
177 | /** @var Tag $tag */ |
||
178 | 1 | foreach ($this->getTags() as $tag) { |
|
179 | 1 | if ($tag->getName() != $name) { |
|
180 | 1 | continue; |
|
181 | } |
||
182 | |||
183 | 1 | $result[] = $tag; |
|
184 | 1 | } |
|
185 | |||
186 | 1 | return $result; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Checks if a tag of a certain type is present in this DocBlock. |
||
191 | * |
||
192 | * @param string $name Tag name to check for. |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 2 | public function hasTag($name) |
|
197 | { |
||
198 | 2 | Assert::string($name); |
|
199 | |||
200 | /** @var Tag $tag */ |
||
201 | 1 | foreach ($this->getTags() as $tag) { |
|
202 | 1 | if ($tag->getName() == $name) { |
|
203 | 1 | return true; |
|
204 | } |
||
205 | 1 | } |
|
206 | |||
207 | 1 | return false; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Adds a tag to this DocBlock. |
||
212 | * |
||
213 | * @param Tag $tag The tag to add. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | 3 | private function addTag(Tag $tag) |
|
221 | } |
||
222 |