-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompose.php
More file actions
94 lines (81 loc) · 2.5 KB
/
Compose.php
File metadata and controls
94 lines (81 loc) · 2.5 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
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
<?php
namespace Core;
/**
* Compose different values automatically.
*/
class Compose
{
/**
* Create unique string.
*
* @param integer $length Length of the string (default 8)
* @param mixed $chars Characters to use, defaults to a-z, A-Z and 0-9
* @return string Unique string
*/
public static function unique($length = 8, $chars = false)
{
if (!is_string($chars)) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
}
$char_count = strlen($chars) - 1;
$key = '';
for ($i = 0; $i < $length; $i++) {
$key .= $chars[mt_rand(0, $char_count)];
}
return $key;
}
/**
* Generate slug from a string.
*
* @param string $value Original string value
* @return mixed Slug string or null if unable to generate.
*/
public static function slug($value)
{
$encoding = mb_detect_encoding($value);
$slug = iconv($encoding, 'ASCII//TRANSLIT', $value);
$slug = preg_replace('/[^a-z0-9-_]/i', '-', $slug);
$slug = trim($slug, '-');
$slug = strtolower($slug);
if (empty($slug)) {
return null;
}
return $slug;}
/**
* Bytes to human readable form.
*/
public static function bytesToHuman($bytes, $decimals = 2, $divider = 1024)
{
$postfixes = array(
1000 => array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB'),
1024 => array('B', 'kiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'),
);
if (!isset($postfixes[$divider])) {
return $bytes;
}
$bytes = floatval($bytes);
$i = 0;
for (; $i < 7; $i++) {
if (strlen(number_format($bytes)) <= 3) {
break;
}
$bytes /= $divider;
}
return number_format($bytes, $i == 0 ? 0 : $decimals) . ' ' . $postfixes[$divider][$i];
}
/**
* Create random (version 4) UUID.
*
* @return string UUID
*/
public static function UUIDv4()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
}