1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
<?php /** * PHP_UML (PHP/MOF program elements classes) * * PHP version 5 * * @category PHP * @package PHP_UML * @subpackage Metamodel * @author Baptiste Autin <ohlesbeauxjours@yahoo.fr> * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3 * @version SVN: $Revision: 175 $ * @link http://pear.php.net/package/PHP_UML * @link http://www.omg.org/mof/ * @since $Date: 2011-09-15 17:07:58 +0200 (jeu., 15 sept. 2011) $ * */
/** * Helper class to deal with Metamodel elements. * * @category PHP * @package PHP_UML * @subpackage Metamodel * @author Baptiste Autin <ohlesbeauxjours@yahoo.fr> * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3 */ class PHP_UML_Metamodel_Helper { const META_INTERFACE = 'PHP_UML_Metamodel_Interface'; const META_CLASS = 'PHP_UML_Metamodel_Class'; const META_DATATYPE = 'PHP_UML_Metamodel_Datatype'; const META_OPERATION = 'PHP_UML_Metamodel_Operation'; const META_PROPERTY = 'PHP_UML_Metamodel_Property';
const PHP_PROFILE_NAME = 'php'; const PHP_STEREOTYPE_DOCBLOCK = 'docblock';
/** * Deletes all empty packages * * @param PHP_UML_Metamodel_Package &$ns Top package */ static public function deleteEmptyPackages(PHP_UML_Metamodel_Package &$ns) { if (!is_null($ns->nestedPackage)) { foreach ($ns->nestedPackage as $key => &$pkg) { if (self::isEmpty($pkg)) { unset($ns->nestedPackage[$key]); } else { if (self::deleteEmptyPackages($pkg)) { unset($ns->nestedPackage[$key]); } } } if (self::isEmpty($ns)) { return true; } } return false; }
/** * Checks if a package is empty * * @param PHP_UML_Metamodel_Package $p Top package */ static private function isEmpty(PHP_UML_Metamodel_Package $p) { return empty($p->nestedPackage) && empty($p->ownedType) && empty($p->ownedOperation) && empty($p->ownedAttribute); } /** * Searches in a given package for a typed element (likely, a class) * * @param PHP_UML_Metamodel_Package $ns A package element * @param string $value A name * * @return mixed Either FALSE if not found, or the element */ public static function searchTypeIntoPackage(PHP_UML_Metamodel_Package $ns, $value) { foreach ($ns->ownedType as $key => &$o) { if (strcasecmp($o->name, $value)==0) { return $o; } } return false; }
/** * Searches if an operation already exists in a given pkg * * @param PHP_UML_Metamodel_Package $ns A package element * @param string $value A name * * @return mixed Either FALSE if not found, or the element */ static public function searchOperationIntoPackage(PHP_UML_Metamodel_Package $ns, $value) { foreach ($ns->ownedOperation as $key => &$o) { if (strcasecmp($o->name, $value)==0) { return $o; } } return false; }
/** * Searches if an attribute already exists in a given pkg * * @param PHP_UML_Metamodel_Package $ns A package element * @param string $value A name * * @return mixed Either FALSE if not found, or the element */ public static function searchAttributeIntoPackage(PHP_UML_Metamodel_Package $ns, $value) { foreach ($ns->ownedAttribute as $key => &$o) { if (strcasecmp($o->name, $value)==0) { return $o; } } return false; } /** * Retrieves a particular tag in a given stereotype * * @param PHP_UML_Metamodel_Stereotype $s The stereotype * @param string $tagName The tag name (eg "description") * * @return PHP_UML_Metamodel_Tag */ public static function getStereotypeTag(PHP_UML_Metamodel_Stereotype $s, $tagName) { if (!empty($s)) { foreach ($s->ownedAttribute as $tag) { if ($tag->name == $tagName) return $tag; } } return null; } /** * Searches recursively in a given package for an element, knowing its ID * * @param PHP_UML_Metamodel_Package $np A package element (context) * @param string $value An ID * * @return mixed Either FALSE if not found, or the position in the stack */ public static function findTypeById(PHP_UML_Metamodel_Package $np, $value) { if ($np->id == $value) return $np; foreach ($np->ownedType as $item) { if ($item->id == $value) return $item; } foreach ($np->ownedAttribute as $item) { if ($item->id == $value) return $item; } foreach ($np->ownedOperation as $item) { if ($item->id == $value) return $item; } foreach ($np->nestedPackage as $item) { $ret = self::findTypeById($item, $value); if (!($ret === false)) return $ret; } return false; }
/** * Searches recursively in a given package for a package, knowing its name * Works with position numbers, not variable references. * * @param PHP_UML_Metamodel_Package $np A package element (context) * @param string $value A package name (to find) * * @return mixed Either FALSE if not found, or the position in the stack */ public static function findSubpackageByName(PHP_UML_Metamodel_Package $np, $value) { foreach ($np->nestedPackage as $pkg) { if (strcasecmp($pkg->name, $value)==0) { return $pkg; } } return false; } /** * Splits a package path into its first/last element, and the rest * Allows for the two different versions of package delimiter * * @param string $path A path to split * @param bool $modeFirst If true, splits into 1st and the rest * If false, splits into last and the rest * @param string $alt Alternate separator (eg, /, or ::) * * @return array Results array */ static public function getPackagePathParts($path, $modeFirst = true, $alt=PHP_UML_Input_PHP_Parser::T_NS_SEPARATOR2) { $first = ''; $last = ''; if ($modeFirst) { $pos1 = strpos($path, PHP_UML_Input_PHP_Parser::T_NS_SEPARATOR); $pos2 = strpos($path, $alt); if ($pos1!==false && ($pos1<$pos2 || $pos2===false)) { $pos = $pos1; $len = strlen(PHP_UML_Input_PHP_Parser::T_NS_SEPARATOR); } else { $pos = $pos2; $len = strlen($alt); } } else { $pos1 = strrpos($path, PHP_UML_Input_PHP_Parser::T_NS_SEPARATOR); $pos2 = strrpos($path, $alt); if ($pos1!==false && ($pos1>$pos2 || $pos2===false)) { $pos = $pos1; $len = strlen(PHP_UML_Input_PHP_Parser::T_NS_SEPARATOR); } else { $pos = $pos2; $len = strlen($alt); } } if ($pos===false) $first = $path; else { $first = substr($path, 0, $pos); $last = substr($path, $pos+$len); } return array($pos, $first, $last); } } ?>
|