Helper.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Utils;
  3. class Helper
  4. {
  5. public static function guid ($format = false) {
  6. if (function_exists('com_create_guid') === true) {
  7. return md5(trim(com_create_guid(), '{}'));
  8. }
  9. $data = openssl_random_pseudo_bytes(16);
  10. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  11. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  12. if ($format) {
  13. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  14. }
  15. return md5(vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)).'-'.time());
  16. }
  17. public static function exchange ($from, $to) {
  18. $result = file_get_contents('https://api.exchangeratesapi.io/latest?symbols=' . $to . '&base=' . $from);
  19. $result = json_decode($result, true);
  20. return $result['rates'][$to];
  21. }
  22. public static function randomChar($len, $special=false){
  23. $chars = array(
  24. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  25. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  26. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  27. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  28. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  29. "3", "4", "5", "6", "7", "8", "9"
  30. );
  31. if($special){
  32. $chars = array_merge($chars, array(
  33. "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
  34. "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
  35. "}", "<", ">", "~", "+", "=", ",", "."
  36. ));
  37. }
  38. $charsLen = count($chars) - 1;
  39. shuffle($chars);
  40. $str = '';
  41. for($i=0; $i<$len; $i++){
  42. $str .= $chars[mt_rand(0, $charsLen)];
  43. }
  44. return $str;
  45. }
  46. }