ar_EG
1 year ago
ar_JO
1 year ago
ar_SA
1 year ago
at_AT
1 year ago
bg_BG
1 year ago
bn_BD
1 year ago
cs_CZ
1 year ago
da_DK
1 year ago
de_AT
1 year ago
de_CH
1 year ago
de_DE
1 year ago
el_CY
1 year ago
el_GR
1 year ago
en_AU
1 year ago
en_CA
1 year ago
en_GB
1 year ago
en_HK
1 year ago
en_IN
1 year ago
en_NG
1 year ago
en_NZ
1 year ago
en_PH
1 year ago
en_SG
1 year ago
en_UG
1 year ago
en_US
1 year ago
en_ZA
1 year ago
es_AR
1 year ago
es_ES
1 year ago
es_PE
1 year ago
es_VE
1 year ago
et_EE
1 year ago
fa_IR
1 year ago
fi_FI
1 year ago
fr_BE
1 year ago
fr_CA
1 year ago
fr_CH
1 year ago
fr_FR
1 year ago
he_IL
1 year ago
hr_HR
1 year ago
hu_HU
1 year ago
hy_AM
1 year ago
id_ID
1 year ago
is_IS
1 year ago
it_CH
1 year ago
it_IT
1 year ago
ja_JP
1 year ago
ka_GE
1 year ago
kk_KZ
1 year ago
ko_KR
1 year ago
lt_LT
1 year ago
lv_LV
1 year ago
me_ME
1 year ago
mn_MN
1 year ago
ms_MY
1 year ago
nb_NO
1 year ago
ne_NP
1 year ago
nl_BE
1 year ago
nl_NL
1 year ago
pl_PL
1 year ago
pt_BR
1 year ago
pt_PT
1 year ago
ro_MD
1 year ago
ro_RO
1 year ago
ru_RU
1 year ago
sk_SK
1 year ago
sl_SI
1 year ago
sr_Cyrl_RS
1 year ago
sr_Latn_RS
1 year ago
sr_RS
1 year ago
sv_SE
1 year ago
th_TH
1 year ago
tr_TR
1 year ago
uk_UA
1 year ago
vi_VN
1 year ago
zh_CN
1 year ago
zh_TW
1 year ago
Address.php
1 year ago
Barcode.php
1 year ago
Base.php
1 year ago
Biased.php
1 year ago
Color.php
1 year ago
Company.php
1 year ago
DateTime.php
1 year ago
File.php
1 year ago
HtmlLorem.php
1 year ago
Image.php
1 year ago
Internet.php
1 year ago
Lorem.php
1 year ago
Medical.php
1 year ago
Miscellaneous.php
1 year ago
Payment.php
1 year ago
Person.php
1 year ago
PhoneNumber.php
1 year ago
Text.php
1 year ago
UserAgent.php
1 year ago
Uuid.php
1 year ago
Base.php
668 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @license MIT |
| 4 | * |
| 5 | * Modified by impress-org on 07-August-2024 using Strauss. |
| 6 | * @see https://github.com/BrianHenryIE/strauss |
| 7 | */ |
| 8 | |
| 9 | namespace Give\Vendors\Faker\Provider; |
| 10 | |
| 11 | use Give\Vendors\Faker\DefaultGenerator; |
| 12 | use Give\Vendors\Faker\Generator; |
| 13 | use Give\Vendors\Faker\UniqueGenerator; |
| 14 | use Give\Vendors\Faker\ValidGenerator; |
| 15 | |
| 16 | class Base |
| 17 | { |
| 18 | /** |
| 19 | * @var \Give\Vendors\Faker\Generator |
| 20 | */ |
| 21 | protected $generator; |
| 22 | |
| 23 | /** |
| 24 | * @var \Give\Vendors\Faker\UniqueGenerator |
| 25 | */ |
| 26 | protected $unique; |
| 27 | |
| 28 | public function __construct(Generator $generator) |
| 29 | { |
| 30 | $this->generator = $generator; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns a random number between 0 and 9 |
| 35 | * |
| 36 | * @return int |
| 37 | */ |
| 38 | public static function randomDigit() |
| 39 | { |
| 40 | return mt_rand(0, 9); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns a random number between 1 and 9 |
| 45 | * |
| 46 | * @return int |
| 47 | */ |
| 48 | public static function randomDigitNotNull() |
| 49 | { |
| 50 | return mt_rand(1, 9); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Generates a random digit, which cannot be $except |
| 55 | * |
| 56 | * @param int $except |
| 57 | * |
| 58 | * @return int |
| 59 | */ |
| 60 | public static function randomDigitNot($except) |
| 61 | { |
| 62 | $result = self::numberBetween(0, 8); |
| 63 | |
| 64 | if ($result >= $except) { |
| 65 | ++$result; |
| 66 | } |
| 67 | |
| 68 | return $result; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns a random integer with 0 to $nbDigits digits. |
| 73 | * |
| 74 | * The maximum value returned is mt_getrandmax() |
| 75 | * |
| 76 | * @param int $nbDigits Defaults to a random number between 1 and 9 |
| 77 | * @param bool $strict Whether the returned number should have exactly $nbDigits |
| 78 | * |
| 79 | * @example 79907610 |
| 80 | * |
| 81 | * @return int |
| 82 | */ |
| 83 | public static function randomNumber($nbDigits = null, $strict = false) |
| 84 | { |
| 85 | if (!is_bool($strict)) { |
| 86 | throw new \InvalidArgumentException('randomNumber() generates numbers of fixed width. To generate numbers between two boundaries, use numberBetween() instead.'); |
| 87 | } |
| 88 | |
| 89 | if (null === $nbDigits) { |
| 90 | $nbDigits = static::randomDigitNotNull(); |
| 91 | } |
| 92 | $max = 10 ** $nbDigits - 1; |
| 93 | |
| 94 | if ($max > mt_getrandmax()) { |
| 95 | throw new \InvalidArgumentException('randomNumber() can only generate numbers up to mt_getrandmax()'); |
| 96 | } |
| 97 | |
| 98 | if ($strict) { |
| 99 | return mt_rand(10 ** ($nbDigits - 1), $max); |
| 100 | } |
| 101 | |
| 102 | return mt_rand(0, $max); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Return a random float number |
| 107 | * |
| 108 | * @param int $nbMaxDecimals |
| 109 | * @param float|int $min |
| 110 | * @param float|int $max |
| 111 | * |
| 112 | * @example 48.8932 |
| 113 | * |
| 114 | * @return float |
| 115 | */ |
| 116 | public static function randomFloat($nbMaxDecimals = null, $min = 0, $max = null) |
| 117 | { |
| 118 | if (null === $nbMaxDecimals) { |
| 119 | $nbMaxDecimals = static::randomDigit(); |
| 120 | } |
| 121 | |
| 122 | if (null === $max) { |
| 123 | $max = static::randomNumber(); |
| 124 | |
| 125 | if ($min > $max) { |
| 126 | $max = $min; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if ($min > $max) { |
| 131 | $tmp = $min; |
| 132 | $min = $max; |
| 133 | $max = $tmp; |
| 134 | } |
| 135 | |
| 136 | return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $nbMaxDecimals); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns a random number between $int1 and $int2 (any order) |
| 141 | * |
| 142 | * @param int $int1 default to 0 |
| 143 | * @param int $int2 defaults to 32 bit max integer, ie 2147483647 |
| 144 | * |
| 145 | * @example 79907610 |
| 146 | * |
| 147 | * @return int |
| 148 | */ |
| 149 | public static function numberBetween($int1 = 0, $int2 = 2147483647) |
| 150 | { |
| 151 | $min = $int1 < $int2 ? $int1 : $int2; |
| 152 | $max = $int1 < $int2 ? $int2 : $int1; |
| 153 | |
| 154 | return mt_rand($min, $max); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Returns the passed value |
| 159 | */ |
| 160 | public static function passthrough($value) |
| 161 | { |
| 162 | return $value; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns a random letter from a to z |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | public static function randomLetter() |
| 171 | { |
| 172 | return chr(mt_rand(97, 122)); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns a random ASCII character (excluding accents and special chars) |
| 177 | * |
| 178 | * @return string |
| 179 | */ |
| 180 | public static function randomAscii() |
| 181 | { |
| 182 | return chr(mt_rand(33, 126)); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns randomly ordered subsequence of $count elements from a provided array |
| 187 | * |
| 188 | * @param array $array Array to take elements from. Defaults to a-c |
| 189 | * @param int $count Number of elements to take. |
| 190 | * @param bool $allowDuplicates Allow elements to be picked several times. Defaults to false |
| 191 | * |
| 192 | * @throws \LengthException When requesting more elements than provided |
| 193 | * |
| 194 | * @return array New array with $count elements from $array |
| 195 | */ |
| 196 | public static function randomElements($array = ['a', 'b', 'c'], $count = 1, $allowDuplicates = false) |
| 197 | { |
| 198 | $traversables = []; |
| 199 | |
| 200 | if ($array instanceof \Traversable) { |
| 201 | foreach ($array as $element) { |
| 202 | $traversables[] = $element; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | $arr = count($traversables) ? $traversables : $array; |
| 207 | |
| 208 | $allKeys = array_keys($arr); |
| 209 | $numKeys = count($allKeys); |
| 210 | |
| 211 | if (!$allowDuplicates && $numKeys < $count) { |
| 212 | throw new \LengthException(sprintf('Cannot get %d elements, only %d in array', $count, $numKeys)); |
| 213 | } |
| 214 | |
| 215 | $highKey = $numKeys - 1; |
| 216 | $keys = $elements = []; |
| 217 | $numElements = 0; |
| 218 | |
| 219 | while ($numElements < $count) { |
| 220 | $num = mt_rand(0, $highKey); |
| 221 | |
| 222 | if (!$allowDuplicates) { |
| 223 | if (isset($keys[$num])) { |
| 224 | continue; |
| 225 | } |
| 226 | $keys[$num] = true; |
| 227 | } |
| 228 | |
| 229 | $elements[] = $arr[$allKeys[$num]]; |
| 230 | ++$numElements; |
| 231 | } |
| 232 | |
| 233 | return $elements; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Returns a random element from a passed array |
| 238 | * |
| 239 | * @param array $array |
| 240 | */ |
| 241 | public static function randomElement($array = ['a', 'b', 'c']) |
| 242 | { |
| 243 | if (!$array || ($array instanceof \Traversable && !count($array))) { |
| 244 | return null; |
| 245 | } |
| 246 | $elements = static::randomElements($array, 1); |
| 247 | |
| 248 | return $elements[0]; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns a random key from a passed associative array |
| 253 | * |
| 254 | * @param array $array |
| 255 | * |
| 256 | * @return int|string|null |
| 257 | */ |
| 258 | public static function randomKey($array = []) |
| 259 | { |
| 260 | if (!$array) { |
| 261 | return null; |
| 262 | } |
| 263 | $keys = array_keys($array); |
| 264 | |
| 265 | return $keys[mt_rand(0, count($keys) - 1)]; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns a shuffled version of the argument. |
| 270 | * |
| 271 | * This function accepts either an array, or a string. |
| 272 | * |
| 273 | * @example $faker->shuffle([1, 2, 3]); // [2, 1, 3] |
| 274 | * @example $faker->shuffle('hello, world'); // 'rlo,h eold!lw' |
| 275 | * |
| 276 | * @see shuffleArray() |
| 277 | * @see shuffleString() |
| 278 | * |
| 279 | * @param array|string $arg The set to shuffle |
| 280 | * |
| 281 | * @return array|string The shuffled set |
| 282 | */ |
| 283 | public static function shuffle($arg = '') |
| 284 | { |
| 285 | if (is_array($arg)) { |
| 286 | return static::shuffleArray($arg); |
| 287 | } |
| 288 | |
| 289 | if (is_string($arg)) { |
| 290 | return static::shuffleString($arg); |
| 291 | } |
| 292 | |
| 293 | throw new \InvalidArgumentException('shuffle() only supports strings or arrays'); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Returns a shuffled version of the array. |
| 298 | * |
| 299 | * This function does not mutate the original array. It uses the |
| 300 | * Fisher–Yates algorithm, which is unbiased, together with a Mersenne |
| 301 | * twister random generator. This function is therefore more random than |
| 302 | * PHP's shuffle() function, and it is seedable. |
| 303 | * |
| 304 | * @see http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle |
| 305 | * |
| 306 | * @example $faker->shuffleArray([1, 2, 3]); // [2, 1, 3] |
| 307 | * |
| 308 | * @param array $array The set to shuffle |
| 309 | * |
| 310 | * @return array The shuffled set |
| 311 | */ |
| 312 | public static function shuffleArray($array = []) |
| 313 | { |
| 314 | $shuffledArray = []; |
| 315 | $i = 0; |
| 316 | reset($array); |
| 317 | |
| 318 | foreach ($array as $key => $value) { |
| 319 | if ($i == 0) { |
| 320 | $j = 0; |
| 321 | } else { |
| 322 | $j = mt_rand(0, $i); |
| 323 | } |
| 324 | |
| 325 | if ($j == $i) { |
| 326 | $shuffledArray[] = $value; |
| 327 | } else { |
| 328 | $shuffledArray[] = $shuffledArray[$j]; |
| 329 | $shuffledArray[$j] = $value; |
| 330 | } |
| 331 | ++$i; |
| 332 | } |
| 333 | |
| 334 | return $shuffledArray; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Returns a shuffled version of the string. |
| 339 | * |
| 340 | * This function does not mutate the original string. It uses the |
| 341 | * Fisher–Yates algorithm, which is unbiased, together with a Mersenne |
| 342 | * twister random generator. This function is therefore more random than |
| 343 | * PHP's shuffle() function, and it is seedable. Additionally, it is |
| 344 | * UTF8 safe if the mb extension is available. |
| 345 | * |
| 346 | * @see http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle |
| 347 | * |
| 348 | * @example $faker->shuffleString('hello, world'); // 'rlo,h eold!lw' |
| 349 | * |
| 350 | * @param string $string The set to shuffle |
| 351 | * @param string $encoding The string encoding (defaults to UTF-8) |
| 352 | * |
| 353 | * @return string The shuffled set |
| 354 | */ |
| 355 | public static function shuffleString($string = '', $encoding = 'UTF-8') |
| 356 | { |
| 357 | if (function_exists('mb_strlen')) { |
| 358 | // UTF8-safe str_split() |
| 359 | $array = []; |
| 360 | $strlen = mb_strlen($string, $encoding); |
| 361 | |
| 362 | for ($i = 0; $i < $strlen; ++$i) { |
| 363 | $array[] = mb_substr($string, $i, 1, $encoding); |
| 364 | } |
| 365 | } else { |
| 366 | $array = str_split($string, 1); |
| 367 | } |
| 368 | |
| 369 | return implode('', static::shuffleArray($array)); |
| 370 | } |
| 371 | |
| 372 | private static function replaceWildcard($string, $wildcard = '#', $callback = 'static::randomDigit') |
| 373 | { |
| 374 | if (($pos = strpos($string, $wildcard)) === false) { |
| 375 | return $string; |
| 376 | } |
| 377 | |
| 378 | for ($i = $pos, $last = strrpos($string, $wildcard, $pos) + 1; $i < $last; ++$i) { |
| 379 | if ($string[$i] === $wildcard) { |
| 380 | $string[$i] = call_user_func($callback); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return $string; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Replaces all hash sign ('#') occurrences with a random number |
| 389 | * Replaces all percentage sign ('%') occurrences with a not null number |
| 390 | * |
| 391 | * @param string $string String that needs to bet parsed |
| 392 | * |
| 393 | * @return string |
| 394 | */ |
| 395 | public static function numerify($string = '###') |
| 396 | { |
| 397 | // instead of using randomDigit() several times, which is slow, |
| 398 | // count the number of hashes and generate once a large number |
| 399 | $toReplace = []; |
| 400 | |
| 401 | if (($pos = strpos($string, '#')) !== false) { |
| 402 | for ($i = $pos, $last = strrpos($string, '#', $pos) + 1; $i < $last; ++$i) { |
| 403 | if ($string[$i] === '#') { |
| 404 | $toReplace[] = $i; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if ($nbReplacements = count($toReplace)) { |
| 410 | $maxAtOnce = strlen((string) mt_getrandmax()) - 1; |
| 411 | $numbers = ''; |
| 412 | $i = 0; |
| 413 | |
| 414 | while ($i < $nbReplacements) { |
| 415 | $size = min($nbReplacements - $i, $maxAtOnce); |
| 416 | $numbers .= str_pad(static::randomNumber($size), $size, '0', STR_PAD_LEFT); |
| 417 | $i += $size; |
| 418 | } |
| 419 | |
| 420 | for ($i = 0; $i < $nbReplacements; ++$i) { |
| 421 | $string[$toReplace[$i]] = $numbers[$i]; |
| 422 | } |
| 423 | } |
| 424 | $string = self::replaceWildcard($string, '%', 'static::randomDigitNotNull'); |
| 425 | |
| 426 | return $string; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Replaces all question mark ('?') occurrences with a random letter |
| 431 | * |
| 432 | * @param string $string String that needs to bet parsed |
| 433 | * |
| 434 | * @return string |
| 435 | */ |
| 436 | public static function lexify($string = '????') |
| 437 | { |
| 438 | return self::replaceWildcard($string, '?', 'static::randomLetter'); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Replaces hash signs ('#') and question marks ('?') with random numbers and letters |
| 443 | * An asterisk ('*') is replaced with either a random number or a random letter |
| 444 | * |
| 445 | * @param string $string String that needs to be parsed |
| 446 | * |
| 447 | * @return string |
| 448 | */ |
| 449 | public static function bothify($string = '## ??') |
| 450 | { |
| 451 | $string = self::replaceWildcard($string, '*', static function () { |
| 452 | return mt_rand(0, 1) ? '#' : '?'; |
| 453 | }); |
| 454 | |
| 455 | return static::lexify(static::numerify($string)); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Replaces * signs with random numbers and letters and special characters |
| 460 | * |
| 461 | * @example $faker->asciify(''********'); // "s5'G!uC3" |
| 462 | * |
| 463 | * @param string $string String that needs to bet parsed |
| 464 | * |
| 465 | * @return string |
| 466 | */ |
| 467 | public static function asciify($string = '****') |
| 468 | { |
| 469 | return preg_replace_callback('/\*/u', 'static::randomAscii', $string); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Transforms a basic regular expression into a random string satisfying the expression. |
| 474 | * |
| 475 | * @example $faker->regexify('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'); // sm0@y8k96a.ej |
| 476 | * |
| 477 | * Regex delimiters '/.../' and begin/end markers '^...$' are ignored. |
| 478 | * |
| 479 | * Only supports a small subset of the regex syntax. For instance, |
| 480 | * unicode, negated classes, unbounded ranges, subpatterns, back references, |
| 481 | * assertions, recursive patterns, and comments are not supported. Escaping |
| 482 | * support is extremely fragile. |
| 483 | * |
| 484 | * This method is also VERY slow. Use it only when no other formatter |
| 485 | * can generate the fake data you want. For instance, prefer calling |
| 486 | * `$faker->email` rather than `regexify` with the previous regular |
| 487 | * expression. |
| 488 | * |
| 489 | * Also note than `bothify` can probably do most of what this method does, |
| 490 | * but much faster. For instance, for a dummy email generation, try |
| 491 | * `$faker->bothify('?????????@???.???')`. |
| 492 | * |
| 493 | * @see https://github.com/icomefromthenet/ReverseRegex for a more robust implementation |
| 494 | * |
| 495 | * @param string $regex A regular expression (delimiters are optional) |
| 496 | * |
| 497 | * @return string |
| 498 | */ |
| 499 | public static function regexify($regex = '') |
| 500 | { |
| 501 | // ditch the anchors |
| 502 | $regex = preg_replace('/^\/?\^?/', '', $regex); |
| 503 | $regex = preg_replace('/\$?\/?$/', '', $regex); |
| 504 | // All {2} become {2,2} |
| 505 | $regex = preg_replace('/\{(\d+)\}/', '{\1,\1}', $regex); |
| 506 | // Single-letter quantifiers (?, *, +) become bracket quantifiers ({0,1}, {0,rand}, {1, rand}) |
| 507 | $regex = preg_replace('/(?<!\\\)\?/', '{0,1}', $regex); |
| 508 | $regex = preg_replace('/(?<!\\\)\*/', '{0,' . static::randomDigitNotNull() . '}', $regex); |
| 509 | $regex = preg_replace('/(?<!\\\)\+/', '{1,' . static::randomDigitNotNull() . '}', $regex); |
| 510 | // [12]{1,2} becomes [12] or [12][12] |
| 511 | $regex = preg_replace_callback('/(\[[^\]]+\])\{(\d+),(\d+)\}/', static function ($matches) { |
| 512 | return str_repeat($matches[1], Base::randomElement(range($matches[2], $matches[3]))); |
| 513 | }, $regex); |
| 514 | // (12|34){1,2} becomes (12|34) or (12|34)(12|34) |
| 515 | $regex = preg_replace_callback('/(\([^\)]+\))\{(\d+),(\d+)\}/', static function ($matches) { |
| 516 | return str_repeat($matches[1], Base::randomElement(range($matches[2], $matches[3]))); |
| 517 | }, $regex); |
| 518 | // A{1,2} becomes A or AA or \d{3} becomes \d\d\d |
| 519 | $regex = preg_replace_callback('/(\\\?.)\{(\d+),(\d+)\}/', static function ($matches) { |
| 520 | return str_repeat($matches[1], Base::randomElement(range($matches[2], $matches[3]))); |
| 521 | }, $regex); |
| 522 | // (this|that) becomes 'this' or 'that' |
| 523 | $regex = preg_replace_callback('/\((.*?)\)/', static function ($matches) { |
| 524 | return Base::randomElement(explode('|', str_replace(['(', ')'], '', $matches[1]))); |
| 525 | }, $regex); |
| 526 | // All A-F inside of [] become ABCDEF |
| 527 | $regex = preg_replace_callback('/\[([^\]]+)\]/', static function ($matches) { |
| 528 | return '[' . preg_replace_callback('/(\w|\d)\-(\w|\d)/', static function ($range) { |
| 529 | return implode('', range($range[1], $range[2])); |
| 530 | }, $matches[1]) . ']'; |
| 531 | }, $regex); |
| 532 | // All [ABC] become B (or A or C) |
| 533 | $regex = preg_replace_callback('/\[([^\]]+)\]/', static function ($matches) { |
| 534 | // remove backslashes (that are not followed by another backslash) because they are escape characters |
| 535 | $match = preg_replace('/\\\(?!\\\)/', '', $matches[1]); |
| 536 | $randomElement = Base::randomElement(str_split($match)); |
| 537 | //[.] should not be a random character, but a literal . |
| 538 | return str_replace('.', '\.', $randomElement); |
| 539 | }, $regex); |
| 540 | // replace \d with number and \w with letter and . with ascii |
| 541 | $regex = preg_replace_callback('/\\\w/', 'static::randomLetter', $regex); |
| 542 | $regex = preg_replace_callback('/\\\d/', 'static::randomDigit', $regex); |
| 543 | //replace . with ascii except backslash |
| 544 | $regex = preg_replace_callback('/(?<!\\\)\./', static function () { |
| 545 | $chr = static::asciify('*'); |
| 546 | |
| 547 | if ($chr === '\\') { |
| 548 | $chr .= '\\'; |
| 549 | } |
| 550 | |
| 551 | return $chr; |
| 552 | }, $regex); |
| 553 | // remove remaining single backslashes |
| 554 | $regex = str_replace('\\\\', '[:escaped_backslash:]', $regex); |
| 555 | $regex = str_replace('\\', '', $regex); |
| 556 | $regex = str_replace('[:escaped_backslash:]', '\\', $regex); |
| 557 | |
| 558 | // phew |
| 559 | return $regex; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Converts string to lowercase. |
| 564 | * Uses mb_string extension if available. |
| 565 | * |
| 566 | * @param string $string String that should be converted to lowercase |
| 567 | * |
| 568 | * @return string |
| 569 | */ |
| 570 | public static function toLower($string = '') |
| 571 | { |
| 572 | return extension_loaded('mbstring') ? mb_strtolower($string, 'UTF-8') : strtolower($string); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Converts string to uppercase. |
| 577 | * Uses mb_string extension if available. |
| 578 | * |
| 579 | * @param string $string String that should be converted to uppercase |
| 580 | * |
| 581 | * @return string |
| 582 | */ |
| 583 | public static function toUpper($string = '') |
| 584 | { |
| 585 | return extension_loaded('mbstring') ? mb_strtoupper($string, 'UTF-8') : strtoupper($string); |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Chainable method for making any formatter optional. |
| 590 | * |
| 591 | * @param float|int $weight Set the probability of receiving a null value. |
| 592 | * "0" will always return null, "1" will always return the generator. |
| 593 | * If $weight is an integer value, then the same system works |
| 594 | * between 0 (always get false) and 100 (always get true). |
| 595 | * |
| 596 | * @return mixed|null |
| 597 | */ |
| 598 | public function optional($weight = 0.5, $default = null) |
| 599 | { |
| 600 | // old system based on 0.1 <= $weight <= 0.9 |
| 601 | // TODO: remove in v2 |
| 602 | if ($weight > 0 && $weight < 1 && mt_rand() / mt_getrandmax() <= $weight) { |
| 603 | return $this->generator; |
| 604 | } |
| 605 | |
| 606 | // new system with percentage |
| 607 | if (is_int($weight) && mt_rand(1, 100) <= $weight) { |
| 608 | return $this->generator; |
| 609 | } |
| 610 | |
| 611 | return new DefaultGenerator($default); |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Chainable method for making any formatter unique. |
| 616 | * |
| 617 | * <code> |
| 618 | * // will never return twice the same value |
| 619 | * $faker->unique()->randomElement(array(1, 2, 3)); |
| 620 | * </code> |
| 621 | * |
| 622 | * @param bool $reset If set to true, resets the list of existing values |
| 623 | * @param int $maxRetries Maximum number of retries to find a unique value, |
| 624 | * After which an OverflowException is thrown. |
| 625 | * |
| 626 | * @throws \OverflowException When no unique value can be found by iterating $maxRetries times |
| 627 | * |
| 628 | * @return UniqueGenerator A proxy class returning only non-existing values |
| 629 | */ |
| 630 | public function unique($reset = false, $maxRetries = 10000) |
| 631 | { |
| 632 | if ($reset || !$this->unique) { |
| 633 | $this->unique = new UniqueGenerator($this->generator, $maxRetries); |
| 634 | } |
| 635 | |
| 636 | return $this->unique; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Chainable method for forcing any formatter to return only valid values. |
| 641 | * |
| 642 | * The value validity is determined by a function passed as first argument. |
| 643 | * |
| 644 | * <code> |
| 645 | * $values = array(); |
| 646 | * $evenValidator = function ($digit) { |
| 647 | * return $digit % 2 === 0; |
| 648 | * }; |
| 649 | * for ($i=0; $i < 10; $i++) { |
| 650 | * $values []= $faker->valid($evenValidator)->randomDigit; |
| 651 | * } |
| 652 | * print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6] |
| 653 | * </code> |
| 654 | * |
| 655 | * @param Closure $validator A function returning true for valid values |
| 656 | * @param int $maxRetries Maximum number of retries to find a unique value, |
| 657 | * After which an OverflowException is thrown. |
| 658 | * |
| 659 | * @throws \OverflowException When no valid value can be found by iterating $maxRetries times |
| 660 | * |
| 661 | * @return ValidGenerator A proxy class returning only valid values |
| 662 | */ |
| 663 | public function valid($validator = null, $maxRetries = 10000) |
| 664 | { |
| 665 | return new ValidGenerator($this->generator, $validator, $maxRetries); |
| 666 | } |
| 667 | } |
| 668 |