PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Faker/Provider/Base.php

https://github.com/gobb/Faker
PHP | 188 lines | 78 code | 22 blank | 88 comment | 5 complexity | ee44c46deaa35560d9f2ea6645df5d1e 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. return $array[mt_rand(0, count($array) - 1)];
  112. }
  113. /**
  114. * Replaces all hash sign ('#') occurrences with a random number
  115. * Replaces all percentage sign ('%') occurrences with a not null number
  116. *
  117. * @param string $string String that needs to bet parsed
  118. * @return string
  119. */
  120. public static function numerify($string = '###')
  121. {
  122. $string = preg_replace_callback('/\#/', 'static::randomDigit', $string);
  123. $string = preg_replace_callback('/\%/', 'static::randomDigitNotNull', $string);
  124. return $string;
  125. }
  126. /**
  127. * Replaces all question mark ('?') occurrences with a random letter
  128. *
  129. * @param string $string String that needs to bet parsed
  130. * @return string
  131. */
  132. public static function lexify($string = '????')
  133. {
  134. return preg_replace_callback('/\?/', 'static::randomLetter', $string);
  135. }
  136. /**
  137. * Replaces hash signs and question marks with random numbers and letters
  138. *
  139. * @param string $string String that needs to bet parsed
  140. * @return string
  141. */
  142. public static function bothify($string = '## ??')
  143. {
  144. return static::lexify(static::numerify($string));
  145. }
  146. /**
  147. * Converts string to lowercase.
  148. * Uses mb_string extension if available
  149. * @param string $string String that should be converted to lowercase
  150. * @return string
  151. */
  152. public static function toLower($string = '')
  153. {
  154. return extension_loaded('mbstring') ? mb_strtolower($string, 'UTF-8') : strtolower($string);
  155. }
  156. /**
  157. * Converts string to uppercase.
  158. * Uses mb_string extension if available
  159. * @param string $string String that should be converted to uppercase
  160. * @return string
  161. */
  162. public static function toUpper($string = '')
  163. {
  164. return extension_loaded('mbstring') ? mb_strtoupper($string, 'UTF-8') : strtoupper($string);
  165. }
  166. }