Calculator
2 years ago
Container
2 years ago
Core
2 years ago
Extension
2 years ago
Guesser
2 years ago
ORM
2 years ago
Provider
2 years ago
ChanceGenerator.php
2 years ago
DefaultGenerator.php
2 years ago
Documentor.php
2 years ago
Factory.php
2 years ago
Generator.php
2 years ago
UniqueGenerator.php
2 years ago
ValidGenerator.php
2 years ago
Generator.php
980 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @license MIT |
| 4 | * |
| 5 | * Modified by impress-org on 30-October-2023 using Strauss. |
| 6 | * @see https://github.com/BrianHenryIE/strauss |
| 7 | */ |
| 8 | |
| 9 | namespace Give\Vendors\Faker; |
| 10 | |
| 11 | use Give\Vendors\Faker\Container\ContainerInterface; |
| 12 | |
| 13 | /** |
| 14 | * @property string $citySuffix |
| 15 | * |
| 16 | * @method string citySuffix() |
| 17 | * |
| 18 | * @property string $streetSuffix |
| 19 | * |
| 20 | * @method string streetSuffix() |
| 21 | * |
| 22 | * @property string $buildingNumber |
| 23 | * |
| 24 | * @method string buildingNumber() |
| 25 | * |
| 26 | * @property string $city |
| 27 | * |
| 28 | * @method string city() |
| 29 | * |
| 30 | * @property string $streetName |
| 31 | * |
| 32 | * @method string streetName() |
| 33 | * |
| 34 | * @property string $streetAddress |
| 35 | * |
| 36 | * @method string streetAddress() |
| 37 | * |
| 38 | * @property string $postcode |
| 39 | * |
| 40 | * @method string postcode() |
| 41 | * |
| 42 | * @property string $address |
| 43 | * |
| 44 | * @method string address() |
| 45 | * |
| 46 | * @property string $country |
| 47 | * |
| 48 | * @method string country() |
| 49 | * |
| 50 | * @property float $latitude |
| 51 | * |
| 52 | * @method float latitude($min = -90, $max = 90) |
| 53 | * |
| 54 | * @property float $longitude |
| 55 | * |
| 56 | * @method float longitude($min = -180, $max = 180) |
| 57 | * |
| 58 | * @property float[] $localCoordinates |
| 59 | * |
| 60 | * @method float[] localCoordinates() |
| 61 | * |
| 62 | * @property int $randomDigitNotNull |
| 63 | * |
| 64 | * @method int randomDigitNotNull() |
| 65 | * |
| 66 | * @property mixed $passthrough |
| 67 | * |
| 68 | * @method mixed passthrough($value) |
| 69 | * |
| 70 | * @property string $randomLetter |
| 71 | * |
| 72 | * @method string randomLetter() |
| 73 | * |
| 74 | * @property string $randomAscii |
| 75 | * |
| 76 | * @method string randomAscii() |
| 77 | * |
| 78 | * @property array $randomElements |
| 79 | * |
| 80 | * @method array randomElements($array = ['a', 'b', 'c'], $count = 1, $allowDuplicates = false) |
| 81 | * |
| 82 | * @property mixed $randomElement |
| 83 | * |
| 84 | * @method mixed randomElement($array = ['a', 'b', 'c']) |
| 85 | * |
| 86 | * @property int|string|null $randomKey |
| 87 | * |
| 88 | * @method int|string|null randomKey($array = []) |
| 89 | * |
| 90 | * @property array|string $shuffle |
| 91 | * |
| 92 | * @method array|string shuffle($arg = '') |
| 93 | * |
| 94 | * @property array $shuffleArray |
| 95 | * |
| 96 | * @method array shuffleArray($array = []) |
| 97 | * |
| 98 | * @property string $shuffleString |
| 99 | * |
| 100 | * @method string shuffleString($string = '', $encoding = 'UTF-8') |
| 101 | * |
| 102 | * @property string $numerify |
| 103 | * |
| 104 | * @method string numerify($string = '###') |
| 105 | * |
| 106 | * @property string $lexify |
| 107 | * |
| 108 | * @method string lexify($string = '????') |
| 109 | * |
| 110 | * @property string $bothify |
| 111 | * |
| 112 | * @method string bothify($string = '## ??') |
| 113 | * |
| 114 | * @property string $asciify |
| 115 | * |
| 116 | * @method string asciify($string = '****') |
| 117 | * |
| 118 | * @property string $regexify |
| 119 | * |
| 120 | * @method string regexify($regex = '') |
| 121 | * |
| 122 | * @property string $toLower |
| 123 | * |
| 124 | * @method string toLower($string = '') |
| 125 | * |
| 126 | * @property string $toUpper |
| 127 | * |
| 128 | * @method string toUpper($string = '') |
| 129 | * |
| 130 | * @property int $biasedNumberBetween |
| 131 | * |
| 132 | * @method int biasedNumberBetween($min = 0, $max = 100, $function = 'sqrt') |
| 133 | * |
| 134 | * @property string $hexColor |
| 135 | * |
| 136 | * @method string hexColor() |
| 137 | * |
| 138 | * @property string $safeHexColor |
| 139 | * |
| 140 | * @method string safeHexColor() |
| 141 | * |
| 142 | * @property array $rgbColorAsArray |
| 143 | * |
| 144 | * @method array rgbColorAsArray() |
| 145 | * |
| 146 | * @property string $rgbColor |
| 147 | * |
| 148 | * @method string rgbColor() |
| 149 | * |
| 150 | * @property string $rgbCssColor |
| 151 | * |
| 152 | * @method string rgbCssColor() |
| 153 | * |
| 154 | * @property string $rgbaCssColor |
| 155 | * |
| 156 | * @method string rgbaCssColor() |
| 157 | * |
| 158 | * @property string $safeColorName |
| 159 | * |
| 160 | * @method string safeColorName() |
| 161 | * |
| 162 | * @property string $colorName |
| 163 | * |
| 164 | * @method string colorName() |
| 165 | * |
| 166 | * @property string $hslColor |
| 167 | * |
| 168 | * @method string hslColor() |
| 169 | * |
| 170 | * @property array $hslColorAsArray |
| 171 | * |
| 172 | * @method array hslColorAsArray() |
| 173 | * |
| 174 | * @property string $company |
| 175 | * |
| 176 | * @method string company() |
| 177 | * |
| 178 | * @property string $companySuffix |
| 179 | * |
| 180 | * @method string companySuffix() |
| 181 | * |
| 182 | * @property string $jobTitle |
| 183 | * |
| 184 | * @method string jobTitle() |
| 185 | * |
| 186 | * @property int $unixTime |
| 187 | * |
| 188 | * @method int unixTime($max = 'now') |
| 189 | * |
| 190 | * @property \DateTime $dateTime |
| 191 | * |
| 192 | * @method \DateTime dateTime($max = 'now', $timezone = null) |
| 193 | * |
| 194 | * @property \DateTime $dateTimeAD |
| 195 | * |
| 196 | * @method \DateTime dateTimeAD($max = 'now', $timezone = null) |
| 197 | * |
| 198 | * @property string $iso8601 |
| 199 | * |
| 200 | * @method string iso8601($max = 'now') |
| 201 | * |
| 202 | * @property string $date |
| 203 | * |
| 204 | * @method string date($format = 'Y-m-d', $max = 'now') |
| 205 | * |
| 206 | * @property string $time |
| 207 | * |
| 208 | * @method string time($format = 'H:i:s', $max = 'now') |
| 209 | * |
| 210 | * @property \DateTime $dateTimeBetween |
| 211 | * |
| 212 | * @method \DateTime dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null) |
| 213 | * |
| 214 | * @property \DateTime $dateTimeInInterval |
| 215 | * |
| 216 | * @method \DateTime dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = null) |
| 217 | * |
| 218 | * @property \DateTime $dateTimeThisCentury |
| 219 | * |
| 220 | * @method \DateTime dateTimeThisCentury($max = 'now', $timezone = null) |
| 221 | * |
| 222 | * @property \DateTime $dateTimeThisDecade |
| 223 | * |
| 224 | * @method \DateTime dateTimeThisDecade($max = 'now', $timezone = null) |
| 225 | * |
| 226 | * @property \DateTime $dateTimeThisYear |
| 227 | * |
| 228 | * @method \DateTime dateTimeThisYear($max = 'now', $timezone = null) |
| 229 | * |
| 230 | * @property \DateTime $dateTimeThisMonth |
| 231 | * |
| 232 | * @method \DateTime dateTimeThisMonth($max = 'now', $timezone = null) |
| 233 | * |
| 234 | * @property string $amPm |
| 235 | * |
| 236 | * @method string amPm($max = 'now') |
| 237 | * |
| 238 | * @property string $dayOfMonth |
| 239 | * |
| 240 | * @method string dayOfMonth($max = 'now') |
| 241 | * |
| 242 | * @property string $dayOfWeek |
| 243 | * |
| 244 | * @method string dayOfWeek($max = 'now') |
| 245 | * |
| 246 | * @property string $month |
| 247 | * |
| 248 | * @method string month($max = 'now') |
| 249 | * |
| 250 | * @property string $monthName |
| 251 | * |
| 252 | * @method string monthName($max = 'now') |
| 253 | * |
| 254 | * @property string $year |
| 255 | * |
| 256 | * @method string year($max = 'now') |
| 257 | * |
| 258 | * @property string $century |
| 259 | * |
| 260 | * @method string century() |
| 261 | * |
| 262 | * @property string $timezone |
| 263 | * |
| 264 | * @method string timezone($countryCode = null) |
| 265 | * |
| 266 | * @property void $setDefaultTimezone |
| 267 | * |
| 268 | * @method void setDefaultTimezone($timezone = null) |
| 269 | * |
| 270 | * @property string $getDefaultTimezone |
| 271 | * |
| 272 | * @method string getDefaultTimezone() |
| 273 | * |
| 274 | * @property string $file |
| 275 | * |
| 276 | * @method string file($sourceDirectory = '/tmp', $targetDirectory = '/tmp', $fullPath = true) |
| 277 | * |
| 278 | * @property string $randomHtml |
| 279 | * |
| 280 | * @method string randomHtml($maxDepth = 4, $maxWidth = 4) |
| 281 | * |
| 282 | * @property string $imageUrl |
| 283 | * |
| 284 | * @method string imageUrl($width = 640, $height = 480, $category = null, $randomize = true, $word = null, $gray = false) |
| 285 | * |
| 286 | * @property string $image |
| 287 | * |
| 288 | * @method string image($dir = null, $width = 640, $height = 480, $category = null, $fullPath = true, $randomize = true, $word = null, $gray = false) |
| 289 | * |
| 290 | * @property string $email |
| 291 | * |
| 292 | * @method string email() |
| 293 | * |
| 294 | * @property string $safeEmail |
| 295 | * |
| 296 | * @method string safeEmail() |
| 297 | * |
| 298 | * @property string $freeEmail |
| 299 | * |
| 300 | * @method string freeEmail() |
| 301 | * |
| 302 | * @property string $companyEmail |
| 303 | * |
| 304 | * @method string companyEmail() |
| 305 | * |
| 306 | * @property string $freeEmailDomain |
| 307 | * |
| 308 | * @method string freeEmailDomain() |
| 309 | * |
| 310 | * @property string $safeEmailDomain |
| 311 | * |
| 312 | * @method string safeEmailDomain() |
| 313 | * |
| 314 | * @property string $userName |
| 315 | * |
| 316 | * @method string userName() |
| 317 | * |
| 318 | * @property string $password |
| 319 | * |
| 320 | * @method string password($minLength = 6, $maxLength = 20) |
| 321 | * |
| 322 | * @property string $domainName |
| 323 | * |
| 324 | * @method string domainName() |
| 325 | * |
| 326 | * @property string $domainWord |
| 327 | * |
| 328 | * @method string domainWord() |
| 329 | * |
| 330 | * @property string $tld |
| 331 | * |
| 332 | * @method string tld() |
| 333 | * |
| 334 | * @property string $url |
| 335 | * |
| 336 | * @method string url() |
| 337 | * |
| 338 | * @property string $slug |
| 339 | * |
| 340 | * @method string slug($nbWords = 6, $variableNbWords = true) |
| 341 | * |
| 342 | * @property string $ipv4 |
| 343 | * |
| 344 | * @method string ipv4() |
| 345 | * |
| 346 | * @property string $ipv6 |
| 347 | * |
| 348 | * @method string ipv6() |
| 349 | * |
| 350 | * @property string $localIpv4 |
| 351 | * |
| 352 | * @method string localIpv4() |
| 353 | * |
| 354 | * @property string $macAddress |
| 355 | * |
| 356 | * @method string macAddress() |
| 357 | * |
| 358 | * @property string $word |
| 359 | * |
| 360 | * @method string word() |
| 361 | * |
| 362 | * @property array|string $words |
| 363 | * |
| 364 | * @method array|string words($nb = 3, $asText = false) |
| 365 | * |
| 366 | * @property string $sentence |
| 367 | * |
| 368 | * @method string sentence($nbWords = 6, $variableNbWords = true) |
| 369 | * |
| 370 | * @property array|string $sentences |
| 371 | * |
| 372 | * @method array|string sentences($nb = 3, $asText = false) |
| 373 | * |
| 374 | * @property string $paragraph |
| 375 | * |
| 376 | * @method string paragraph($nbSentences = 3, $variableNbSentences = true) |
| 377 | * |
| 378 | * @property array|string $paragraphs |
| 379 | * |
| 380 | * @method array|string paragraphs($nb = 3, $asText = false) |
| 381 | * |
| 382 | * @property string $text |
| 383 | * |
| 384 | * @method string text($maxNbChars = 200) |
| 385 | * |
| 386 | * @property bool $boolean |
| 387 | * |
| 388 | * @method bool boolean($chanceOfGettingTrue = 50) |
| 389 | * |
| 390 | * @property string $md5 |
| 391 | * |
| 392 | * @method string md5() |
| 393 | * |
| 394 | * @property string $sha1 |
| 395 | * |
| 396 | * @method string sha1() |
| 397 | * |
| 398 | * @property string $sha256 |
| 399 | * |
| 400 | * @method string sha256() |
| 401 | * |
| 402 | * @property string $locale |
| 403 | * |
| 404 | * @method string locale() |
| 405 | * |
| 406 | * @property string $countryCode |
| 407 | * |
| 408 | * @method string countryCode() |
| 409 | * |
| 410 | * @property string $countryISOAlpha3 |
| 411 | * |
| 412 | * @method string countryISOAlpha3() |
| 413 | * |
| 414 | * @property string $languageCode |
| 415 | * |
| 416 | * @method string languageCode() |
| 417 | * |
| 418 | * @property string $currencyCode |
| 419 | * |
| 420 | * @method string currencyCode() |
| 421 | * |
| 422 | * @property string $emoji |
| 423 | * |
| 424 | * @method string emoji() |
| 425 | * |
| 426 | * @property string $creditCardType |
| 427 | * |
| 428 | * @method string creditCardType() |
| 429 | * |
| 430 | * @property string $creditCardNumber |
| 431 | * |
| 432 | * @method string creditCardNumber($type = null, $formatted = false, $separator = '-') |
| 433 | * |
| 434 | * @property \DateTime $creditCardExpirationDate |
| 435 | * |
| 436 | * @method \DateTime creditCardExpirationDate($valid = true) |
| 437 | * |
| 438 | * @property string $creditCardExpirationDateString |
| 439 | * |
| 440 | * @method string creditCardExpirationDateString($valid = true, $expirationDateFormat = null) |
| 441 | * |
| 442 | * @property array $creditCardDetails |
| 443 | * |
| 444 | * @method array creditCardDetails($valid = true) |
| 445 | * |
| 446 | * @property string $iban |
| 447 | * |
| 448 | * @method string iban($countryCode = null, $prefix = '', $length = null) |
| 449 | * |
| 450 | * @property string $swiftBicNumber |
| 451 | * |
| 452 | * @method string swiftBicNumber() |
| 453 | * |
| 454 | * @property string $name |
| 455 | * |
| 456 | * @method string name($gender = null) |
| 457 | * |
| 458 | * @property string $firstName |
| 459 | * |
| 460 | * @method string firstName($gender = null) |
| 461 | * |
| 462 | * @property string $firstNameMale |
| 463 | * |
| 464 | * @method string firstNameMale() |
| 465 | * |
| 466 | * @property string $firstNameFemale |
| 467 | * |
| 468 | * @method string firstNameFemale() |
| 469 | * |
| 470 | * @property string $lastName |
| 471 | * |
| 472 | * @method string lastName() |
| 473 | * |
| 474 | * @property string $title |
| 475 | * |
| 476 | * @method string title($gender = null) |
| 477 | * |
| 478 | * @property string $titleMale |
| 479 | * |
| 480 | * @method string titleMale() |
| 481 | * |
| 482 | * @property string $titleFemale |
| 483 | * |
| 484 | * @method string titleFemale() |
| 485 | * |
| 486 | * @property string $phoneNumber |
| 487 | * |
| 488 | * @method string phoneNumber() |
| 489 | * |
| 490 | * @property string $e164PhoneNumber |
| 491 | * |
| 492 | * @method string e164PhoneNumber() |
| 493 | * |
| 494 | * @property int $imei |
| 495 | * |
| 496 | * @method int imei() |
| 497 | * |
| 498 | * @property string $realText |
| 499 | * |
| 500 | * @method string realText($maxNbChars = 200, $indexSize = 2) |
| 501 | * |
| 502 | * @property string $realTextBetween |
| 503 | * |
| 504 | * @method string realTextBetween($minNbChars = 160, $maxNbChars = 200, $indexSize = 2) |
| 505 | * |
| 506 | * @property string $macProcessor |
| 507 | * |
| 508 | * @method string macProcessor() |
| 509 | * |
| 510 | * @property string $linuxProcessor |
| 511 | * |
| 512 | * @method string linuxProcessor() |
| 513 | * |
| 514 | * @property string $userAgent |
| 515 | * |
| 516 | * @method string userAgent() |
| 517 | * |
| 518 | * @property string $chrome |
| 519 | * |
| 520 | * @method string chrome() |
| 521 | * |
| 522 | * @property string $msedge |
| 523 | * |
| 524 | * @method string msedge() |
| 525 | * |
| 526 | * @property string $firefox |
| 527 | * |
| 528 | * @method string firefox() |
| 529 | * |
| 530 | * @property string $safari |
| 531 | * |
| 532 | * @method string safari() |
| 533 | * |
| 534 | * @property string $opera |
| 535 | * |
| 536 | * @method string opera() |
| 537 | * |
| 538 | * @property string $internetExplorer |
| 539 | * |
| 540 | * @method string internetExplorer() |
| 541 | * |
| 542 | * @property string $windowsPlatformToken |
| 543 | * |
| 544 | * @method string windowsPlatformToken() |
| 545 | * |
| 546 | * @property string $macPlatformToken |
| 547 | * |
| 548 | * @method string macPlatformToken() |
| 549 | * |
| 550 | * @property string $iosMobileToken |
| 551 | * |
| 552 | * @method string iosMobileToken() |
| 553 | * |
| 554 | * @property string $linuxPlatformToken |
| 555 | * |
| 556 | * @method string linuxPlatformToken() |
| 557 | * |
| 558 | * @property string $uuid |
| 559 | * |
| 560 | * @method string uuid() |
| 561 | */ |
| 562 | class Generator |
| 563 | { |
| 564 | protected $providers = []; |
| 565 | protected $formatters = []; |
| 566 | |
| 567 | private $container; |
| 568 | |
| 569 | /** |
| 570 | * @var UniqueGenerator |
| 571 | */ |
| 572 | private $uniqueGenerator; |
| 573 | |
| 574 | public function __construct(ContainerInterface $container = null) |
| 575 | { |
| 576 | $this->container = $container ?: Container\ContainerBuilder::getDefault(); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * @template T of Extension\Extension |
| 581 | * |
| 582 | * @param class-string<T> $id |
| 583 | * |
| 584 | * @throws Extension\ExtensionNotFound |
| 585 | * |
| 586 | * @return T |
| 587 | */ |
| 588 | public function ext(string $id): Extension\Extension |
| 589 | { |
| 590 | if (!$this->container->has($id)) { |
| 591 | throw new Extension\ExtensionNotFound(sprintf( |
| 592 | 'No Faker extension with id "%s" was loaded.', |
| 593 | $id |
| 594 | )); |
| 595 | } |
| 596 | |
| 597 | $extension = $this->container->get($id); |
| 598 | |
| 599 | if ($extension instanceof Extension\GeneratorAwareExtension) { |
| 600 | $extension = $extension->withGenerator($this); |
| 601 | } |
| 602 | |
| 603 | return $extension; |
| 604 | } |
| 605 | |
| 606 | public function addProvider($provider) |
| 607 | { |
| 608 | array_unshift($this->providers, $provider); |
| 609 | |
| 610 | $this->formatters = []; |
| 611 | } |
| 612 | |
| 613 | public function getProviders() |
| 614 | { |
| 615 | return $this->providers; |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * With the unique generator you are guaranteed to never get the same two |
| 620 | * values. |
| 621 | * |
| 622 | * <code> |
| 623 | * // will never return twice the same value |
| 624 | * $faker->unique()->randomElement(array(1, 2, 3)); |
| 625 | * </code> |
| 626 | * |
| 627 | * @param bool $reset If set to true, resets the list of existing values |
| 628 | * @param int $maxRetries Maximum number of retries to find a unique value, |
| 629 | * After which an OverflowException is thrown. |
| 630 | * |
| 631 | * @throws \OverflowException When no unique value can be found by iterating $maxRetries times |
| 632 | * |
| 633 | * @return self A proxy class returning only non-existing values |
| 634 | */ |
| 635 | public function unique($reset = false, $maxRetries = 10000) |
| 636 | { |
| 637 | if ($reset || $this->uniqueGenerator === null) { |
| 638 | $this->uniqueGenerator = new UniqueGenerator($this, $maxRetries); |
| 639 | } |
| 640 | |
| 641 | return $this->uniqueGenerator; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Get a value only some percentage of the time. |
| 646 | * |
| 647 | * @param float $weight A probability between 0 and 1, 0 means that we always get the default value. |
| 648 | * |
| 649 | * @return self |
| 650 | */ |
| 651 | public function optional(float $weight = 0.5, $default = null) |
| 652 | { |
| 653 | if ($weight > 1) { |
| 654 | trigger_deprecation('fakerphp/faker', '1.16', 'First argument ($weight) to method "optional()" must be between 0 and 1. You passed %f, we assume you meant %f.', $weight, $weight / 100); |
| 655 | $weight = $weight / 100; |
| 656 | } |
| 657 | |
| 658 | return new ChanceGenerator($this, $weight, $default); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * To make sure the value meet some criteria, pass a callable that verifies the |
| 663 | * output. If the validator fails, the generator will try again. |
| 664 | * |
| 665 | * The value validity is determined by a function passed as first argument. |
| 666 | * |
| 667 | * <code> |
| 668 | * $values = array(); |
| 669 | * $evenValidator = function ($digit) { |
| 670 | * return $digit % 2 === 0; |
| 671 | * }; |
| 672 | * for ($i=0; $i < 10; $i++) { |
| 673 | * $values []= $faker->valid($evenValidator)->randomDigit; |
| 674 | * } |
| 675 | * print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6] |
| 676 | * </code> |
| 677 | * |
| 678 | * @param ?\Closure $validator A function returning true for valid values |
| 679 | * @param int $maxRetries Maximum number of retries to find a valid value, |
| 680 | * After which an OverflowException is thrown. |
| 681 | * |
| 682 | * @throws \OverflowException When no valid value can be found by iterating $maxRetries times |
| 683 | * |
| 684 | * @return self A proxy class returning only valid values |
| 685 | */ |
| 686 | public function valid(?\Closure $validator = null, int $maxRetries = 10000) |
| 687 | { |
| 688 | return new ValidGenerator($this, $validator, $maxRetries); |
| 689 | } |
| 690 | |
| 691 | public function seed($seed = null) |
| 692 | { |
| 693 | if ($seed === null) { |
| 694 | mt_srand(); |
| 695 | } else { |
| 696 | mt_srand((int) $seed, MT_RAND_PHP); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | public function format($format, $arguments = []) |
| 701 | { |
| 702 | return call_user_func_array($this->getFormatter($format), $arguments); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * @param string $format |
| 707 | * |
| 708 | * @return callable |
| 709 | */ |
| 710 | public function getFormatter($format) |
| 711 | { |
| 712 | if (isset($this->formatters[$format])) { |
| 713 | return $this->formatters[$format]; |
| 714 | } |
| 715 | |
| 716 | if (method_exists($this, $format)) { |
| 717 | $this->formatters[$format] = [$this, $format]; |
| 718 | |
| 719 | return $this->formatters[$format]; |
| 720 | } |
| 721 | |
| 722 | // "Give\Vendors\Faker\Core\Barcode->ean13" |
| 723 | if (preg_match('|^([a-zA-Z0-9\\\]+)->([a-zA-Z0-9]+)$|', $format, $matches)) { |
| 724 | $this->formatters[$format] = [$this->ext($matches[1]), $matches[2]]; |
| 725 | |
| 726 | return $this->formatters[$format]; |
| 727 | } |
| 728 | |
| 729 | foreach ($this->providers as $provider) { |
| 730 | if (method_exists($provider, $format)) { |
| 731 | $this->formatters[$format] = [$provider, $format]; |
| 732 | |
| 733 | return $this->formatters[$format]; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | throw new \InvalidArgumentException(sprintf('Unknown format "%s"', $format)); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Replaces tokens ('{{ tokenName }}') with the result from the token method call |
| 742 | * |
| 743 | * @param string $string String that needs to bet parsed |
| 744 | * |
| 745 | * @return string |
| 746 | */ |
| 747 | public function parse($string) |
| 748 | { |
| 749 | $callback = function ($matches) { |
| 750 | return $this->format($matches[1]); |
| 751 | }; |
| 752 | |
| 753 | return preg_replace_callback('/{{\s?(\w+|[\w\\\]+->\w+?)\s?}}/u', $callback, $string); |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * Get a random MIME type |
| 758 | * |
| 759 | * @example 'video/avi' |
| 760 | */ |
| 761 | public function mimeType() |
| 762 | { |
| 763 | return $this->ext(Extension\FileExtension::class)->mimeType(); |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Get a random file extension (without a dot) |
| 768 | * |
| 769 | * @example avi |
| 770 | */ |
| 771 | public function fileExtension() |
| 772 | { |
| 773 | return $this->ext(Extension\FileExtension::class)->extension(); |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Get a full path to a new real file on the system. |
| 778 | */ |
| 779 | public function filePath() |
| 780 | { |
| 781 | return $this->ext(Extension\FileExtension::class)->filePath(); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Get an actual blood type |
| 786 | * |
| 787 | * @example 'AB' |
| 788 | */ |
| 789 | public function bloodType(): string |
| 790 | { |
| 791 | return $this->ext(Extension\BloodExtension::class)->bloodType(); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Get a random resis value |
| 796 | * |
| 797 | * @example '+' |
| 798 | */ |
| 799 | public function bloodRh(): string |
| 800 | { |
| 801 | return $this->ext(Extension\BloodExtension::class)->bloodRh(); |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Get a full blood group |
| 806 | * |
| 807 | * @example 'AB+' |
| 808 | */ |
| 809 | public function bloodGroup(): string |
| 810 | { |
| 811 | return $this->ext(Extension\BloodExtension::class)->bloodGroup(); |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Get a random EAN13 barcode. |
| 816 | * |
| 817 | * @example '4006381333931' |
| 818 | */ |
| 819 | public function ean13(): string |
| 820 | { |
| 821 | return $this->ext(Extension\BarcodeExtension::class)->ean13(); |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Get a random EAN8 barcode. |
| 826 | * |
| 827 | * @example '73513537' |
| 828 | */ |
| 829 | public function ean8(): string |
| 830 | { |
| 831 | return $this->ext(Extension\BarcodeExtension::class)->ean8(); |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * Get a random ISBN-10 code |
| 836 | * |
| 837 | * @see http://en.wikipedia.org/wiki/International_Standard_Book_Number |
| 838 | * |
| 839 | * @example '4881416324' |
| 840 | */ |
| 841 | public function isbn10(): string |
| 842 | { |
| 843 | return $this->ext(Extension\BarcodeExtension::class)->isbn10(); |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * Get a random ISBN-13 code |
| 848 | * |
| 849 | * @see http://en.wikipedia.org/wiki/International_Standard_Book_Number |
| 850 | * |
| 851 | * @example '9790404436093' |
| 852 | */ |
| 853 | public function isbn13(): string |
| 854 | { |
| 855 | return $this->ext(Extension\BarcodeExtension::class)->isbn13(); |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * Returns a random number between $int1 and $int2 (any order) |
| 860 | * |
| 861 | * @example 79907610 |
| 862 | */ |
| 863 | public function numberBetween($int1 = 0, $int2 = 2147483647): int |
| 864 | { |
| 865 | return $this->ext(Extension\NumberExtension::class)->numberBetween((int) $int1, (int) $int2); |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * Returns a random number between 0 and 9 |
| 870 | */ |
| 871 | public function randomDigit(): int |
| 872 | { |
| 873 | return $this->ext(Extension\NumberExtension::class)->randomDigit(); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * Generates a random digit, which cannot be $except |
| 878 | */ |
| 879 | public function randomDigitNot($except): int |
| 880 | { |
| 881 | return $this->ext(Extension\NumberExtension::class)->randomDigitNot((int) $except); |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * Returns a random number between 1 and 9 |
| 886 | */ |
| 887 | public function randomDigitNotZero(): int |
| 888 | { |
| 889 | return $this->ext(Extension\NumberExtension::class)->randomDigitNotZero(); |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * Return a random float number |
| 894 | * |
| 895 | * @example 48.8932 |
| 896 | */ |
| 897 | public function randomFloat($nbMaxDecimals = null, $min = 0, $max = null): float |
| 898 | { |
| 899 | return $this->ext(Extension\NumberExtension::class)->randomFloat( |
| 900 | $nbMaxDecimals !== null ? (int) $nbMaxDecimals : null, |
| 901 | (float) $min, |
| 902 | $max !== null ? (float) $max : null |
| 903 | ); |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Returns a random integer with 0 to $nbDigits digits. |
| 908 | * |
| 909 | * The maximum value returned is mt_getrandmax() |
| 910 | * |
| 911 | * @param int|null $nbDigits Defaults to a random number between 1 and 9 |
| 912 | * @param bool $strict Whether the returned number should have exactly $nbDigits |
| 913 | * |
| 914 | * @example 79907610 |
| 915 | */ |
| 916 | public function randomNumber($nbDigits = null, $strict = false): int |
| 917 | { |
| 918 | return $this->ext(Extension\NumberExtension::class)->randomNumber( |
| 919 | $nbDigits !== null ? (int) $nbDigits : null, |
| 920 | (bool) $strict |
| 921 | ); |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * Get a version number in semantic versioning syntax 2.0.0. (https://semver.org/spec/v2.0.0.html) |
| 926 | * |
| 927 | * @param bool $preRelease Pre release parts may be randomly included |
| 928 | * @param bool $build Build parts may be randomly included |
| 929 | * |
| 930 | * @example 1.0.0 |
| 931 | * @example 1.0.0-alpha.1 |
| 932 | * @example 1.0.0-alpha.1+b71f04d |
| 933 | */ |
| 934 | public function semver(bool $preRelease = false, bool $build = false): string |
| 935 | { |
| 936 | return $this->ext(Extension\VersionExtension::class)->semver($preRelease, $build); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * @deprecated |
| 941 | */ |
| 942 | protected function callFormatWithMatches($matches) |
| 943 | { |
| 944 | trigger_deprecation('fakerphp/faker', '1.14', 'Protected method "callFormatWithMatches()" is deprecated and will be removed.'); |
| 945 | |
| 946 | return $this->format($matches[1]); |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * @param string $attribute |
| 951 | * |
| 952 | * @deprecated Use a method instead. |
| 953 | */ |
| 954 | public function __get($attribute) |
| 955 | { |
| 956 | trigger_deprecation('fakerphp/faker', '1.14', 'Accessing property "%s" is deprecated, use "%s()" instead.', $attribute, $attribute); |
| 957 | |
| 958 | return $this->format($attribute); |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * @param string $method |
| 963 | * @param array $attributes |
| 964 | */ |
| 965 | public function __call($method, $attributes) |
| 966 | { |
| 967 | return $this->format($method, $attributes); |
| 968 | } |
| 969 | |
| 970 | public function __destruct() |
| 971 | { |
| 972 | $this->seed(); |
| 973 | } |
| 974 | |
| 975 | public function __wakeup() |
| 976 | { |
| 977 | $this->formatters = []; |
| 978 | } |
| 979 | } |
| 980 |