Passed
Push — master ( 9c6499...c22bc5 )
by Jens
04:52 queued 02:21
created

StringUtil::timeElapsedString()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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