Util::buildUrl()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 9
eloc 10
c 4
b 0
f 0
nc 256
nop 1
dl 0
loc 11
rs 6.5222
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 to the ISO UTC "Zulu format" e.g. "2021-08-19T19:33:15Z". The date and time may be passed
69
	 * as a DateTime object or any string format accepted by the DateTime constructor.
70
	 * @param string|\DateTime|null $dateTime
71
	 */
72
	public static function formatZuluDateTime(/*mixed*/ $dateTime) : ?string {
73
		if ($dateTime === null) {
74
			return null;
75
		} else {
76
			if (\is_string($dateTime)) {
77
				$dateTime = new \DateTime($dateTime);
78
			}
79
			return $dateTime->format('Y-m-d\TH:i:s.v\Z');
80
		}
81
	}
82
83
	/**
84
	 * Convert date and time given in the SQL format to the ISO UTC "offset format" e.g. "2021-08-19T19:33:15+00:00"
85
	 */
86
	public static function formatDateTimeUtcOffset(?string $dbDateString) : ?string {
87
		if ($dbDateString === null) {
88
			return null;
89
		} else {
90
			$dateTime = new \DateTime($dbDateString);
91
			return $dateTime->format('c');
92
		}
93
	}
94
95
	/**
96
	 * Encode a file path so that it can be used as part of a WebDAV URL
97
	 */
98
	public static function urlEncodePath(string $path) : string {
99
		// URL encode each part of the file path
100
		return \join('/', \array_map('rawurlencode', \explode('/', $path)));
101
	}
102
103
	/**
104
	 * Compose URL from parts as returned by the system function parse_url.
105
	 * Based on https://stackoverflow.com/a/35207936
106
	 * @param array{scheme?: string, host?: string, port?: int, user?: string, pass?: string, query?: string, path?: string, fragment?: string} $parts
107
	 */
108
	public static function buildUrl(array $parts) : string {
109
		return (isset($parts['scheme']) ? "{$parts['scheme']}:" : '')
110
			. ((isset($parts['user']) || isset($parts['host'])) ? '//' : '')
111
			. ($parts['user'] ?? '')
112
			. (isset($parts['pass']) ? ":{$parts['pass']}" : '')
113
			. (isset($parts['user']) ? '@' : '')
114
			. ($parts['host'] ?? '')
115
			. (isset($parts['port']) ? ":{$parts['port']}" : '')
116
			. ($parts['path'] ?? '')
117
			. (isset($parts['query']) ? "?{$parts['query']}" : '')
118
			. (isset($parts['fragment']) ? "#{$parts['fragment']}" : '');
119
120
	}
121
122
	/**
123
	 * Swap values of two variables in place
124
	 * @param mixed $a
125
	 * @param mixed $b
126
	 */
127
	public static function swap(&$a, &$b) : void {
128
		$temp = $a;
129
		$a = $b;
130
		$b = $temp;
131
	}
132
133
	/**
134
	 * Limit an integer value between the specified minimum and maximum.
135
	 * A null value is a valid input and will produce a null output.
136
	 * @param int|float|null $input
137
	 * @param int|float $min
138
	 * @param int|float $max
139
	 * @return int|float|null
140
	 */
141
	public static function limit($input, $min, $max) {
142
		if ($input === null) {
143
			return null;
144
		} else {
145
			return \max($min, \min($input, $max));
146
		}
147
	}
148
}
149