Util::formatTime()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 5
rs 10
c 2
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2018 - 2025
11
 */
12
13
namespace OCA\Music\Utility;
14
15
/**
16
 * Miscellaneous static utility functions
17
 */
18
class Util {
19
20
	const UINT32_MAX = 0xFFFFFFFF;
21
	const SINT32_MAX = 0x7FFFFFFF;
22
	const SINT32_MIN = -self::SINT32_MAX - 1;
23
24
	/**
25
	 * Like the built-in \explode(...) function but this one can be safely called with
26
	 * null string, and no warning will be emitted. Also, this returns an empty array from
27
	 * null and '' inputs while the built-in alternative returns a 1-item array containing
28
	 * an empty string.
29
	 * @param string $delimiter
30
	 * @param string|null $string
31
	 * @return string[]
32
	 */
33
	public static function explode(string $delimiter, ?string $string) : array {
34
		if ($delimiter === '') {
35
			throw new \UnexpectedValueException();
36
		} elseif ($string === null || $string === '') {
37
			return [];
38
		} else {
39
			return \explode($delimiter, $string);
40
		}
41
	}
42
43
	/**
44
	 * Convert file size given in bytes to human-readable format
45
	 */
46
	public static function formatFileSize(?int $bytes, int $decimals = 1) : ?string {
47
		if ($bytes === null) {
48
			return null;
49
		} else {
50
			$units = 'BKMGTP';
51
			$factor = \floor((\strlen((string)$bytes) - 1) / 3);
52
			return \sprintf("%.{$decimals}f", $bytes / \pow(1024, $factor)) . @$units[(int)$factor];
53
		}
54
	}
55
56
	/**
57
	 * Convert time given as seconds to the HH:MM:SS format
58
	 */
59
	public static function formatTime(?int $seconds) : ?string {
60
		if ($seconds === null) {
61
			return null;
62
		} else {
63
			return \sprintf('%02d:%02d:%02d', ($seconds/3600), ($seconds/60%60), $seconds%60);
64
		}
65
	}
66
67
	/**
68
	 * Convert date and time given in the SQL format to the ISO UTC "Zulu format" e.g. "2021-08-19T19:33:15Z"
69
	 */
70
	public static function formatZuluDateTime(?string $dbDateString) : ?string {
71
		if ($dbDateString === null) {
72
			return null;
73
		} else {
74
			$dateTime = new \DateTime($dbDateString);
75
			return $dateTime->format('Y-m-d\TH:i:s.v\Z');
76
		}
77
	}
78
79
	/**
80
	 * Convert date and time given in the SQL format to the ISO UTC "offset format" e.g. "2021-08-19T19:33:15+00:00"
81
	 */
82
	public static function formatDateTimeUtcOffset(?string $dbDateString) : ?string {
83
		if ($dbDateString === null) {
84
			return null;
85
		} else {
86
			$dateTime = new \DateTime($dbDateString);
87
			return $dateTime->format('c');
88
		}
89
	}
90
91
	/**
92
	 * Encode a file path so that it can be used as part of a WebDAV URL
93
	 */
94
	public static function urlEncodePath(string $path) : string {
95
		// URL encode each part of the file path
96
		return \join('/', \array_map('rawurlencode', \explode('/', $path)));
97
	}
98
99
	/**
100
	 * Compose URL from parts as returned by the system function parse_url.
101
	 * Based on https://stackoverflow.com/a/35207936
102
	 * @param string[] $parts
103
	 */
104
	public static function buildUrl(array $parts) : string {
105
		return (isset($parts['scheme']) ? "{$parts['scheme']}:" : '')
106
			. ((isset($parts['user']) || isset($parts['host'])) ? '//' : '')
107
			. ($parts['user'] ?? '')
108
			. (isset($parts['pass']) ? ":{$parts['pass']}" : '')
109
			. (isset($parts['user']) ? '@' : '')
110
			. ($parts['host'] ?? '')
111
			. (isset($parts['port']) ? ":{$parts['port']}" : '')
112
			. ($parts['path'] ?? '')
113
			. (isset($parts['query']) ? "?{$parts['query']}" : '')
114
			. (isset($parts['fragment']) ? "#{$parts['fragment']}" : '');
115
116
	}
117
118
	/**
119
	 * Swap values of two variables in place
120
	 * @param mixed $a
121
	 * @param mixed $b
122
	 */
123
	public static function swap(&$a, &$b) : void {
124
		$temp = $a;
125
		$a = $b;
126
		$b = $temp;
127
	}
128
129
	/**
130
	 * Limit an integer value between the specified minimum and maximum.
131
	 * A null value is a valid input and will produce a null output.
132
	 * @param int|float|null $input
133
	 * @param int|float $min
134
	 * @param int|float $max
135
	 * @return int|float|null
136
	 */
137
	public static function limit($input, $min, $max) {
138
		if ($input === null) {
139
			return null;
140
		} else {
141
			return \max($min, \min($input, $max));
142
		}
143
	}
144
}
145