Skip to content

Commit d7481a6

Browse files
committed
string util, uuid and static functions
1 parent 0575cae commit d7481a6

File tree

1 file changed

+58
-44
lines changed

1 file changed

+58
-44
lines changed

src/Util/StringUtil.php

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,65 @@
2525
*
2626
* @package doganoo\PHPUtil\Util
2727
*/
28-
final class StringUtil{
29-
/**
30-
* prevent from instantiation
31-
* StringUtil constructor.
32-
*/
33-
private function __construct(){
34-
}
28+
final class StringUtil {
29+
/**
30+
* prevent from instantiation
31+
* StringUtil constructor.
32+
*/
33+
private function __construct() {
34+
}
3535

36-
/**
37-
* returns an array of elements of the string
38-
*
39-
* @param null|string $string
40-
*
41-
* @return array
42-
*/
43-
public static function stringToArray(?string $string): array{
44-
$result = [];
45-
$strLen = \strlen($string);
46-
if(null === $string) return $result;
47-
if(1 === $strLen){
48-
$result[] = $string;
49-
return $result;
50-
}
51-
for($i = 0; $i < $strLen; $i ++){
52-
$result[] = $string[$i];
53-
}
54-
return $result;
55-
}
36+
/**
37+
* returns an array of elements of the string
38+
*
39+
* @param null|string $string
40+
*
41+
* @return array
42+
*/
43+
public static function stringToArray(?string $string): array {
44+
$result = [];
45+
$strLen = \strlen($string);
46+
if (null === $string) return $result;
47+
if (1 === $strLen) {
48+
$result[] = $string;
49+
return $result;
50+
}
51+
for ($i = 0; $i < $strLen; $i++) {
52+
$result[] = $string[$i];
53+
}
54+
return $result;
55+
}
5656

57-
/**
58-
* @param string $string
59-
*
60-
* @return string
61-
*/
62-
public function toUTF8(string $string): string{
63-
$string = iconv('ASCII', 'UTF-8//IGNORE', $string);
64-
return $string;
65-
}
57+
/**
58+
* returns an UUID.
59+
* See here: http://www.seanbehan.com/how-to-generate-a-uuid-in-php/
60+
*
61+
* @return string
62+
* @throws \Exception
63+
*/
64+
public static function getUUID(): string {
65+
$data = random_bytes(16);
66+
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
67+
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
68+
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
69+
}
6670

67-
/**
68-
* @param string $string
69-
*
70-
* @return string
71-
*/
72-
public function getEncoding(string $string): string{
73-
return \mb_detect_encoding($string, "auto", true);
74-
}
71+
/**
72+
* @param string $string
73+
*
74+
* @return string
75+
*/
76+
public static function toUTF8(string $string): string {
77+
$string = iconv('ASCII', 'UTF-8//IGNORE', $string);
78+
return $string;
79+
}
80+
81+
/**
82+
* @param string $string
83+
*
84+
* @return string
85+
*/
86+
public static function getEncoding(string $string): string {
87+
return \mb_detect_encoding($string, "auto", true);
88+
}
7589
}

0 commit comments

Comments
 (0)