1
0

util.php 8.7 KB

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