|
2 | 2 |
|
3 | 3 | namespace Microwin7\PHPUtils; |
4 | 4 |
|
5 | | -if (!function_exists('str_starts_with_slash')) { |
6 | | - function str_starts_with_slash(string $string, bool $needle_starts_with_slash = FALSE): string |
7 | | - { |
8 | | - if (str_starts_with($string, DIRECTORY_SEPARATOR)) { |
9 | | - if (!$needle_starts_with_slash) $string = substr($string, 1); |
10 | | - } else { |
11 | | - if ($needle_starts_with_slash) $string = DIRECTORY_SEPARATOR . $string; |
12 | | - } |
13 | | - return $string; |
| 5 | +function str_starts_with_slash(string $string, bool $needle_starts_with_slash = FALSE): string |
| 6 | +{ |
| 7 | + if (str_starts_with($string, DIRECTORY_SEPARATOR)) { |
| 8 | + if (!$needle_starts_with_slash) $string = substr($string, 1); |
| 9 | + } else { |
| 10 | + if ($needle_starts_with_slash) $string = DIRECTORY_SEPARATOR . $string; |
14 | 11 | } |
| 12 | + return $string; |
15 | 13 | } |
16 | | -if (!function_exists('str_ends_with_slash')) { |
17 | | - function str_ends_with_slash(string $string, bool $needle_ends_with_slash = TRUE): string |
18 | | - { |
19 | | - if (str_ends_with($string, DIRECTORY_SEPARATOR)) { |
20 | | - if (!$needle_ends_with_slash) $string = substr($string, 0, -1); |
21 | | - } else { |
22 | | - if ($needle_ends_with_slash) $string .= DIRECTORY_SEPARATOR; |
23 | | - } |
24 | | - return $string; |
| 14 | +function str_ends_with_slash(string $string, bool $needle_ends_with_slash = TRUE): string |
| 15 | +{ |
| 16 | + if (str_ends_with($string, DIRECTORY_SEPARATOR)) { |
| 17 | + if (!$needle_ends_with_slash) $string = substr($string, 0, -1); |
| 18 | + } else { |
| 19 | + if ($needle_ends_with_slash) $string .= DIRECTORY_SEPARATOR; |
25 | 20 | } |
| 21 | + return $string; |
26 | 22 | } |
27 | | -if (!function_exists('ar_slash_string')) { |
28 | | - function ar_slash_string(string $string, bool $needle_starts_with_slash = FALSE, bool $needle_ends_with_slash = TRUE): string |
29 | | - { |
30 | | - return str_ends_with_slash(str_starts_with_slash($string, $needle_starts_with_slash), $needle_ends_with_slash); |
31 | | - } |
| 23 | +function ar_slash_string(string $string, bool $needle_starts_with_slash = FALSE, bool $needle_ends_with_slash = TRUE): string |
| 24 | +{ |
| 25 | + return str_ends_with_slash(str_starts_with_slash($string, $needle_starts_with_slash), $needle_ends_with_slash); |
32 | 26 | } |
33 | | -if (!function_exists('implodeRecursive')) { |
34 | | - /** |
35 | | - * @param string $separator |
36 | | - * @param string[]|list<string|list<string>> $array |
37 | | - * @return string |
38 | | - */ |
39 | | - function implodeRecursive(string $separator, array $array): string |
40 | | - { |
41 | | - $result = ''; |
42 | | - foreach ($array as $value) { |
43 | | - if (is_array($value)) { |
44 | | - $result .= implodeRecursive($separator, $value) . $separator; |
45 | | - } else { |
46 | | - if (strrpos($value, '\\') === false) { |
47 | | - $result .= $value . $separator; |
48 | | - } else if (enum_exists($value)) { |
49 | | - $argumentClazz = new \ReflectionClass($value); |
50 | | - if ($argumentClazz->implementsInterface(\Microwin7\PHPUtils\Contracts\Enum\EnumRequestInterface::class)) { |
51 | | - /** |
52 | | - * @var interface-string< |
53 | | - * \BackedEnum & |
54 | | - * \Microwin7\PHPUtils\Contracts\Enum\EnumInterface & |
55 | | - * \Microwin7\PHPUtils\Contracts\Enum\EnumRequestInterface |
56 | | - * > $enumClass |
57 | | - */ |
58 | | - $enumClass = $value; |
59 | | - $result .= $enumClass::getNameRequestVariable() . $separator; |
60 | | - } |
| 27 | +/** |
| 28 | + * @param string $separator |
| 29 | + * @param string[]|list<string|list<string>> $array |
| 30 | + * @return string |
| 31 | + */ |
| 32 | +function implodeRecursive(string $separator, array $array): string |
| 33 | +{ |
| 34 | + $result = ''; |
| 35 | + foreach ($array as $value) { |
| 36 | + if (is_array($value)) { |
| 37 | + $result .= implodeRecursive($separator, $value) . $separator; |
| 38 | + } else { |
| 39 | + if (strrpos($value, '\\') === false) { |
| 40 | + $result .= $value . $separator; |
| 41 | + } else if (enum_exists($value)) { |
| 42 | + $argumentClazz = new \ReflectionClass($value); |
| 43 | + if ($argumentClazz->implementsInterface(\Microwin7\PHPUtils\Contracts\Enum\EnumRequestInterface::class)) { |
| 44 | + /** |
| 45 | + * @var interface-string< |
| 46 | + * \BackedEnum & |
| 47 | + * \Microwin7\PHPUtils\Contracts\Enum\EnumInterface & |
| 48 | + * \Microwin7\PHPUtils\Contracts\Enum\EnumRequestInterface |
| 49 | + * > $enumClass |
| 50 | + */ |
| 51 | + $enumClass = $value; |
| 52 | + $result .= $enumClass::getNameRequestVariable() . $separator; |
61 | 53 | } |
62 | 54 | } |
63 | 55 | } |
64 | | - |
65 | | - return rtrim($result, $separator); |
66 | 56 | } |
| 57 | + return rtrim($result, $separator); |
67 | 58 | } |
68 | | -if (!function_exists('getClassMethodsAllFromDocComment')) { |
69 | | - /** |
70 | | - * @param class-string|object|trait-string $class |
71 | | - * |
72 | | - * @return string[]|null |
73 | | - */ |
74 | | - function getClassMethodsAllFromDocComment(string|object $class): ?array |
75 | | - { |
76 | | - $reflectionClass = new \ReflectionClass($class); |
77 | | - $docComment = $reflectionClass->getDocComment(); |
78 | | - if ($docComment !== false) { |
79 | | - preg_match_all('/@method\s+([^\r\n\t\f\v(]+)/', $docComment, $matches); |
80 | | - if (isset($matches[1]) && !empty($matches[1])) { |
81 | | - return $matches[1]; |
82 | | - } |
| 59 | +/** |
| 60 | + * @param class-string|object|trait-string $class |
| 61 | + * |
| 62 | + * @return string[]|null |
| 63 | + */ |
| 64 | +function getClassMethodsAllFromDocComment(string|object $class): ?array |
| 65 | +{ |
| 66 | + $reflectionClass = new \ReflectionClass($class); |
| 67 | + $docComment = $reflectionClass->getDocComment(); |
| 68 | + if ($docComment !== false) { |
| 69 | + preg_match_all('/@method\s+([^\r\n\t\f\v(]+)/', $docComment, $matches); |
| 70 | + if (isset($matches[1]) && !empty($matches[1])) { |
| 71 | + return $matches[1]; |
83 | 72 | } |
84 | | - return null; |
85 | 73 | } |
| 74 | + return null; |
86 | 75 | } |
87 | | -if (!function_exists('getClassMethodsFromAnnotations')) { |
88 | | - /** |
89 | | - * @param class-string|object|trait-string $class |
90 | | - * |
91 | | - * @return string[] |
92 | | - */ |
93 | | - function getClassMethodsFromAnnotations(string|object $class): array |
94 | | - { |
95 | | - $annotations = []; |
96 | | - $methodsDocComment = getClassMethodsAllFromDocComment($class); |
97 | | - if ($methodsDocComment !== null) { |
98 | | - foreach ($methodsDocComment as $v) { |
99 | | - $methodComment = explode(" ", $v); |
100 | | - if (count($methodComment) === 2) { |
101 | | - $annotations[] = $methodComment[1]; |
102 | | - } |
| 76 | +/** |
| 77 | + * @param class-string|object|trait-string $class |
| 78 | + * |
| 79 | + * @return string[] |
| 80 | + */ |
| 81 | +function getClassMethodsFromAnnotations(string|object $class): array |
| 82 | +{ |
| 83 | + $annotations = []; |
| 84 | + $methodsDocComment = getClassMethodsAllFromDocComment($class); |
| 85 | + if ($methodsDocComment !== null) { |
| 86 | + foreach ($methodsDocComment as $v) { |
| 87 | + $methodComment = explode(" ", $v); |
| 88 | + if (count($methodComment) === 2) { |
| 89 | + $annotations[] = $methodComment[1]; |
103 | 90 | } |
104 | 91 | } |
105 | | - return $annotations; |
106 | 92 | } |
| 93 | + return $annotations; |
107 | 94 | } |
108 | | -if (!function_exists('getClassStaticMethodsFromAnnotations')) { |
109 | | - /** |
110 | | - * @param class-string|object|trait-string $class |
111 | | - * |
112 | | - * @return list<object{'name': string, 'type': string}> |
113 | | - */ |
114 | | - function getClassStaticMethodsFromAnnotations(string|object $class): array |
115 | | - { |
116 | | - $annotations = []; |
117 | | - $methodsDocComment = getClassMethodsAllFromDocComment($class); |
118 | | - if ($methodsDocComment !== null) { |
119 | | - foreach ($methodsDocComment as $v) { |
120 | | - $methodComment = explode(" ", $v); |
121 | | - if (count($methodComment) === 3 && $methodComment[0] === 'static') { |
122 | | - $annotations[] = (object) ['name' => $methodComment[2], 'type' => $methodComment[1]]; |
123 | | - } |
| 95 | +/** |
| 96 | + * @param class-string|object|trait-string $class |
| 97 | + * |
| 98 | + * @return list<object{'name': string, 'type': string}> |
| 99 | + */ |
| 100 | +function getClassStaticMethodsFromAnnotations(string|object $class): array |
| 101 | +{ |
| 102 | + $annotations = []; |
| 103 | + $methodsDocComment = getClassMethodsAllFromDocComment($class); |
| 104 | + if ($methodsDocComment !== null) { |
| 105 | + foreach ($methodsDocComment as $v) { |
| 106 | + $methodComment = explode(" ", $v); |
| 107 | + if (count($methodComment) === 3 && $methodComment[0] === 'static') { |
| 108 | + $annotations[] = (object) ['name' => $methodComment[2], 'type' => $methodComment[1]]; |
124 | 109 | } |
125 | 110 | } |
126 | | - return $annotations; |
127 | 111 | } |
| 112 | + return $annotations; |
128 | 113 | } |
129 | | -if (!function_exists('minifier')) { |
130 | | - function minifier(string $code): string |
131 | | - { |
132 | | - $search = array( |
133 | | - // Remove whitespaces after tags |
134 | | - '/\>[^\S]+/s', |
135 | | - // Remove whitespaces before tags |
136 | | - '/[^\S]+\</s', |
137 | | - // Remove multiple whitespace sequences |
138 | | - '/(\s)+/s', |
139 | | - // Removes comments |
140 | | - '/<!--(.|\s)*?-->/' |
141 | | - ); |
142 | | - $replace = array('>', '<', '\\1'); |
143 | | - $code = preg_replace($search, $replace, $code); |
144 | | - return $code; |
145 | | - } |
| 114 | +function minifier(string $code): string |
| 115 | +{ |
| 116 | + $search = array( |
| 117 | + // Remove whitespaces after tags |
| 118 | + '/\>[^\S]+/s', |
| 119 | + // Remove whitespaces before tags |
| 120 | + '/[^\S]+\</s', |
| 121 | + // Remove multiple whitespace sequences |
| 122 | + '/(\s)+/s', |
| 123 | + // Removes comments |
| 124 | + '/<!--(.|\s)*?-->/' |
| 125 | + ); |
| 126 | + $replace = array('>', '<', '\\1'); |
| 127 | + $code = preg_replace($search, $replace, $code); |
| 128 | + return $code; |
146 | 129 | } |
147 | | -if (!function_exists('convertToBytes')) { |
148 | | - function convertToBytes(string $size): int |
149 | | - { |
150 | | - $unit = strtoupper(substr($size, -1)); // Получаем последний символ (единицу измерения) |
151 | | - /** @var int */ |
152 | | - $number = substr($size, 0, -1); // Получаем число (без последнего символа) |
153 | | - return match ($unit) { |
154 | | - 'K' => $number * 1024, |
155 | | - 'M' => $number * 1024 * 1024, |
156 | | - 'G' => $number * 1024 * 1024 * 1024, |
157 | | - default => (int) $size |
158 | | - }; |
159 | | - } |
| 130 | +function convertToBytes(string $size): int |
| 131 | +{ |
| 132 | + $unit = strtoupper(substr($size, -1)); // Получаем последний символ (единицу измерения) |
| 133 | + /** @var int */ |
| 134 | + $number = substr($size, 0, -1); // Получаем число (без последнего символа) |
| 135 | + return match ($unit) { |
| 136 | + 'K' => $number * 1024, |
| 137 | + 'M' => $number * 1024 * 1024, |
| 138 | + 'G' => $number * 1024 * 1024 * 1024, |
| 139 | + default => (int) $size |
| 140 | + }; |
160 | 141 | } |
0 commit comments