forked from ReactiveX/RxPHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.php
66 lines (57 loc) · 1.68 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
abstract class Str {
public static function contains($haystack, $needle) {
return false !== strpos($haystack, $needle);
}
public static function containsAny($haystack, $needles) {
foreach ($needles as $needle) {
if (Str::contains($haystack, $needle)) {
return true;
}
}
return false;
}
public static function firstWord($haystack) {
$words = explode(' ', $haystack);
return $words[0];
}
public static function startsWith($haystack, $needle) {
return 0 === strpos($haystack, $needle);
}
public static function substringAfter($haystack, $needle) {
$pos = strpos($haystack, $needle);
if (false === $pos) {
return $haystack;
}
return trim(substr($haystack, $pos + strlen($needle)));
}
public static function substringUntil($haystack, $needle) {
$pos = strpos($haystack, $needle);
if (false === $pos) {
return $haystack;
}
return trim(substr($haystack, 0, $pos));
}
}
function group_by($collection, callable $groupSelector) {
$grouped = [];
foreach ($collection as $item) {
$group = $groupSelector($item);
if (!isset($grouped[$group])) {
$grouped[$group] = [];
}
$grouped[$group][] = $item;
}
return $grouped;
}
function is_clean_git_checkout($path) {
list($exitCode, $output) = run_cmd("git status --porcelain $path");
return $exitCode === 0
&& count($output) === 0;
}
function run_cmd($cmd) {
$output = [];
$exitCode = 0;
exec($cmd, $output, $exitCode);
return array($exitCode, $output);
}