1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* phpDocumentor |
4
|
|
|
* |
5
|
|
|
* PHP Version 5.3 |
6
|
|
|
* |
7
|
|
|
* @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com) |
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
9
|
|
|
* @link http://phpdoc.org |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace phpDocumentor\Plugin\Core\Transformer\Writer\Xml; |
13
|
|
|
|
14
|
|
|
use phpDocumentor\Descriptor\Tag\MethodDescriptor; |
15
|
|
|
use phpDocumentor\Descriptor\Tag\BaseTypes\TypedAbstract; |
16
|
|
|
use phpDocumentor\Descriptor\Tag\BaseTypes\TypedVariableAbstract; |
17
|
|
|
use phpDocumentor\Descriptor\Tag\DeprecatedDescriptor; |
18
|
|
|
use phpDocumentor\Descriptor\Tag\LinkDescriptor; |
19
|
|
|
use phpDocumentor\Descriptor\Tag\SeeDescriptor; |
20
|
|
|
use phpDocumentor\Descriptor\Tag\SinceDescriptor; |
21
|
|
|
use phpDocumentor\Descriptor\Tag\UsesDescriptor; |
22
|
|
|
use phpDocumentor\Descriptor\Tag\VersionDescriptor; |
23
|
|
|
use phpDocumentor\Descriptor\TagDescriptor; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Creates an XML Element 'tag' and appends it to the provided parent element. |
27
|
|
|
* |
28
|
|
|
* With this class we convert a TagDescriptor, or any child thereof, into an XML element that is subsequently appended |
29
|
|
|
* onto a provided parent element (usually an XML Element that represents a DocBlock). |
30
|
|
|
* |
31
|
|
|
* During the conversion process the generated XML Element is enriched with additional elements and attributes based on |
32
|
|
|
* which tags are provided (or more specifically which methods that support). |
33
|
|
|
*/ |
34
|
|
|
class TagConverter |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Export this tag to the given DocBlock. |
38
|
|
|
* |
39
|
|
|
* @param \DOMElement $parent Element to augment. |
40
|
|
|
* @param TagDescriptor $tag The tag to export. |
41
|
|
|
* |
42
|
|
|
* @return \DOMElement |
43
|
|
|
*/ |
44
|
9 |
|
public function convert(\DOMElement $parent, TagDescriptor $tag) |
45
|
|
|
{ |
46
|
9 |
|
$description = $this->getDescription($tag); |
47
|
|
|
|
48
|
9 |
|
$child = new \DOMElement('tag'); |
49
|
9 |
|
$parent->appendChild($child); |
50
|
|
|
|
51
|
9 |
|
$child->setAttribute('name', str_replace('&', '&', $tag->getName())); |
52
|
9 |
|
$child->setAttribute('line', $parent->getAttribute('line')); |
53
|
9 |
|
$child->setAttribute('description', str_replace('&', '&', $description)); |
54
|
9 |
|
$this->addTypes($tag, $child); |
55
|
|
|
|
56
|
|
|
// TODO: make the tests below configurable from the outside so that more could be added using plugins |
57
|
9 |
|
if ($tag instanceof TypedVariableAbstract) { |
58
|
3 |
|
$child->setAttribute('variable', str_replace('&', '&', $tag->getVariableName())); |
59
|
|
|
} |
60
|
9 |
|
if ($tag instanceof SeeDescriptor || $tag instanceof UsesDescriptor) { |
61
|
1 |
|
$child->setAttribute('link', str_replace('&', '&', $tag->getReference())); |
62
|
|
|
} |
63
|
9 |
|
if ($tag instanceof LinkDescriptor) { |
64
|
1 |
|
$child->setAttribute('link', str_replace('&', '&', $tag->getLink())); |
65
|
|
|
} |
66
|
9 |
|
if ($tag instanceof MethodDescriptor) { |
67
|
1 |
|
$child->setAttribute('method_name', str_replace('&', '&', $tag->getMethodName())); |
68
|
|
|
} |
69
|
|
|
|
70
|
9 |
|
return $child; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the description from the Tag with the version prepended when applicable. |
75
|
|
|
* |
76
|
|
|
* @param TagDescriptor $tag |
77
|
|
|
* |
78
|
|
|
* @todo the version should not be prepended here but in templates; remove this. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
3 |
|
protected function getDescription(TagDescriptor $tag) |
83
|
|
|
{ |
84
|
3 |
|
$description = ''; |
85
|
|
|
|
86
|
|
|
if ( |
87
|
3 |
|
$tag instanceof VersionDescriptor || |
88
|
2 |
|
$tag instanceof DeprecatedDescriptor || |
89
|
3 |
|
$tag instanceof SinceDescriptor |
90
|
|
|
) { |
91
|
1 |
|
$description .= $tag->getVersion() . ' '; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
$description .= $tag->getDescription(); |
95
|
|
|
|
96
|
3 |
|
return trim($description); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Adds type elements and a type attribute to the tag if a method 'getTypes' is present. |
101
|
|
|
* |
102
|
|
|
* @param TagDescriptor $tag |
103
|
|
|
* @param \DOMElement $child |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
3 |
|
protected function addTypes(TagDescriptor $tag, \DOMElement $child) |
108
|
|
|
{ |
109
|
3 |
|
$typeString = ''; |
110
|
|
|
|
111
|
3 |
|
if ($tag instanceof TypedAbstract) { |
112
|
3 |
|
$types = $tag->getTypes(); |
113
|
|
|
|
114
|
3 |
|
if ($types instanceof \IteratorAggregate) { |
115
|
1 |
|
foreach ($types as $type) { |
116
|
1 |
|
$typeString .= $type . '|'; |
117
|
|
|
|
118
|
|
|
/** @var \DOMElement $typeNode */ |
119
|
1 |
|
$typeNode = $child->appendChild(new \DOMElement('type')); |
120
|
1 |
|
$typeNode->appendChild(new \DOMText($type)); |
121
|
|
|
} |
122
|
|
|
} else { |
123
|
2 |
|
$typeString .= $types . '|'; |
124
|
|
|
|
125
|
|
|
/** @var \DOMElement $typeNode */ |
126
|
2 |
|
$typeNode = $child->appendChild(new \DOMElement('type')); |
127
|
2 |
|
$typeNode->appendChild(new \DOMText($types)); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
$child->setAttribute('type', str_replace('&', '&', rtrim($typeString, '|'))); |
131
|
|
|
} |
132
|
3 |
|
} |
133
|
|
|
} |
134
|
|
|
|