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

/src/Faker/Generator.php

http://github.com/fzaninotto/Faker
PHP | 292 lines | 64 code | 13 blank | 215 comment | 6 complexity | 3175d580bca244723981887ffc7b8993 MD5 | raw file
  1. <?php
  2. namespace Faker;
  3. /**
  4. * @property string $name
  5. * @method string name(string $gender = null)
  6. * @property string $firstName
  7. * @method string firstName(string $gender = null)
  8. * @property string $firstNameMale
  9. * @property string $firstNameFemale
  10. * @property string $lastName
  11. * @property string $title
  12. * @method string title(string $gender = null)
  13. * @property string $titleMale
  14. * @property string $titleFemale
  15. *
  16. * @property string $citySuffix
  17. * @property string $streetSuffix
  18. * @property string $buildingNumber
  19. * @property string $city
  20. * @property string $streetName
  21. * @property string $streetAddress
  22. * @property string $secondaryAddress
  23. * @property string $postcode
  24. * @property string $address
  25. * @property string $state
  26. * @property string $country
  27. * @property float $latitude
  28. * @property float $longitude
  29. *
  30. * @property string $ean13
  31. * @property string $ean8
  32. * @property string $isbn13
  33. * @property string $isbn10
  34. *
  35. * @property string $phoneNumber
  36. * @property string $e164PhoneNumber
  37. *
  38. * @property string $company
  39. * @property string $companySuffix
  40. * @property string $jobTitle
  41. *
  42. * @property string $creditCardType
  43. * @property string $creditCardNumber
  44. * @method string creditCardNumber($type = null, $formatted = false, $separator = '-')
  45. * @property \DateTime $creditCardExpirationDate
  46. * @property string $creditCardExpirationDateString
  47. * @property array $creditCardDetails
  48. * @property string $bankAccountNumber
  49. * @method string iban($countryCode = null, $prefix = '', $length = null)
  50. * @property string $swiftBicNumber
  51. * @property string $vat
  52. *
  53. * @property string $word
  54. * @property string|array $words
  55. * @method string|array words($nb = 3, $asText = false)
  56. * @method string word()
  57. * @property string $sentence
  58. * @method string sentence($nbWords = 6, $variableNbWords = true)
  59. * @property string|array $sentences
  60. * @method string|array sentences($nb = 3, $asText = false)
  61. * @property string $paragraph
  62. * @method string paragraph($nbSentences = 3, $variableNbSentences = true)
  63. * @property string|array $paragraphs
  64. * @method string|array paragraphs($nb = 3, $asText = false)
  65. * @property string $text
  66. * @method string text($maxNbChars = 200)
  67. *
  68. * @method string realText($maxNbChars = 200, $indexSize = 2)
  69. *
  70. * @property string $email
  71. * @property string $safeEmail
  72. * @property string $freeEmail
  73. * @property string $companyEmail
  74. * @property string $freeEmailDomain
  75. * @property string $safeEmailDomain
  76. * @property string $userName
  77. * @property string $password
  78. * @method string password($minLength = 6, $maxLength = 20)
  79. * @property string $domainName
  80. * @property string $domainWord
  81. * @property string $tld
  82. * @property string $url
  83. * @property string $slug
  84. * @method string slug($nbWords = 6, $variableNbWords = true)
  85. * @property string $ipv4
  86. * @property string $ipv6
  87. * @property string $localIpv4
  88. * @property string $macAddress
  89. *
  90. * @property int $unixTime
  91. * @property \DateTime $dateTime
  92. * @property \DateTime $dateTimeAD
  93. * @property string $iso8601
  94. * @property \DateTime $dateTimeThisCentury
  95. * @property \DateTime $dateTimeThisDecade
  96. * @property \DateTime $dateTimeThisYear
  97. * @property \DateTime $dateTimeThisMonth
  98. * @property string $amPm
  99. * @property string $dayOfMonth
  100. * @property string $dayOfWeek
  101. * @property string $month
  102. * @property string $monthName
  103. * @property string $year
  104. * @property string $century
  105. * @property string $timezone
  106. * @method string amPm($max = 'now')
  107. * @method string date($format = 'Y-m-d', $max = 'now')
  108. * @method string dayOfMonth($max = 'now')
  109. * @method string dayOfWeek($max = 'now')
  110. * @method string iso8601($max = 'now')
  111. * @method string month($max = 'now')
  112. * @method string monthName($max = 'now')
  113. * @method string time($format = 'H:i:s', $max = 'now')
  114. * @method int unixTime($max = 'now')
  115. * @method string year($max = 'now')
  116. * @method \DateTime dateTime($max = 'now', $timezone = null)
  117. * @method \DateTime dateTimeAd($max = 'now', $timezone = null)
  118. * @method \DateTime dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null)
  119. * @method \DateTime dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = null)
  120. * @method \DateTime dateTimeThisCentury($max = 'now', $timezone = null)
  121. * @method \DateTime dateTimeThisDecade($max = 'now', $timezone = null)
  122. * @method \DateTime dateTimeThisYear($max = 'now', $timezone = null)
  123. * @method \DateTime dateTimeThisMonth($max = 'now', $timezone = null)
  124. *
  125. * @property string $md5
  126. * @property string $sha1
  127. * @property string $sha256
  128. * @property string $locale
  129. * @property string $countryCode
  130. * @property string $countryISOAlpha3
  131. * @property string $languageCode
  132. * @property string $currencyCode
  133. * @property boolean $boolean
  134. * @method boolean boolean($chanceOfGettingTrue = 50)
  135. *
  136. * @property int $randomDigit
  137. * @property int $randomDigitNot
  138. * @property int $randomDigitNotNull
  139. * @property string $randomLetter
  140. * @property string $randomAscii
  141. * @method int randomNumber($nbDigits = null, $strict = false)
  142. * @method int|string|null randomKey(array $array = array())
  143. * @method int numberBetween($min = 0, $max = 2147483647)
  144. * @method float randomFloat($nbMaxDecimals = null, $min = 0, $max = null)
  145. * @method mixed randomElement(array $array = array('a', 'b', 'c'))
  146. * @method array randomElements(array $array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false)
  147. * @method array|string shuffle($arg = '')
  148. * @method array shuffleArray(array $array = array())
  149. * @method string shuffleString($string = '', $encoding = 'UTF-8')
  150. * @method string numerify($string = '###')
  151. * @method string lexify($string = '????')
  152. * @method string bothify($string = '## ??')
  153. * @method string asciify($string = '****')
  154. * @method string regexify($regex = '')
  155. * @method string toLower($string = '')
  156. * @method string toUpper($string = '')
  157. * @method Generator optional($weight = 0.5, $default = null)
  158. * @method Generator unique($reset = false, $maxRetries = 10000)
  159. * @method Generator valid($validator = null, $maxRetries = 10000)
  160. * @method mixed passthrough($passthrough)
  161. *
  162. * @method integer biasedNumberBetween($min = 0, $max = 100, $function = 'sqrt')
  163. *
  164. * @property string $macProcessor
  165. * @property string $linuxProcessor
  166. * @property string $userAgent
  167. * @property string $chrome
  168. * @property string $firefox
  169. * @property string $safari
  170. * @property string $opera
  171. * @property string $internetExplorer
  172. * @property string $windowsPlatformToken
  173. * @property string $macPlatformToken
  174. * @property string $linuxPlatformToken
  175. *
  176. * @property string $uuid
  177. *
  178. * @property string $mimeType
  179. * @property string $fileExtension
  180. * @method string file($sourceDirectory = '/tmp', $targetDirectory = '/tmp', $fullPath = true)
  181. *
  182. * @method string imageUrl($width = 640, $height = 480, $category = null, $randomize = true, $word = null, $gray = false)
  183. * @method string image($dir = null, $width = 640, $height = 480, $category = null, $fullPath = true, $randomize = true, $word = null)
  184. *
  185. * @property string $hexColor
  186. * @property string $safeHexColor
  187. * @property string $rgbColor
  188. * @property array $rgbColorAsArray
  189. * @property string $rgbCssColor
  190. * @property string $safeColorName
  191. * @property string $colorName
  192. *
  193. * @method string randomHtml($maxDepth = 4, $maxWidth = 4)
  194. *
  195. */
  196. class Generator
  197. {
  198. protected $providers = array();
  199. protected $formatters = array();
  200. public function addProvider($provider)
  201. {
  202. array_unshift($this->providers, $provider);
  203. }
  204. public function getProviders()
  205. {
  206. return $this->providers;
  207. }
  208. public function seed($seed = null)
  209. {
  210. if ($seed === null) {
  211. mt_srand();
  212. } else {
  213. if (PHP_VERSION_ID < 70100) {
  214. mt_srand((int) $seed);
  215. } else {
  216. mt_srand((int) $seed, MT_RAND_PHP);
  217. }
  218. }
  219. }
  220. public function format($formatter, $arguments = array())
  221. {
  222. return call_user_func_array($this->getFormatter($formatter), $arguments);
  223. }
  224. /**
  225. * @param string $formatter
  226. *
  227. * @return Callable
  228. */
  229. public function getFormatter($formatter)
  230. {
  231. if (isset($this->formatters[$formatter])) {
  232. return $this->formatters[$formatter];
  233. }
  234. foreach ($this->providers as $provider) {
  235. if (method_exists($provider, $formatter)) {
  236. $this->formatters[$formatter] = array($provider, $formatter);
  237. return $this->formatters[$formatter];
  238. }
  239. }
  240. throw new \InvalidArgumentException(sprintf('Unknown formatter "%s"', $formatter));
  241. }
  242. /**
  243. * Replaces tokens ('{{ tokenName }}') with the result from the token method call
  244. *
  245. * @param string $string String that needs to bet parsed
  246. * @return string
  247. */
  248. public function parse($string)
  249. {
  250. return preg_replace_callback('/\{\{\s?(\w+)\s?\}\}/u', array($this, 'callFormatWithMatches'), $string);
  251. }
  252. protected function callFormatWithMatches($matches)
  253. {
  254. return $this->format($matches[1]);
  255. }
  256. /**
  257. * @param string $attribute
  258. *
  259. * @return mixed
  260. */
  261. public function __get($attribute)
  262. {
  263. return $this->format($attribute);
  264. }
  265. /**
  266. * @param string $method
  267. * @param array $attributes
  268. *
  269. * @return mixed
  270. */
  271. public function __call($method, $attributes)
  272. {
  273. return $this->format($method, $attributes);
  274. }
  275. public function __destruct()
  276. {
  277. $this->seed();
  278. }
  279. }