Passed
Push — master ( d5fa9e...eded98 )
by Pauli
01:56
created

Util::endsWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
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, 2019
11
 */
12
13
namespace OCA\Music\Utility;
14
15
/**
16
 * Miscellaneous static utility functions
17
 */
18
class Util {
19
20
	/**
21
	 * Extract ID of each array element by calling getId and return
22
	 * the IDs as array
23
	 * @param array $arr
24
	 * @return array
25
	 */
26
	public static function extractIds(array $arr) {
27 4
		return \array_map(function ($i) {
28 4
			return $i->getId();
29 4
		}, $arr);
30
	}
31
32
	/**
33
	 * Create look-up table from given array of items which have a `getId` function.
34
	 * @param array $array
35
	 * @return array where keys are the values returned by `getId` of each item
36
	 */
37
	public static function createIdLookupTable($array) {
38
		$lut = [];
39
		foreach ($array as $item) {
40
			$lut[$item->getId()] = $item;
41
		}
42
		return $lut;
43
	}
44
45
	/**
46
	 * Get difference of two arrays, i.e. elements belonging to $b but not $a.
47
	 * This function is faster than the built-in array_diff for large arrays but
48
	 * at the expense of higher RAM usage and can be used only for arrays of
49
	 * integers or strings.
50
	 * From https://stackoverflow.com/a/8827033
51
	 * @param array $b
52
	 * @param array $a
53
	 * @return array
54
	 */
55
	public static function arrayDiff($b, $a) {
56
		$at = \array_flip($a);
57
		$d = [];
58
		foreach ($b as $i) {
59
			if (!isset($at[$i])) {
60
				$d[] = $i;
61
			}
62
		}
63
		return $d;
64
	}
65
66
	/**
67
	 * Get multiple items from @a $array, as indicated by a second array @a $indices.
68
	 * @param array $array
69
	 * @param array $indices
70
	 * @return array
71
	 */
72
	public static function arrayMultiGet($array, $indices) {
73
		$result = [];
74
		foreach ($indices as $index) {
75
			$result[] = $array[$index];
76
		}
77
		return $result;
78
	}
79
80
	/**
81
	 * Get array value if exists, otherwise return a default value or null
82
	 * @param array $array
83
	 * @param int|string $key
84
	 * @param mixed|null $default
85
	 * @return mixed|null
86
	 */
87 3
	public static function arrayGetOrDefault($array, $key, $default=null) {
88 3
		return isset($array[$key]) ? $array[$key] : $default;
89
	}
90
91
	/**
92
	 * Truncate the given string to maximum length, appendig ellipsis character
93
	 * if the truncation happened. Also null argument may be safely passed and
94
	 * it remains unaltered.
95
	 * @param string|null $string
96
	 * @param int $maxLength
97
	 * @return string|null
98
	 */
99 3
	public static function truncate($string, $maxLength) {
100 3
		if ($string === null) {
101 1
			return null;
102
		} else {
103 2
			return \mb_strimwidth($string, 0, $maxLength, "\u{2026}");
104
		}
105
	}
106
107
	/**
108
	 * Test if given string starts with another given string
109
	 * @param string $string
110
	 * @param string $potentialStart
111
	 * @return boolean
112
	 */
113
	public static function startsWith($string, $potentialStart) {
114
		return \substr($string, 0, \strlen($potentialStart)) === $potentialStart;
115
	}
116
117
	/**
118
	 * Test if given string ends with another given string
119
	 * @param string $string
120
	 * @param string $potentialEnd
121
	 * @return boolean
122
	 */
123
	public static function endsWith($string, $potentialEnd) {
124
		return \substr($string, -\strlen($potentialEnd)) === $potentialEnd;
125
	}
126
127
	/**
128
	 * Multi-byte safe case-insensitive string comparison
129
	 * @param string $a
130
	 * @param string $b
131
	 */
132
	public static function stringCaseCompare($a, $b) {
133
		return \strcmp(\mb_strtolower($a), \mb_strtolower($b));
134
	}
135
136
	/**
137
	 * Convert file size given in bytes to human-readable format
138
	 * @param int $bytes
139
	 * @param int $decimals
140
	 * @return string
141
	 */
142
	public static function formatFileSize($bytes, $decimals = 1) {
143
		$units = 'BKMGTP';
144
		$factor = \floor((\strlen($bytes) - 1) / 3);
145
		return \sprintf("%.{$decimals}f", $bytes / \pow(1024, $factor)) . @$units[(int)$factor];
146
	}
147
148
	/**
149
	 * @param Folder $parentFolder
0 ignored issues
show
Bug introduced by
The type OCA\Music\Utility\Folder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
150
	 * @param string $relativePath
151
	 * @return Folder
152
	 */
153
	public static function getFolderFromRelativePath($parentFolder, $relativePath) {
154
		if ($relativePath !== null && $relativePath !== '/' && $relativePath !== '') {
155
			return $parentFolder->get($relativePath);
156
		} else {
157
			return $parentFolder;
158
		}
159
	}
160
161
	/**
162
	 * Swap values of two variables in place
163
	 * @param mixed $a
164
	 * @param mixed $b
165
	 */
166
	public static function swap(&$a, &$b) {
167
		$temp = $a;
168
		$a = $b;
169
		$b = $temp;
170
	}
171
}
172