util.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. class Util {
  3. /**
  4. * @param string|null $gender
  5. * @return string
  6. */
  7. public static function getGenderText(?string $gender): string {
  8. switch($gender) {
  9. case "M": return MALE;
  10. case "F": return FEMALE;
  11. default: return "";
  12. }
  13. }
  14. /**
  15. * @param string|null $boolean
  16. * @return string
  17. */
  18. public static function getBooleanText(?string $boolean): string {
  19. switch($boolean) {
  20. case "Y": return YES;
  21. case "N": return NO;
  22. default: return "";
  23. }
  24. }
  25. /**
  26. * @param string $string
  27. * @param string $target_encoding
  28. * @return string
  29. */
  30. public static function convertUTF8(string $string, string $target_encoding = "CP1252"): string {
  31. return mb_convert_encoding($string, $target_encoding, "UTF-8");
  32. }
  33. /**
  34. * @param array $items
  35. * @param int $page_size
  36. * @return object
  37. * @throws Exception
  38. */
  39. public static function getPaginationObject(array $items, int $page_size = 25) {
  40. $item_count = count($items);
  41. $page = http::value("page");
  42. if($page < 1) $page = 1;
  43. $offset = ($page - 1) * $page_size;
  44. $page_count = ceil($item_count / $page_size);
  45. if($page_count < 1) $page_count = 1;
  46. $first_item = ($offset + 1);
  47. $last_item = $offset + $page_size;
  48. if($last_item > $item_count) {
  49. $last_item = $item_count;
  50. }
  51. if($page_count == 1) {
  52. $range_text = "Showing $item_count items";
  53. }
  54. else {
  55. $range_text = "Showing $first_item - $last_item of $item_count items";
  56. }
  57. return (object) [
  58. "page" => $page,
  59. "pageCount" => $page_count,
  60. "pageSize" => $page_size,
  61. "offset" => $offset,
  62. "firstItem" => $first_item,
  63. "lastItem" => $last_item,
  64. "rangeText" => $range_text
  65. ];
  66. }
  67. /**
  68. * @param string $folder
  69. * @param int $id
  70. * @return string
  71. */
  72. public static function getItemDirectory(string $folder, int $id): string {
  73. $shard = str_pad($id % 1000, 3, "0", STR_PAD_LEFT);
  74. return ROOT_DIR . "/files/" . $folder . "/" . $shard . "/" . $id;
  75. }
  76. /**
  77. * @param string $folder
  78. * @param int $id
  79. * @return string
  80. */
  81. public static function createItemDirectory(string $folder, int $id): string {
  82. $directory = Util::getItemDirectory($folder, $id);
  83. if(!file_exists($directory)) {
  84. mkdir($directory, 0777, true);
  85. }
  86. return $directory;
  87. }
  88. /**
  89. * @param string $string
  90. * @return string
  91. */
  92. public static function slug(string $string): string {
  93. $slug = strtolower($string);
  94. $slug = preg_replace("/[^0-9a-z]/i", "-", $slug); // Replace anything not 0-9 a-z with dash
  95. $slug = preg_replace("/-+/", "-", $slug); // Replace consequential dashes with dash
  96. $slug = trim($slug, "-");
  97. return $slug;
  98. }
  99. /**
  100. * @param string $string
  101. * @param int $id
  102. * @return string
  103. */
  104. public static function slugWithId(string $string, int $id): string {
  105. return Util::slug($string) . "-" . $id;
  106. }
  107. /**
  108. * @param int $size
  109. * @return string
  110. */
  111. public static function getDisplayFileSize(int $size): string {
  112. if($size < pow(2, 10)) return number_format($size) . " bytes";
  113. if($size < pow(2, 20)) return number_format($size / pow(2, 10), 2) . " KB";
  114. if($size < pow(2, 30)) return number_format($size / pow(2, 20), 2) . " MB";
  115. if($size < pow(2, 40)) return number_format($size / pow(2, 30), 2) . " GB";
  116. return number_format($size / pow(2, 40), 2) . " TB";
  117. }
  118. /**
  119. * @param int $unix_time
  120. * @param array $options
  121. * @return string
  122. */
  123. public static function timeDifference(int $unix_time, $options = []): string {
  124. $plain = $options["plain"] ?? false;
  125. $now = time();
  126. $difference = $now - $unix_time;
  127. $tense = ($difference >= 0) ? "ago" : "to go";
  128. $difference = abs($difference);
  129. if($difference < 60) {
  130. $value = $difference;
  131. $class = "text-danger";
  132. $unit = "second";
  133. }
  134. else if($difference < 3600) {
  135. $value = floor($difference / 60);
  136. $class = "text-danger";
  137. $unit = "minute";
  138. }
  139. else if($difference < 86400) {
  140. $value = floor($difference / 3600);
  141. $class = "text-warning";
  142. $unit = "hour";
  143. }
  144. else if($difference < 2592000) {
  145. $value = floor($difference / 86400);
  146. $class = "";
  147. $unit = "day";
  148. }
  149. else if($difference < 31536000) {
  150. $value = floor($difference / 2592000);
  151. $class = "text-muted";
  152. $unit = "month";
  153. }
  154. else {
  155. $value = floor($difference / 31536000);
  156. $class = "text-muted";
  157. $unit = "year";
  158. }
  159. $text = "$value $unit" . ($value != 1 ? "s" : "") . " $tense";
  160. if($plain) {
  161. return "$text";
  162. }
  163. else {
  164. return "<span class=\"$class\">$text</span>";
  165. }
  166. }
  167. /**
  168. * @return array
  169. * @throws Exception
  170. */
  171. public static function getTimeZones(): array {
  172. // Taken and modified from: https://stackoverflow.com/a/17355238
  173. static $regions = [
  174. DateTimeZone::AFRICA,
  175. DateTimeZone::AMERICA,
  176. DateTimeZone::ANTARCTICA,
  177. DateTimeZone::ASIA,
  178. DateTimeZone::ATLANTIC,
  179. DateTimeZone::AUSTRALIA,
  180. DateTimeZone::EUROPE,
  181. DateTimeZone::INDIAN,
  182. DateTimeZone::PACIFIC,
  183. ];
  184. $identifiers = [];
  185. foreach($regions as $region) {
  186. $identifiers = array_merge($identifiers, DateTimeZone::listIdentifiers($region));
  187. }
  188. $time_zone_offsets = [];
  189. foreach($identifiers as $identifier) {
  190. $tz = new DateTimeZone($identifier);
  191. $time_zone_offsets[] = (object) [
  192. "identifier" => $identifier,
  193. "offset" => $tz->getOffset(new DateTime),
  194. ];
  195. }
  196. uasort($time_zone_offsets, function($a, $b) {
  197. if($a->offset > $b->offset) return 1;
  198. else if($a->offset < $b->offset) return -1;
  199. else return strcasecmp($a->identifier, $b->identifier);
  200. });
  201. $sr = [
  202. "/" => ", ",
  203. "St" => "St.",
  204. "_" => " ",
  205. ];
  206. $time_zone_list = [];
  207. foreach($time_zone_offsets as $object) {
  208. $identifier = $object->identifier;
  209. $offset = $object->offset;
  210. $display_identifier = str_replace(array_keys($sr), array_values($sr), $identifier);
  211. $offset_prefix = $offset < 0 ? "-" : "+";
  212. $offset_formatted = gmdate("H:i", abs($offset));
  213. $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  214. $time_zone_list[$identifier] = "${pretty_offset} - $display_identifier";
  215. }
  216. return $time_zone_list;
  217. }
  218. /**
  219. * @param bool $time
  220. * @return string
  221. */
  222. public static function getDate($time = false): string {
  223. if(!$time) $time = time();
  224. return gmdate("Y-m-d", $time);
  225. }
  226. /**
  227. * @param bool $time
  228. * @return string
  229. */
  230. public static function getUTCDateTime($time = false): string {
  231. return Util::getDateTime($time);
  232. }
  233. /**
  234. * @param bool $time
  235. * @return string
  236. */
  237. public static function getDateTime($time = false): string {
  238. if(!$time) $time = time();
  239. return gmdate("Y-m-d H:i:s", $time);
  240. }
  241. /**
  242. * @param string $dts
  243. * @return false|int
  244. */
  245. public static function getUnixTimeFromUTC(string $dts) {
  246. return strtotime($dts . " UTC");
  247. }
  248. /**
  249. * @param string $dts1
  250. * @param string $dts2
  251. * @return float|int
  252. */
  253. public static function getDurationFromDTS(string $dts1, string $dts2) {
  254. $tz = date_default_timezone_get();
  255. date_default_timezone_set("UTC");
  256. $t1 = strtotime($dts1);
  257. $t2 = strtotime($dts2);
  258. date_default_timezone_set($tz);
  259. return abs($t2 - $t1);
  260. }
  261. /**
  262. * @param int|bool $time
  263. * @return string
  264. */
  265. public static function getDisplayDateTime($time = false): string {
  266. if($time === false) $time = time();
  267. $current_year = date("Y");
  268. $year = date("Y", $time);
  269. $format = ($year == $current_year) ? "M j, H:i" : "M j Y, H:i";
  270. return date($format, $time);
  271. }
  272. /**
  273. * @param int|bool $time
  274. * @return string
  275. */
  276. public static function getDisplayDate($time = false): string {
  277. if($time === false) $time = time();
  278. $current_year = date("Y");
  279. $year = date("Y", $time);
  280. $format = ($year == $current_year) ? "M j" : "M j Y";
  281. return date($format, $time);
  282. }
  283. /**
  284. * @param string $dts
  285. * @return string
  286. */
  287. public static function getDisplayDateTimeFromUTC(string $dts): string {
  288. $unix = Util::getUnixTimeFromUTC($dts);
  289. return Util::getDisplayDateTime($unix);
  290. }
  291. /**
  292. * @param array $array
  293. * @param string $field
  294. * @param string $compare_function
  295. * @return array
  296. */
  297. public static function sortObjectArray(array $array, string $field, string $compare_function = ""): array {
  298. usort($array, function($a, $b) use ($compare_function, $field) {
  299. switch($compare_function) {
  300. default:
  301. if($a->$field == $b->$field) return 0;
  302. return ($a->$field < $b->$field) ? -1 : 1;
  303. case "strnatcasecmp":
  304. return strnatcasecmp($a->$field, $b->$field);
  305. }
  306. });
  307. return $array;
  308. }
  309. /**
  310. * @param array $input
  311. * @return array
  312. */
  313. public static function arrayFlipByID(array $input = []): array {
  314. $output = [];
  315. foreach($input as $object) {
  316. $output[$object->getID()] = $object;
  317. }
  318. return $output;
  319. }
  320. }