Generator.php 22 KB

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