-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFqcn.php
342 lines (265 loc) · 9.17 KB
/
Fqcn.php
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <[email protected]>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// fqcn
// class with static methods to deal with fully qualified class name strings
final class Fqcn extends Set
{
// config
protected static array $config = [
'option'=>[ // tableau d'options
'extension'=>'php'], // extension du path
'separator'=>['\\'], // séparateur pour les fully qualified classname
'plusSeperator'=>'+', // séparateur pour la méthode many
'sensitive'=>false // la classe est sensible ou non à la case
];
// is
// retourne vrai si la valeur est un fqcn
final public static function is($value):bool
{
return Validate::regex('fqcn',$value);
}
// sameName
// retourne vrai si les deux valeurs ont le même nom
// la comparaison est insensible à la case
final public static function sameName($same,$value):bool
{
$same = (string) self::name($same);
$value = (string) self::name($value);
return $same === $value || strtolower($same) === strtolower($value);
}
// sameNamespace
// retourne vrai si les deux valeurs ont le même namespace
// la comparaison est insensible à la case
final public static function sameNamespace($same,$value):bool
{
$same = (string) self::namespace($same);
$value = (string) self::namespace($value);
return $same === $value || strtolower($same) === strtolower($value);
}
// hasNamespace
// retourne vrai si la valeur a exactement le namespace spécifié
// la comparaison est insensible à la case
final public static function hasNamespace($namespace,$value):bool
{
$return = false;
$value = self::str($value);
if(!empty($value))
{
$namespace = self::str($namespace);
$value = (string) self::namespace($value);
$return = ($namespace === $value || strtolower($namespace) === strtolower($value));
}
return $return;
}
// inNamespace
// retourne vrai si la valeur fait partie du namespace spécifié
// la comparaison est insensible à la case
final public static function inNamespace($namespace,$value):bool
{
$namespace = self::str($namespace);
$value = self::str($value);
return !empty($namespace) && !empty($value) && stripos($value,$namespace) === 0;
}
// str
// retourne le fully qualified classname
// value peut être une string, array ou objet
final public static function str($value,?array $option=null):string
{
$return = '';
if(is_object($value))
$return = $value::class;
elseif(is_string($value) && (strpos($value,'\\') > 0 || class_exists($value,false)))
$return = $value;
else
$return = parent::str($value,$option);
return $return;
}
// arr
// retourne le nom fully qualified classname sous la forme d'un array
// étend la méthode de la classe set
final public static function arr($value,?array $option=null):array
{
$return = [];
if(is_object($value))
$value = $value::class;
elseif(is_string($value) && (strpos($value,'\\') > 0 || class_exists($value,false)))
$return = explode('\\',$value);
else
$return = parent::arr($value,$option);
return $return;
}
// root
// retourne le root du namespace d'un fqcn
// si la classe n'a pas de namespace, retoure null
final public static function root($value):?string
{
$return = null;
$value = self::str($value);
if(is_string($value) && strpos($value,'\\') !== false)
{
$array = explode('\\',$value);
$root = array_shift($array);
if(is_string($root) && strlen($root))
$return = $root;
}
return $return;
}
// name
// retourne le nom d'un fqcn
final public static function name($value):?string
{
$return = null;
$value = self::str($value);
if(is_string($value))
{
if(strpos($value,'\\') !== false)
{
$array = explode('\\',$value);
$name = array_pop($array);
if(is_string($name) && strlen($name))
$return = $name;
}
elseif(strlen($value))
$return = $value;
}
return $return;
}
// namespace
// retourne le namespace, sans le nom de classe, d'un fqcn
final public static function namespace($value):?string
{
$return = null;
$value = self::str($value);
if(is_string($value) && strpos($value,'\\') !== false && strpos($value,'@') === false)
{
$array = explode('\\',$value);
array_pop($array);
if(!empty($array))
$return = implode('\\',$array);
}
return $return;
}
// spliceRoot
// retourne le fqcn sans le root
final public static function spliceRoot($value,$replace=null):string
{
return self::spliceFirst(self::str($value),$replace);
}
// sliceMiddle
// retoure un fqcn sans le root et le nom de classe
final public static function sliceMiddle($value):string
{
return self::unsets([0,-1],self::str($value));
}
// many
// prépare une string classe, qui pourrait contenir plusieurs classes ou namespace séparés par +
// retourne un tableau
final public static function many($value):array
{
$return = [];
$plusSeperator = self::$config['plusSeperator'];
if(is_array($value))
$value = self::str($value);
if(is_string($value) && strlen($value))
{
$explodes = Str::explodes(['\\',$plusSeperator],$value);
if(!empty($explodes))
{
foreach (Arrs::valuesCrush($explodes) as $v)
{
if(is_array($v) && !empty($v))
$return[] = self::str($v);
}
}
}
return $return;
}
// path
// transforme un fully qualified classname en path
// utiliser option path pour contrôler le output du path
final public static function path($value,?array $option=null):?string
{
$return = null;
$option = self::option($option);
$array = self::arr($value,$option);
if(!empty($array))
{
$option['path'] = !empty($option['path'])? Path::option($option['path']):null;
$return = Path::implode($array,$option['path']);
if(!empty($option['extension']))
$return = self::extension($return,$option['extension']);
}
return $return;
}
// fromPath
// transforme un chemin en fully qualified classname
// utiliser option path pour contrôler le input du path
final public static function fromPath(string $value,?array $option=null):?string
{
$return = null;
$option = self::option($option);
$value = Path::removeExtension($value);
if(!empty($value))
{
$option['path'] = !empty($option['path'])? Path::option($option['path']):null;
$array = Path::arr($value,$option['path']);
if(!empty($array))
$return = self::str($array,$option);
}
return $return;
}
// fromPathRoot
// retourne le fqcn à partir d'un path et d'une string root
// option root permet de mettre une valeur en prepend du fqcn de retour
final public static function fromPathRoot(string $value,string $root,?array $option=null):?string
{
$return = null;
if(Str::isStart($root,$value,false))
{
$value = Str::stripStart($root,$value,false);
if(!empty($value))
{
if(!empty($option['root']))
$value = Path::append($option['root'],$value);
$return = self::fromPath($value,$option);
}
}
return $return;
}
// fromPathRoots
// retourne le fqcn à partir d'un path et un tableau root
// si les clés de roots sont string non numérique, append au path
final public static function fromPathRoots(string $value,array $roots,?array $option=null):?string
{
$return = null;
foreach ($roots as $k => $root)
{
if(is_string($root))
{
$v = self::fromPathRoot($value,$root,(is_string($k))? Arr::plus($option,['root'=>$k]):$option);
if(!empty($v))
{
$return = $v;
break;
}
}
}
return $return;
}
// extension
// change l'extension du chemin
// utilise relative, ne force pas l'ajout d'un forward slash au path
final public static function extension(string $value,$extension=null):string
{
return PathTrack::changeExtension($extension ?? self::getOption('extension'),$value);
}
}
// init
Fqcn::__init();
?>