1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by jensk on 17-3-2017. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CloudControl\Cms\util; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class StringUtil |
10
|
|
|
{ |
11
|
|
|
public static $fileTypeIcons = array( |
12
|
|
|
'image' => 'file-image-o', |
13
|
|
|
'pdf' => 'file-pdf-o', |
14
|
|
|
'audio' => 'file-audio-o', |
15
|
|
|
'x-msdownload' => 'windows', |
16
|
|
|
'application/vnd.ms-excel' => 'file-excel-o', |
17
|
|
|
'application/msexcel' => 'file-excel-o', |
18
|
|
|
'application/xls' => 'file-excel-o', |
19
|
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'file-excel-o', |
20
|
|
|
'application/vnd.google-apps.spreadsheet' => 'file-excel-o', |
21
|
|
|
'application/msword' => 'file-word-o', |
22
|
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'file-word-o', |
23
|
|
|
'application/x-rar-compressed' => 'file-archive-o', |
24
|
|
|
'application/x-zip-compressed' => 'file-archive-o', |
25
|
|
|
'application/zip' => 'file-archive-o', |
26
|
|
|
'text' => 'file-text-o', |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Convert a string to url friendly slug |
31
|
|
|
* |
32
|
|
|
* @param string $str |
33
|
|
|
* @param array $replace |
34
|
|
|
* @param string $delimiter |
35
|
|
|
* |
36
|
|
|
* @return mixed|string |
37
|
|
|
*/ |
38
|
|
|
public static function slugify($str, $replace = array(), $delimiter = '-') |
39
|
|
|
{ |
40
|
|
|
if (!empty($replace)) { |
41
|
|
|
$str = str_replace((array)$replace, ' ', $str); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str); |
45
|
|
|
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); |
46
|
|
|
$clean = strtolower(trim($clean, '-')); |
47
|
|
|
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); |
48
|
|
|
|
49
|
|
|
return $clean; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Selects the right font-awesome icon for each filetype |
54
|
|
|
* |
55
|
|
|
* @param $fileType |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public static function iconByFileType($fileType) |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
foreach (self::$fileTypeIcons as $needle => $icon) { |
64
|
|
|
if (strpos($fileType, $needle) !== false) { |
65
|
|
|
return $icon; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return 'file-o'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Converts an amount of bytes to a human readable |
74
|
|
|
* format |
75
|
|
|
* |
76
|
|
|
* @param $size |
77
|
|
|
* @param string $unit |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public static function humanFileSize($size, $unit = '') |
82
|
|
|
{ |
83
|
|
|
if (self::isHumanFilesizeUnitGb($size, $unit)) { |
84
|
|
|
return number_format($size / (1 << 30), 2) . 'GB'; |
85
|
|
|
} |
86
|
|
|
if (self::isHumanFilesizeUnitMb($size, $unit)) { |
87
|
|
|
return number_format($size / (1 << 20), 2) . 'MB'; |
88
|
|
|
} |
89
|
|
|
if (self::isHumanFilesizeUnitKb($size, $unit)) { |
90
|
|
|
return number_format($size / (1 << 10), 2) . 'KB'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return number_format($size) . ' bytes'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $ptime |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
* |
101
|
|
|
*/ |
102
|
|
|
public static function timeElapsedString($ptime) |
103
|
|
|
{ |
104
|
|
|
$etime = time() - $ptime; |
105
|
|
|
|
106
|
|
|
if ($etime < 1) { |
107
|
|
|
return '0 seconds'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */ |
111
|
|
|
$a = array( |
112
|
|
|
365 * 24 * 60 * 60 => 'year', |
113
|
|
|
30 * 24 * 60 * 60 => 'month', |
114
|
|
|
24 * 60 * 60 => 'day', |
115
|
|
|
60 * 60 => 'hour', |
116
|
|
|
60 => 'minute', |
117
|
|
|
1 => 'second' |
118
|
|
|
); |
119
|
|
|
$a_plural = array( |
120
|
|
|
'year' => 'years', |
121
|
|
|
'month' => 'months', |
122
|
|
|
'day' => 'days', |
123
|
|
|
'hour' => 'hours', |
124
|
|
|
'minute' => 'minutes', |
125
|
|
|
'second' => 'seconds' |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
foreach ($a as $secs => $str) { |
129
|
|
|
$d = $etime / $secs; |
130
|
|
|
if ($d >= 1) { |
131
|
|
|
$r = round($d); |
132
|
|
|
|
133
|
|
|
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago'; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return 0; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $size |
142
|
|
|
* @param $unit |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
protected static function isHumanFilesizeUnitGb($size, $unit) |
146
|
|
|
{ |
147
|
|
|
return (!$unit && $size >= 1 << 30) || $unit === 'GB'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $size |
152
|
|
|
* @param $unit |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
protected static function isHumanFilesizeUnitMb($size, $unit) |
156
|
|
|
{ |
157
|
|
|
return (!$unit && $size >= 1 << 20) || $unit === 'MB'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $size |
162
|
|
|
* @param $unit |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
protected static function isHumanFilesizeUnitKb($size, $unit) |
166
|
|
|
{ |
167
|
|
|
return (!$unit && $size >= 1 << 10) || $unit === 'KB'; |
168
|
|
|
} |
169
|
|
|
} |