|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rico\Lib; |
|
6
|
|
|
|
|
7
|
|
|
use Rico\Slib\StringUtils as StaticStringUtils; |
|
8
|
|
|
|
|
9
|
|
|
class StringUtils |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Converts an alphabetic $string into an identifier (an integer). |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $string |
|
15
|
|
|
* @param string $secret to decode the alphabetic string |
|
16
|
|
|
* |
|
17
|
|
|
* @return int|string |
|
18
|
|
|
*/ |
|
19
|
|
|
public function alphaToId(string $string, string $secret = '') |
|
20
|
|
|
{ |
|
21
|
|
|
return StaticStringUtils::alphaToId($string, $secret); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Transforms an ugly $string (with incorrect ponctuation) into beautiful string (with correct ponctuation). |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $string |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function beautifulise(string $string): string |
|
32
|
|
|
{ |
|
33
|
|
|
return StaticStringUtils::beautifulise($string); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Gets a human readable string of a size in $bytes. |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $bytes |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function humanFilesize(int $bytes): string |
|
44
|
|
|
{ |
|
45
|
|
|
return StaticStringUtils::humanFilesize($bytes); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Converts a $identifier into an alphanumeric string. |
|
50
|
|
|
* |
|
51
|
|
|
* @param int $identifier |
|
52
|
|
|
* @param string $secret to encode the integer |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function idToAlpha(int $identifier, string $secret = ''): string |
|
57
|
|
|
{ |
|
58
|
|
|
return StaticStringUtils::idToAlpha($identifier, $secret); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Removes whitespaces, line breaks and comment out of a $string. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $string |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function minify(string $string): string |
|
69
|
|
|
{ |
|
70
|
|
|
return StaticStringUtils::minify($string); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Cleans a $string by removing multi-spaces, line breaks, indents and HTML tags. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $string |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function normalize(string $string): string |
|
81
|
|
|
{ |
|
82
|
|
|
return StaticStringUtils::normalize($string); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Replaces all sort of spaces (tab, nil, non-breaking…) in a $string by a simple space. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $string |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function normalizeWhitespace(string $string): string |
|
93
|
|
|
{ |
|
94
|
|
|
return StaticStringUtils::normalizeWhitespace($string); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Generates a random string of $length $allowedChars. |
|
99
|
|
|
* |
|
100
|
|
|
* @param int $length |
|
101
|
|
|
* @param string $allowedChars |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function randString(int $length = 10, string $allowedChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'): string |
|
106
|
|
|
{ |
|
107
|
|
|
return StaticStringUtils::randString($length, $allowedChars); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Removes brackets and its content from a $string. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $string |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function removeBracketContent(string $string): string |
|
118
|
|
|
{ |
|
119
|
|
|
return StaticStringUtils::removeBracketContent($string); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Removes all sort of line breaks inside a $string. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $string |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function removeLine(string $string): string |
|
130
|
|
|
{ |
|
131
|
|
|
return StaticStringUtils::removeLine($string); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Removes all sort of spaces from a $string. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $string |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
public function removeWhitespace(string $string): string |
|
142
|
|
|
{ |
|
143
|
|
|
return StaticStringUtils::removeWhitespace($string); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Transforms a $string into a ascii-only string separated by -. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $string |
|
150
|
|
|
* |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public function slugify(string $string): string |
|
154
|
|
|
{ |
|
155
|
|
|
return StaticStringUtils::slugify($string); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Replaces underscore in $string by spaces. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $string |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function underscoreToSpace(string $string): string |
|
166
|
|
|
{ |
|
167
|
|
|
return StaticStringUtils::underscoreToSpace($string); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|