PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Faker/Provider/Internet.php

https://github.com/JhonnyL/Faker
PHP | 149 lines | 89 code | 21 blank | 39 comment | 2 complexity | 97ecdb25c79b75a8c854280b345f222b MD5 | raw file
  1. <?php
  2. namespace Faker\Provider;
  3. class Internet extends \Faker\Provider\Base
  4. {
  5. protected static $freeEmailDomain = array('gmail.com', 'yahoo.com', 'hotmail.com');
  6. protected static $tld = array('com', 'com', 'com', 'com', 'com', 'com', 'biz', 'info', 'net', 'org');
  7. protected static $userNameFormats = array(
  8. '{{lastName}}.{{firstName}}',
  9. '{{firstName}}.{{lastName}}',
  10. '{{firstName}}##',
  11. '?{{lastName}}',
  12. );
  13. protected static $emailFormats = array(
  14. '{{userName}}@{{domainName}}',
  15. '{{userName}}@{{freeEmailDomain}}',
  16. );
  17. protected static $urlFormats = array(
  18. 'http://www.{{domainName}}/',
  19. 'http://{{domainName}}/',
  20. );
  21. /**
  22. * @example 'jdoe@acme.biz'
  23. */
  24. public function email()
  25. {
  26. $format = static::randomElement(static::$emailFormats);
  27. return preg_replace('/\s/u', '', $this->generator->parse($format));
  28. }
  29. /**
  30. * @example 'jdoe@example.com'
  31. */
  32. final public function safeEmail()
  33. {
  34. return preg_replace('/\s/u', '', $this->userName() . '@' . static::safeEmailDomain());
  35. }
  36. /**
  37. * @example 'jdoe@gmail.com'
  38. */
  39. public function freeEmail()
  40. {
  41. return preg_replace('/\s/u', '', $this->userName() . '@' . static::freeEmailDomain());
  42. }
  43. /**
  44. * @example 'jdoe@dawson.com'
  45. */
  46. public function companyEmail()
  47. {
  48. return preg_replace('/\s/u', '', $this->userName() . '@' . $this->domainName());
  49. }
  50. /**
  51. * @example 'gmail.com'
  52. */
  53. public static function freeEmailDomain()
  54. {
  55. return static::randomElement(static::$freeEmailDomain);
  56. }
  57. /**
  58. * @example 'example.org'
  59. */
  60. final public static function safeEmailDomain()
  61. {
  62. $domains = array(
  63. 'example.com',
  64. 'example.org',
  65. 'example.net'
  66. );
  67. return static::randomElement($domains);
  68. }
  69. /**
  70. * @example 'jdoe'
  71. */
  72. public function userName()
  73. {
  74. $format = static::randomElement(static::$userNameFormats);
  75. return static::toLower(static::bothify($this->generator->parse($format)));
  76. }
  77. /**
  78. * @example 'tiramisu.com'
  79. */
  80. public function domainName()
  81. {
  82. return $this->domainWord() . '.' . $this->tld();
  83. }
  84. /**
  85. * @example 'faber'
  86. */
  87. public function domainWord()
  88. {
  89. $company = $this->generator->format('company');
  90. $companyElements = explode(' ', $company);
  91. $company = $companyElements[0];
  92. $company = preg_replace('/\W/u', '', $company);
  93. return static::toLower($company);
  94. }
  95. /**
  96. * @example 'com'
  97. */
  98. public function tld()
  99. {
  100. return static::randomElement(static::$tld);
  101. }
  102. /**
  103. * @example 'http://www.runolfsdottir.com/'
  104. */
  105. public function url()
  106. {
  107. $format = static::randomElement(static::$urlFormats);
  108. return $this->generator->parse($format);
  109. }
  110. /**
  111. * @example '237.149.115.38'
  112. */
  113. public function ipv4()
  114. {
  115. return long2ip(mt_rand(0, 1) == 0 ? mt_rand(-2147483648, 0) : mt_rand(1, 2147483647));
  116. }
  117. /**
  118. * @example '35cd:186d:3e23:2986:ef9f:5b41:42a4:e6f1'
  119. */
  120. public function ipv6()
  121. {
  122. $res = array();
  123. for ($i=0; $i < 8; $i++) {
  124. $res []= dechex(mt_rand(0, "65535"));
  125. }
  126. return join(':', $res);
  127. }
  128. }