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