Random.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace Tools\Utility;
  3. /**
  4. * Random Lib
  5. *
  6. * @author Mark Scherer
  7. * @license http://opensource.org/licenses/mit-license.php MIT
  8. */
  9. class Random {
  10. /**
  11. * @param int $min
  12. * @param int $max
  13. * @return int Random int value
  14. */
  15. public static function int($min = 0, $max = 999) {
  16. return mt_rand($min, $max);
  17. }
  18. /**
  19. * @param float $min
  20. * @param float $max
  21. * @return float Random float value
  22. */
  23. public static function float($min = 0.0, $max = 999.0) {
  24. $rand = rand(1, 358);
  25. return $rand * cos($rand);
  26. }
  27. /**
  28. * Randomly return one of the values provided
  29. * careful: only works with numerical keys (0 based!)
  30. *
  31. * @param array $array
  32. * @param int|null $minPosition
  33. * @param int|null $maxPosition
  34. * @param bool $integerKeys
  35. * @return mixed
  36. */
  37. public static function arrayValue($array, $minPosition = null, $maxPosition = null, $integerKeys = false) {
  38. if (empty($array)) {
  39. return null;
  40. }
  41. if ($integerKeys) {
  42. $max = count($array) - 1;
  43. return $array[static::int(0, $max)];
  44. }
  45. $keys = array_keys($array);
  46. $values = array_values($array);
  47. $max = count($keys) - 1;
  48. return $values[static::int(0, $max)];
  49. }
  50. /**
  51. * 1950-01-01 - 2050-12-31
  52. *
  53. * @param int|null $min
  54. * @param int|null $max
  55. * @param bool|null $formatReturn
  56. * @return int|string|null
  57. */
  58. public static function date($min = null, $max = null, $formatReturn = null) {
  59. if ($min === null && $max === null) {
  60. $res = time();
  61. } elseif ($min > 0 && $max === null) {
  62. $res = $min;
  63. } elseif ($min > 0 && $max > 0) {
  64. $res = static::int($min, $max);
  65. } else {
  66. $res = time();
  67. }
  68. $res = 0;
  69. $formatReturnAs = FORMAT_DB_DATETIME;
  70. if ($formatReturn !== null) {
  71. if ($formatReturn === false) {
  72. return $res;
  73. }
  74. $formatReturnAs = $formatReturn;
  75. }
  76. return date($formatReturnAs);
  77. }
  78. /**
  79. * 00:00:00 - 23:59:59
  80. *
  81. * TODO
  82. *
  83. * @param int|null $min
  84. * @param int|null $max
  85. * @param bool|null $formatReturn
  86. * @return int
  87. */
  88. public static function time($min = null, $max = null, $formatReturn = null) {
  89. $res = 0;
  90. //$returnValueAs = FORMAT_DB_TIME;
  91. if ($formatReturn !== null) {
  92. if ($formatReturn === false) {
  93. return $res;
  94. }
  95. }
  96. return $res;
  97. }
  98. /**
  99. * Returns a date of birth within the specified age range
  100. *
  101. * @param int $min minimum age in years
  102. * @param int $max maximum age in years
  103. * @return string Dob a db (ISO) format datetime string
  104. */
  105. public static function dob($min = 18, $max = 100) {
  106. $dobYear = (int)date('Y') - (static::int($min, $max));
  107. $dobMonth = static::int(1, 12);
  108. if ($dobMonth == 2) {
  109. // leap year?
  110. if ($dobYear % 4 || $dobYear % 400) {
  111. $maxDays = 29;
  112. } else {
  113. $maxDays = 28;
  114. }
  115. } elseif (in_array($dobMonth, [4, 6, 9, 11])) {
  116. $maxDays = 30;
  117. } else {
  118. $maxDays = 31;
  119. }
  120. $dobDay = static::int(1, $maxDays);
  121. $dob = sprintf('%4d-%02d-%02d', $dobYear, $dobMonth, $dobDay);
  122. return $dob;
  123. }
  124. /**
  125. * Generates a password
  126. *
  127. * @param int $length Password length
  128. * @return string
  129. * @link https://github.com/CakeDC/users/blob/master/models/user.php#L498
  130. */
  131. public static function pronounceablePwd($length = 10) {
  132. srand((int)(double)microtime() * 1000000);
  133. $password = '';
  134. $vowels = ['a', 'e', 'i', 'o', 'u'];
  135. $cons = ['b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'tr',
  136. 'cr', 'br', 'fr', 'th', 'dr', 'ch', 'ph', 'wr', 'st', 'sp', 'sw', 'pr', 'sl', 'cl'];
  137. for ($i = 0; $i < $length; $i++) {
  138. $password .= $cons[mt_rand(0, 31)] . $vowels[mt_rand(0, 4)];
  139. }
  140. return substr($password, 0, $length);
  141. }
  142. /**
  143. * Generates random passwords.
  144. *
  145. * @param int $length (necessary!)
  146. * @param string|null $chars
  147. * @return string Password
  148. */
  149. public static function pwd($length, $chars = null) {
  150. if ($chars === null) {
  151. $chars = '234567890abcdefghijkmnopqrstuvwxyz'; // ABCDEFGHIJKLMNOPQRSTUVWXYZ
  152. }
  153. $i = 0;
  154. $password = '';
  155. $max = strlen($chars) - 1;
  156. while ($i < $length) {
  157. $password .= $chars[mt_rand(0, $max)];
  158. $i++;
  159. }
  160. return $password;
  161. }
  162. /**
  163. * Generates a random string of a given type and length.
  164. * $str = Text::random(); // 8 character random string
  165. *
  166. * The following types are supported:
  167. *
  168. * alnum
  169. * : Upper and lower case a-z, 0-9
  170. *
  171. * alpha
  172. * : Upper and lower case a-z
  173. *
  174. * hexdec
  175. * : Hexadecimal characters a-f, 0-9
  176. *
  177. * distinct
  178. * : Uppercase characters and numbers that cannot be confused
  179. *
  180. * You can also create a custom type by providing the "pool" of characters
  181. * as the type.
  182. *
  183. * @param string $type Type of pool, or a string of characters to use as the pool
  184. * @param int $length of string to return
  185. * @return string
  186. */
  187. public static function generate($type = 'alnum', $length = 8) {
  188. switch ($type) {
  189. case 'alnum':
  190. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  191. break;
  192. case 'alpha':
  193. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  194. break;
  195. case 'hexdec':
  196. $pool = '0123456789abcdef';
  197. break;
  198. case 'numeric':
  199. $pool = '0123456789';
  200. break;
  201. case 'nozero':
  202. $pool = '123456789';
  203. break;
  204. case 'distinct':
  205. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  206. break;
  207. default:
  208. $pool = (string)$type;
  209. break;
  210. }
  211. // Split the pool into an array of characters
  212. $pool = str_split($pool, 1);
  213. // Largest pool key
  214. $max = count($pool) - 1;
  215. $str = '';
  216. for ($i = 0; $i < $length; $i++) {
  217. // Select a random character from the pool and add it to the string
  218. $str .= $pool[mt_rand(0, $max)];
  219. }
  220. // Make sure alnum strings contain at least one letter and one digit
  221. if ($type === 'alnum' && $length > 1) {
  222. if (ctype_alpha($str)) {
  223. // Add a random digit
  224. $str[mt_rand(0, $length - 1)] = chr(mt_rand(48, 57));
  225. } elseif (ctype_digit($str)) {
  226. // Add a random letter
  227. $str[mt_rand(0, $length - 1)] = chr(mt_rand(65, 90));
  228. }
  229. }
  230. return $str;
  231. }
  232. }