PageRenderTime 1085ms CodeModel.GetById 32ms RepoModel.GetById 20ms app.codeStats 0ms

/classes/kohana/faker/internet.php

https://github.com/jmhobbs/K3-Faker
PHP | 164 lines | 82 code | 15 blank | 67 comment | 5 complexity | 11a9ab9a985524c181a069d1f6ba7509 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /*!
  3. Fake Internet data.
  4. Don't access directly, instead use the Faker class.
  5. \code
  6. Faker::Internet()->email
  7. \endcode
  8. */
  9. class Kohana_Faker_Internet extends Kohana_Faker_Module {
  10. protected static $FREE_EMAIL_DOMAIN = null;
  11. protected static $DOMAIN_SUFFIX = null;
  12. /*!
  13. Get an email address.
  14. \param name A persons name, if you want it based on that.
  15. \return String - An email address
  16. */
  17. public function email ( $name = null ) {
  18. return sprintf(
  19. '%s@%s',
  20. $this->user_name( $name ),
  21. $this->domain_name()
  22. );
  23. }
  24. /*!
  25. Get an email address at a free vendor.
  26. \param name A persons name, if you want it based on that.
  27. \return String - An email address, from a free vendor
  28. */
  29. public function free_email ( $name = null ) {
  30. return sprintf(
  31. '%s@%s',
  32. $this->user_name( $name ),
  33. self::data_rand( 'free_email_domain' )
  34. );
  35. }
  36. /*!
  37. Get a user name.
  38. \param name A persons name, if you want it based on that.
  39. \return String - A user name
  40. */
  41. public function user_name ( $name = null ) {
  42. if( $name == null ) {
  43. if( 0 == mt_rand( 0, 1 ) ) {
  44. $name = Faker::Name()->first();
  45. }
  46. else {
  47. $name = sprintf(
  48. '%s_%s',
  49. Faker::Name()->first(),
  50. Faker::Name()->last()
  51. );
  52. }
  53. }
  54. return preg_replace( '/\W/', '', str_replace( ' ', '.', strtolower( $name ) ) );
  55. }
  56. /*!
  57. Alias of user_name()
  58. \param name A persons name, if you want it based on that.
  59. \return String - A user name
  60. */
  61. public function username ( $name = null ) {
  62. return $this->user_name( $name );
  63. }
  64. /*!
  65. Get a domain name.
  66. \return String - A domain name
  67. */
  68. public function domain_name () {
  69. return sprintf(
  70. '%s.%s',
  71. $this->domain_word(),
  72. self::data_rand( 'domain_suffix' )
  73. );
  74. }
  75. /*!
  76. Get a valid word for in a domain name.
  77. \return String - A word, valid for use in a domain
  78. */
  79. public function domain_word () {
  80. $company_name = explode( ' ', Faker::Company()->name() );
  81. return strtolower(
  82. preg_replace(
  83. '/\W/',
  84. '',
  85. $company_name[0]
  86. )
  87. );
  88. }
  89. /*!
  90. Get an IPv4 address.
  91. \return String - An IPv4 address in dotted notation
  92. */
  93. public function ip_v4_address () {
  94. return sprintf(
  95. '%d.%d.%d.%d',
  96. mt_rand(2,255),
  97. mt_rand(2,255),
  98. mt_rand(2,255),
  99. mt_rand(2,255)
  100. );
  101. }
  102. /*!
  103. Alias of ip_v4_address
  104. \return String - An IPv4 address in dotted notation
  105. */
  106. public function ip_v4 () {
  107. return $this->ip_v4_address();
  108. }
  109. /*!
  110. Get an IPv6 address.
  111. \return String - An IPv6 address in hex-colon notation
  112. */
  113. public function ip_v6_address () {
  114. return sprintf(
  115. '%x:%x:%x:%x:%x:%x:%x:%x',
  116. mt_rand(0,65535),
  117. mt_rand(0,65535),
  118. mt_rand(0,65535),
  119. mt_rand(0,65535),
  120. mt_rand(0,65535),
  121. mt_rand(0,65535),
  122. mt_rand(0,65535),
  123. mt_rand(0,65535)
  124. );
  125. }
  126. /*!
  127. Alias of ip_v6_address()
  128. \return String - An IPv6 address in hex-colon notation
  129. */
  130. public function ip_v6 () {
  131. return $this->ip_v6_address();
  132. }
  133. }