|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ClassGeneration package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Antonio Spinelli <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ClassGeneration\DocBlock; |
|
13
|
|
|
|
|
14
|
|
|
use ClassGeneration\Collection\ArrayCollection; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Antonio Spinelli <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class TagCollection extends ArrayCollection implements TagCollectionInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Unique tags for Docblock. |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $uniqueTags = array( |
|
27
|
|
|
'access', 'category', 'copyright', |
|
28
|
|
|
'license', 'name', 'package', |
|
29
|
|
|
'return', 'subpackage', 'var', 'version', |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Adds a new Tag on Dockblock. |
|
34
|
|
|
* |
|
35
|
|
|
* @param TagInterface $tag |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \InvalidArgumentException |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
24 |
|
public function add($tag) |
|
41
|
|
|
{ |
|
42
|
24 |
|
if (!$tag instanceof TagInterface) { |
|
43
|
1 |
|
throw new \InvalidArgumentException('This tag is not allowed'); |
|
44
|
|
|
} |
|
45
|
24 |
|
if ($this->isUniqueTag($tag->getName())) { |
|
46
|
10 |
|
$this->removeByName($tag->getName()); |
|
47
|
10 |
|
} |
|
48
|
|
|
|
|
49
|
24 |
|
return parent::add($tag); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
25 |
|
public function isUniqueTag($tagName) |
|
56
|
|
|
{ |
|
57
|
25 |
|
return in_array($tagName, $this->uniqueTags); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get Tag Iterator. |
|
62
|
|
|
* @return TagIterator|TagInterface[] |
|
63
|
|
|
*/ |
|
64
|
23 |
|
public function getIterator() |
|
65
|
|
|
{ |
|
66
|
23 |
|
return new TagIterator($this); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
* @return TagInterface |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function current() |
|
74
|
|
|
{ |
|
75
|
1 |
|
return parent::current(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritdoc |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function removeByReferece($reference) |
|
82
|
|
|
{ |
|
83
|
2 |
|
$removedList = new self(); |
|
84
|
2 |
|
$list = $this->getIterator(); |
|
85
|
2 |
|
foreach ($list as $index => $tag) { |
|
86
|
2 |
|
if ((is_array($reference) && in_array($tag->getReferenced(), $reference)) |
|
87
|
2 |
|
|| ($tag->getReferenced() === $reference) |
|
88
|
2 |
|
) { |
|
89
|
2 |
|
$removedList->add(clone $tag); |
|
90
|
2 |
|
$this->remove($index); |
|
91
|
2 |
|
} |
|
92
|
2 |
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
return $removedList; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
11 |
|
public function removeByName($tagName) |
|
101
|
|
|
{ |
|
102
|
11 |
|
$removedList = new self(); |
|
103
|
11 |
|
$list = $this->getIterator(); |
|
104
|
11 |
|
foreach ($list as $index => $tag) { |
|
105
|
5 |
|
$name = $tag->getName(); |
|
106
|
5 |
|
if ((is_array($tagName) && in_array($name, $tagName)) || |
|
107
|
5 |
|
($name === $tagName) |
|
108
|
5 |
|
) { |
|
109
|
2 |
|
$this->remove($index); |
|
110
|
2 |
|
$removedList->add($tag); |
|
111
|
2 |
|
} |
|
112
|
11 |
|
} |
|
113
|
|
|
|
|
114
|
11 |
|
return $removedList; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sort the list by elements. |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function sortAsc() |
|
122
|
|
|
{ |
|
123
|
|
|
$cmp = function (TagInterface $a, TagInterface $b) { |
|
124
|
1 |
|
if ($a->getName() == $b->getName()) { |
|
125
|
1 |
|
return 0; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
return ($a->getName() < $b->getName()) ? -1 : 1; |
|
129
|
1 |
|
}; |
|
130
|
1 |
|
usort($this->elements, $cmp); |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Sort the list by elements. |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
|
|
public function sortDesc() |
|
138
|
|
|
{ |
|
139
|
1 |
|
$cmp = function (TagInterface $a, TagInterface $b) { |
|
140
|
1 |
|
if ($a->getName() == $b->getName()) { |
|
141
|
1 |
|
return 0; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
return ($a->getName() > $b->getName()) ? -1 : 1; |
|
145
|
1 |
|
}; |
|
146
|
1 |
|
usort($this->elements, $cmp); |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
21 |
|
public function toString() |
|
150
|
|
|
{ |
|
151
|
21 |
|
if ($this->count() < 1) { |
|
152
|
12 |
|
return ''; |
|
153
|
|
|
} |
|
154
|
11 |
|
$tagIterator = $this->getIterator(); |
|
155
|
11 |
|
$string = ''; |
|
156
|
11 |
|
foreach ($tagIterator as $tag) { |
|
157
|
11 |
|
$string .= ' * ' . $tag->toString(); |
|
158
|
11 |
|
} |
|
159
|
|
|
|
|
160
|
11 |
|
return $string; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @inheritdoc |
|
165
|
|
|
*/ |
|
166
|
3 |
|
public function getByName($tagName) |
|
167
|
|
|
{ |
|
168
|
3 |
|
$found = new self; |
|
169
|
3 |
|
$list = $this->getIterator(); |
|
170
|
|
|
|
|
171
|
3 |
|
foreach ($list as $index => $tag) { |
|
172
|
3 |
|
$name = $tag->getName(); |
|
173
|
3 |
|
if (is_array($tagName) && in_array($name, $tagName)) { |
|
174
|
2 |
|
$found->add($this->get($index)); |
|
175
|
3 |
|
} elseif ($name === $tagName) { |
|
176
|
3 |
|
$found->add($this->get($index)); |
|
177
|
3 |
|
} |
|
178
|
3 |
|
} |
|
179
|
|
|
|
|
180
|
3 |
|
return $found; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|