1 | <?php |
||
11 | class Utils |
||
12 | { |
||
13 | /** |
||
14 | * New line supporting cli or browser. |
||
15 | * |
||
16 | * @return string |
||
17 | */ |
||
18 | public static function newLine() |
||
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) |
||
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) |
||
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) |
||
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) { |
||
177 | |||
178 | /** |
||
179 | * Calculate a percentage |
||
180 | * @param $cur |
||
181 | * @param $total |
||
182 | * @return float |
||
183 | */ |
||
184 | public static function getPercentage($cur, $total) { |
||
187 | } |
||
188 |