-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathText.php
More file actions
47 lines (41 loc) · 1.25 KB
/
Text.php
File metadata and controls
47 lines (41 loc) · 1.25 KB
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
<?php
namespace Matscode\Paystack\Utility;
class Text
{
/**
*
* @param int $length
*
* @param int $capsMix
*
* @return string
* @throws \Exception
* @author Hackan <hackan@gmail.com>
* @link https://php.net/manual/en/function.uniqid.php#120123
*
*/
public static function uniqueStr(int $length = 11, int $capsMix = 5): string
{
// uniqid gives 15 chars, but you could adjust it to your needs.
if (function_exists("random_bytes")) {
$bytes = random_bytes(ceil($length / 2));
} elseif (function_exists("openssl_random_pseudo_bytes")) {
$bytes = openssl_random_pseudo_bytes(ceil($length / 2));
} else {
throw new \Exception("No cryptographically secure random function available");
}
if ($capsMix > 10) {
throw new \Exception('capsMix can not be greater than 10');
}
$caps = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 1, $capsMix);
return str_shuffle(substr(bin2hex($bytes), 0, $length) . $caps);
}
/**
* @param string $string
* @return string
*/
public static function removeSlashes(string $string): string
{
return trim($string, '/');
}
}