1 | <?php |
||
2 | |||
3 | namespace Mathielen\ImportEngineBundle; |
||
4 | |||
5 | class Utils |
||
6 | { |
||
7 | /** |
||
8 | * @return array |
||
9 | */ |
||
10 | 4 | public static function parseSourceId($sourceId) |
|
11 | { |
||
12 | 4 | if (is_file($sourceId)) { |
|
13 | return $sourceId; |
||
14 | 4 | } elseif (preg_match('/^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+/', $sourceId)) { |
|
15 | 1 | $parsedSourceId = parse_url($sourceId); |
|
16 | 1 | if (isset($parsedSourceId['query'])) { |
|
17 | 1 | parse_str($parsedSourceId['query'], $parsedSourceId['query']); |
|
18 | } |
||
19 | 1 | $pathTokens = explode('.', $parsedSourceId['path']); |
|
20 | 1 | $method = array_pop($pathTokens); |
|
21 | 1 | $service = implode('.', $pathTokens); |
|
22 | |||
23 | return array( |
||
24 | 1 | 'service' => $service, |
|
25 | 1 | 'method' => $method, |
|
26 | 1 | 'arguments' => isset($parsedSourceId['query']) ? $parsedSourceId['query'] : null, |
|
27 | ); |
||
28 | } |
||
29 | |||
30 | 3 | return $sourceId; |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * @return bool |
||
35 | */ |
||
36 | 5 | public static function isWindows() |
|
37 | { |
||
38 | 5 | return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | 1 | public static function isCli() |
|
45 | { |
||
46 | 1 | return php_sapi_name() == 'cli'; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return string |
||
51 | */ |
||
52 | 5 | public static function whoAmI() |
|
53 | { |
||
54 | 5 | if (self::isWindows()) { |
|
55 | $user = getenv('username'); |
||
56 | } else { |
||
57 | 5 | $processUser = posix_getpwuid(posix_geteuid()); |
|
58 | 5 | $user = $processUser['name']; |
|
59 | } |
||
60 | |||
61 | 5 | return $user; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return array |
||
66 | */ |
||
67 | 5 | public static function numbersToRangeText(array $numbers) |
|
68 | { |
||
69 | 5 | if (empty($numbers)) { |
|
70 | 1 | return []; |
|
71 | } |
||
72 | |||
73 | 4 | $ranges = []; |
|
74 | 4 | sort($numbers); |
|
75 | |||
76 | 4 | $currentRange = []; |
|
77 | 4 | foreach ($numbers as $number) { |
|
78 | 4 | if (!empty($currentRange) && current($currentRange) !== $number - 1) { |
|
79 | 2 | self::addRangeText($ranges, $currentRange); |
|
80 | |||
81 | 2 | $currentRange = []; |
|
82 | } |
||
83 | |||
84 | 4 | $currentRange[] = $number; |
|
85 | 4 | end($currentRange); |
|
86 | } |
||
87 | |||
88 | 4 | self::addRangeText($ranges, $currentRange); |
|
89 | |||
90 | 4 | return $ranges; |
|
91 | } |
||
92 | |||
93 | 4 | private static function addRangeText(array &$ranges, array $currentRange) |
|
94 | { |
||
95 | 4 | $lastItem = current($currentRange); |
|
96 | |||
97 | 4 | if (count($currentRange) === 1) { |
|
98 | 2 | $ranges[] = $lastItem; |
|
99 | } else { |
||
100 | 2 | $firstItem = reset($currentRange); |
|
101 | 2 | $ranges[] = $firstItem.'-'.$lastItem; |
|
102 | } |
||
103 | 4 | } |
|
104 | } |
||
105 |