1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Utilities |
4
|
|
|
*/ |
5
|
|
|
namespace App\Utils; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Utils |
9
|
|
|
* @package App\Utils |
10
|
|
|
*/ |
11
|
|
|
class Utils |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* New line supporting cli or browser. |
15
|
|
|
* |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public static function newLine() |
19
|
|
|
{ |
20
|
|
|
if (php_sapi_name() == "cli") { |
21
|
|
|
return "\n"; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return "<br>"; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Count the total lessons of an array of lessons & series. |
29
|
|
|
* |
30
|
|
|
* @param $array |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public static function countAllLessons($array) |
35
|
|
|
{ |
36
|
|
|
$total = count($array['lessons']); |
37
|
|
|
$total += self::countEpisodes($array); |
38
|
|
|
|
39
|
|
|
return $total; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Counts the lessons from the array. |
44
|
|
|
* |
45
|
|
|
* @param $array |
46
|
|
|
* |
47
|
|
|
* @return int |
48
|
|
|
*/ |
49
|
|
|
public static function countLessons($array) |
50
|
|
|
{ |
51
|
|
|
return count($array['lessons']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Counts the episodes from the array. |
56
|
|
|
* |
57
|
|
|
* @param $array |
58
|
|
|
* |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
|
|
public static function countEpisodes($array) |
62
|
|
|
{ |
63
|
|
|
$total = 0; |
64
|
|
|
foreach ($array['series'] as $serie) { |
65
|
|
|
$total += count($serie); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $total; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Compare two arrays and returns the diff array. |
73
|
|
|
* |
74
|
|
|
* @param $onlineListArray |
75
|
|
|
* @param $localListArray |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public static function resolveFaultyLessons($onlineListArray, $localListArray) |
80
|
|
|
{ |
81
|
|
|
$array = []; |
82
|
|
|
$array['series'] = []; |
83
|
|
|
$array['lessons'] = []; |
84
|
|
|
|
85
|
|
|
foreach ($onlineListArray['series'] as $serie => $episodes) { |
86
|
|
|
if (isset($localListArray['series'][$serie])) { |
87
|
|
|
if (count($episodes) == count($localListArray['series'][$serie])) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach ($episodes as $episode) { |
92
|
|
|
if (!in_array($episode, $localListArray['series'][$serie])) { |
93
|
|
|
$array['series'][$serie][] = $episode; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
$array['series'][$serie] = $episodes; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$array['lessons'] = array_diff($onlineListArray['lessons'], $localListArray['lessons']); |
102
|
|
|
|
103
|
|
|
return $array; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Echo's text in a nice box. |
108
|
|
|
* |
109
|
|
|
* @param $text |
110
|
|
|
*/ |
111
|
|
|
public static function box($text) |
112
|
|
|
{ |
113
|
|
|
echo self::newLine(); |
114
|
|
|
echo "====================================".self::newLine(); |
115
|
|
|
echo $text.self::newLine(); |
116
|
|
|
echo "====================================".self::newLine(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Echo's a message. |
121
|
|
|
* |
122
|
|
|
* @param $text |
123
|
|
|
*/ |
124
|
|
|
public static function write($text) |
125
|
|
|
{ |
126
|
|
|
echo "> ".$text.self::newLine(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Remove specials chars that windows does not support for filenames. |
131
|
|
|
* |
132
|
|
|
* @param $name |
133
|
|
|
* |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
|
|
public static function parseEpisodeName($name) |
137
|
|
|
{ |
138
|
|
|
$toRemove = 'New'; |
139
|
|
|
$striped = preg_replace('/[^A-Za-z0-9\- _]/', '', $name); |
140
|
|
|
|
141
|
|
|
if (strpos($striped, $toRemove) !== false) { //remove last New string |
142
|
|
|
$striped = preg_replace('/'. preg_quote($toRemove, '/') . '$/', '', $striped); |
143
|
|
|
return rtrim($striped); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $striped; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Echo's a message in a new line. |
151
|
|
|
* |
152
|
|
|
* @param $text |
153
|
|
|
*/ |
154
|
|
|
public static function writeln($text) |
155
|
|
|
{ |
156
|
|
|
echo self::newLine(); |
157
|
|
|
echo "> ".$text.self::newLine(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Convert bytes to precision |
162
|
|
|
* @param $bytes |
163
|
|
|
* @param int $precision |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public static function formatBytes($bytes, $precision = 2) { |
167
|
|
|
$units = array('B', 'KB', 'MB', 'GB', 'TB'); |
168
|
|
|
|
169
|
|
|
$bytes = max($bytes, 0); |
170
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
171
|
|
|
$pow = min($pow, count($units) - 1); |
172
|
|
|
|
173
|
|
|
$bytes /= (1 << (10 * $pow)); |
174
|
|
|
|
175
|
|
|
return round($bytes, $precision) . ' ' . $units[$pow]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Calculate a percentage |
180
|
|
|
* @param $cur |
181
|
|
|
* @param $total |
182
|
|
|
* @return float |
183
|
|
|
*/ |
184
|
|
|
public static function getPercentage($cur, $total) { |
185
|
|
|
return @($cur/$total * 100); //hide warning division by zero |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|