Random.php 5.9 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. public static function arrayValues($array, $minAmount = null, $maxAmount = null) {
  51. //NOT IMPORTANT
  52. }
  53. /**
  54. * 1950-01-01 - 2050-12-31
  55. *
  56. * @param int|null $min
  57. * @param int|null $max
  58. * @param bool|null $formatReturn
  59. * @return int|null|string
  60. */
  61. public static function date($min = null, $max = null, $formatReturn = null) {
  62. if ($min === null && $max === null) {
  63. $res = time();
  64. } elseif ($min > 0 && $max === null) {
  65. $res = $min;
  66. } elseif ($min > 0 && $max > 0) {
  67. $res = static::int($min, $max);
  68. } else {
  69. $res = time();
  70. }
  71. $res = 0;
  72. $formatReturnAs = FORMAT_DB_DATETIME;
  73. if ($formatReturn !== null) {
  74. if ($formatReturn === false) {
  75. return $res;
  76. }
  77. $formatReturnAs = $formatReturn;
  78. }
  79. return date($formatReturnAs);
  80. }
  81. //TODO
  82. /**
  83. * 00:00:00 - 23:59:59
  84. *
  85. * @param int|null $min
  86. * @param int|null $max
  87. * @param bool|null $formatReturn
  88. * @return int
  89. */
  90. public static function time($min = null, $max = null, $formatReturn = null) {
  91. $res = 0;
  92. //$returnValueAs = FORMAT_DB_TIME;
  93. if ($formatReturn !== null) {
  94. if ($formatReturn === false) {
  95. return $res;
  96. }
  97. }
  98. }
  99. /**
  100. * Returns a date of birth within the specified age range
  101. *
  102. * @param int $min minimum age in years
  103. * @param int $max maximum age in years
  104. * @return string Dob a db (ISO) format datetime string
  105. */
  106. public static function dob($min = 18, $max = 100) {
  107. $dobYear = date('Y') - (static::int($min, $max));
  108. $dobMonth = static::int(1, 12);
  109. if ($dobMonth == 2) {
  110. // leap year?
  111. if ($dobYear % 4 || $dobYear % 400) {
  112. $maxDays = 29;
  113. } else {
  114. $maxDays = 28;
  115. }
  116. } elseif (in_array($dobMonth, [4, 6, 9, 11])) {
  117. $maxDays = 30;
  118. } else {
  119. $maxDays = 31;
  120. }
  121. $dobDay = static::int(1, $maxDays);
  122. $dob = sprintf('%4d-%02d-%02d', $dobYear, $dobMonth, $dobDay);
  123. return $dob;
  124. }
  125. /**
  126. * Generates a password
  127. *
  128. * @param int $length Password length
  129. * @return string
  130. * @link https://github.com/CakeDC/users/blob/master/models/user.php#L498
  131. */
  132. public static function pronounceablePwd($length = 10) {
  133. srand((double)microtime() * 1000000);
  134. $password = '';
  135. $vowels = ['a', 'e', 'i', 'o', 'u'];
  136. $cons = ['b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'tr',
  137. 'cr', 'br', 'fr', 'th', 'dr', 'ch', 'ph', 'wr', 'st', 'sp', 'sw', 'pr', 'sl', 'cl'];
  138. for ($i = 0; $i < $length; $i++) {
  139. $password .= $cons[mt_rand(0, 31)] . $vowels[mt_rand(0, 4)];
  140. }
  141. return substr($password, 0, $length);
  142. }
  143. /**
  144. * Generates random passwords.
  145. *
  146. * @param int $length (necessary!)
  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. }