Passed
Push — master ( 6d3e46...96e47d )
by Pauli
02:27
created

Util::arrayMultiGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 10
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
	 * Get difference of two arrays, i.e. elements belonging to $b but not $a.
34
	 * This function is faster than the built-in array_diff for large arrays but
35
	 * at the expense of higher RAM usage and can be used only for arrays of
36
	 * integers or strings.
37
	 * From https://stackoverflow.com/a/8827033
38
	 * @param array $b
39
	 * @param array $a
40
	 * @return array
41
	 */
42
	public static function arrayDiff($b, $a) {
43
		$at = \array_flip($a);
44
		$d = [];
45
		foreach ($b as $i) {
46
			if (!isset($at[$i])) {
47
				$d[] = $i;
48
			}
49
		}
50
		return $d;
51
	}
52
53
	/**
54
	 * Get multiple items from @a $array, as indicated by a second array @a $indices.
55
	 * @param array $array
56
	 * @param array $indices
57
	 * @return array
58
	 */
59
	public static function arrayMultiGet($array, $indices) {
60
		$result = [];
61
		foreach ($indices as $index) {
62
			$result[] = $array[$index];
63
		}
64
		return $result;
65
	}
66
67
	/**
68
	 * Truncate the given string to maximum length, appendig ellipsis character
69
	 * if the truncation happened. Also null argument may be safely passed and
70
	 * it remains unaltered.
71
	 * @param string|null $string
72
	 * @param int $maxLength
73
	 * @return string|null
74
	 */
75 3
	public static function truncate($string, $maxLength) {
76 3
		if ($string === null) {
77 1
			return null;
78
		} else {
79 2
			return \mb_strimwidth($string, 0, $maxLength, "\u{2026}");
80
		}
81
	}
82
83
	/**
84
	 * Test if given string starts with another given string
85
	 * @param string $string
86
	 * @param string $potentialStart
87
	 * @return boolean
88
	 */
89
	public static function startsWith($string, $potentialStart) {
90
		return \substr($string, 0, \strlen($potentialStart)) === $potentialStart;
91
	}
92
93
	/**
94
	 * Test if given string ends with another given string
95
	 * @param string $string
96
	 * @param string $potentialEnd
97
	 * @return boolean
98
	 */
99
	public static function endsWith($string, $potentialEnd) {
100
		return \substr($string, -\strlen($potentialEnd)) === $potentialEnd;
101
	}
102
103
	/**
104
	 * Multi-byte safe case-insensitive string comparison
105
	 * @param string $a
106
	 * @param string $b
107
	 */
108
	public static function stringCaseCompare($a, $b) {
109
		return \strcmp(\mb_strtolower($a), \mb_strtolower($b));
110
	}
111
112
	/**
113
	 * Convert file size given in bytes to human-readable format
114
	 * @param int $bytes
115
	 * @param int $decimals
116
	 * @return string
117
	 */
118
	public static function formatFileSize($bytes, $decimals = 1) {
119
		$units = 'BKMGTP';
120
		$factor = \floor((\strlen($bytes) - 1) / 3);
121
		return \sprintf("%.{$decimals}f", $bytes / \pow(1024, $factor)) . @$units[(int)$factor];
122
	}
123
124
	/**
125
	 * @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...
126
	 * @param string $relativePath
127
	 * @return Folder
128
	 */
129
	public static function getFolderFromRelativePath($parentFolder, $relativePath) {
130
		if ($relativePath !== null && $relativePath !== '/' && $relativePath !== '') {
131
			return $parentFolder->get($relativePath);
132
		} else {
133
			return $parentFolder;
134
		}
135
	}
136
}
137