-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.php
101 lines (83 loc) · 3.27 KB
/
Utils.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
<?php
declare(strict_types=1);
namespace ApiClients\Tools\OpenApiClientGenerator;
use Jawira\CaseConverter\Convert;
use function array_map;
use function basename;
use function count;
use function dirname;
use function explode;
use function implode;
use function in_array;
use function str_replace;
use function strtolower;
use function trim;
final class Utils
{
public static function cleanUpString(string $string): string
{
return str_replace(
['{', '}', '-', '$', '+', '*', '.', ';', '=', ' ', '/', '\\', ';', ' '],
['', '', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'],
self::fixKeyword(
$string,
),
);
}
public static function className(string $className): string
{
$className = str_replace(
['{', '}', '-', '$', '+', '*', '.', ';', '=', ' '],
['', '', '_', '_', '_', '_', '_', '_', '_', '_'],
$className,
);
$className = implode(
'\\',
array_map(
static fn (string $chunk): string => self::fixKeyword(
(new Convert($chunk))->toPascal(),
),
explode(
'\\',
$className,
),
),
);
return trim(self::cleanUpNamespace(self::fixKeyword($className)), '\\');
}
public static function cleanUpNamespace(string $namespace): string
{
do {
$previousNamespace = $namespace;
$namespace = str_replace('/', '\\', $namespace);
$namespace = str_replace('\\\\', '\\', $namespace);
} while ($previousNamespace !== $namespace);
$namespace = trim($namespace, '\\');
return '\\' . $namespace;
}
public static function fqcn(string $fqcn): string
{
return str_replace('/', '\\', $fqcn);
}
public static function dirname(string $fqcn): string
{
$fqcn = str_replace('\\', '/', $fqcn);
return trim(self::cleanUpNamespace(dirname($fqcn)), '\\');
}
public static function basename(string $fqcn): string
{
$fqcn = str_replace('\\', '/', $fqcn);
return trim(self::cleanUpNamespace(basename($fqcn)), '\\');
}
public static function fixKeyword(string $name): string
{
$name = self::fqcn($name);
$nameBoom = explode('\\', $name);
/** @phpstan-ignore-next-line */
return $name . (in_array(
strtolower($nameBoom[count($nameBoom) - 1]),
['__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor', 'self', 'parent', 'object'],
false,
) ? '_' : '');
}
}