PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Faker/Provider/Base.php

https://github.com/JhonnyL/Faker
PHP | 191 lines | 80 code | 23 blank | 88 comment | 5 complexity | 47a02ef55189a886fab5399fc4fb8aad MD5 | raw file
  1. <?php
  2. namespace Faker\Provider;
  3. use Faker\Generator;
  4. class Base
  5. {
  6. /**
  7. * @var \Faker\Generator
  8. */
  9. protected $generator;
  10. /**
  11. * @param \Faker\Generator $generator
  12. */
  13. public function __construct(Generator $generator)
  14. {
  15. $this->generator = $generator;
  16. }
  17. /**
  18. * Returns a random number between 0 and 9
  19. *
  20. * @return integer
  21. */
  22. public static function randomDigit()
  23. {
  24. return mt_rand(0, 9);
  25. }
  26. /**
  27. * Returns a random number between 1 and 9
  28. *
  29. * @return integer
  30. */
  31. public static function randomDigitNotNull()
  32. {
  33. return mt_rand(1, 9);
  34. }
  35. /**
  36. * Returns a random number with 0 to $nbDigits digits
  37. *
  38. * If $upTo is passed, it returns a number between $nbDigits (read as from) and $upTo
  39. *
  40. * @param integer $nbDigits
  41. * @param integer $upTo
  42. * @example 79907610
  43. *
  44. * @return integer
  45. */
  46. public static function randomNumber($nbDigits = null, $upTo = null)
  47. {
  48. if (null === $nbDigits) {
  49. $nbDigits = static::randomDigit();
  50. }
  51. if (null !== $upTo) {
  52. return static::numberBetween($nbDigits, $upTo);
  53. }
  54. return mt_rand(0, pow(10, $nbDigits) - 1);
  55. }
  56. /**
  57. * Return a random float number
  58. *
  59. * @param int $nbMaxDecimals
  60. * @param int|float $min
  61. * @param int|float $max
  62. * @example 48.8932
  63. *
  64. * @return float
  65. */
  66. public static function randomFloat($nbMaxDecimals = null, $min = 0, $max = null)
  67. {
  68. if (null === $nbMaxDecimals) {
  69. $nbMaxDecimals = static::randomDigit();
  70. }
  71. if (null === $max) {
  72. $max = static::randomNumber();
  73. }
  74. if ($min > $max) {
  75. $tmp = $min;
  76. $min = $max;
  77. $max = $tmp;
  78. }
  79. return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $nbMaxDecimals);
  80. }
  81. /**
  82. * Returns a random number between $from and $to
  83. *
  84. * @param integer $from
  85. * @param integer $to
  86. * @example 79907610
  87. *
  88. * @return integer
  89. */
  90. public static function numberBetween($from = null, $to = null)
  91. {
  92. return mt_rand($from ?: 0, $to ?: 2147483647); // 32bit compat default
  93. }
  94. /**
  95. * Returns a random letter from a to z
  96. *
  97. * @return string
  98. */
  99. public static function randomLetter()
  100. {
  101. return chr(mt_rand(97, 122));
  102. }
  103. /**
  104. * Returns a random element from a passed array
  105. *
  106. * @param array $array
  107. * @return mixed
  108. */
  109. public static function randomElement($array = array('a', 'b', 'c'))
  110. {
  111. $keys = array_keys($array);
  112. $key = $keys[mt_rand(0, count($keys) - 1)];
  113. return $array[$key];
  114. }
  115. /**
  116. * Replaces all hash sign ('#') occurrences with a random number
  117. * Replaces all percentage sign ('%') occurrences with a not null number
  118. *
  119. * @param string $string String that needs to bet parsed
  120. * @return string
  121. */
  122. public static function numerify($string = '###')
  123. {
  124. $string = preg_replace_callback('/\#/u', 'static::randomDigit', $string);
  125. $string = preg_replace_callback('/\%/u', 'static::randomDigitNotNull', $string);
  126. return $string;
  127. }
  128. /**
  129. * Replaces all question mark ('?') occurrences with a random letter
  130. *
  131. * @param string $string String that needs to bet parsed
  132. * @return string
  133. */
  134. public static function lexify($string = '????')
  135. {
  136. return preg_replace_callback('/\?/u', 'static::randomLetter', $string);
  137. }
  138. /**
  139. * Replaces hash signs and question marks with random numbers and letters
  140. *
  141. * @param string $string String that needs to bet parsed
  142. * @return string
  143. */
  144. public static function bothify($string = '## ??')
  145. {
  146. return static::lexify(static::numerify($string));
  147. }
  148. /**
  149. * Converts string to lowercase.
  150. * Uses mb_string extension if available
  151. * @param string $string String that should be converted to lowercase
  152. * @return string
  153. */
  154. public static function toLower($string = '')
  155. {
  156. return extension_loaded('mbstring') ? mb_strtolower($string, 'UTF-8') : strtolower($string);
  157. }
  158. /**
  159. * Converts string to uppercase.
  160. * Uses mb_string extension if available
  161. * @param string $string String that should be converted to uppercase
  162. * @return string
  163. */
  164. public static function toUpper($string = '')
  165. {
  166. return extension_loaded('mbstring') ? mb_strtoupper($string, 'UTF-8') : strtoupper($string);
  167. }
  168. }