/src/Faker/Provider/Lorem.php

https://github.com/gobb/Faker · PHP · 214 lines · 138 code · 17 blank · 59 comment · 15 complexity · 0389d568e8856d3a15d025e270ab626a MD5 · raw file

  1. <?php
  2. namespace Faker\Provider;
  3. class Lorem extends \Faker\Provider\Base
  4. {
  5. protected static $wordList = array(
  6. 'alias', 'consequatur', 'aut', 'perferendis', 'sit', 'voluptatem',
  7. 'accusantium', 'doloremque', 'aperiam', 'eaque','ipsa', 'quae', 'ab',
  8. 'illo', 'inventore', 'veritatis', 'et', 'quasi', 'architecto',
  9. 'beatae', 'vitae', 'dicta', 'sunt', 'explicabo', 'aspernatur', 'aut',
  10. 'odit', 'aut', 'fugit', 'sed', 'quia', 'consequuntur', 'magni',
  11. 'dolores', 'eos', 'qui', 'ratione', 'voluptatem', 'sequi', 'nesciunt',
  12. 'neque', 'dolorem', 'ipsum', 'quia', 'dolor', 'sit', 'amet',
  13. 'consectetur', 'adipisci', 'velit', 'sed', 'quia', 'non', 'numquam',
  14. 'eius', 'modi', 'tempora', 'incidunt', 'ut', 'labore', 'et', 'dolore',
  15. 'magnam', 'aliquam', 'quaerat', 'voluptatem', 'ut', 'enim', 'ad',
  16. 'minima', 'veniam', 'quis', 'nostrum', 'exercitationem', 'ullam',
  17. 'corporis', 'nemo', 'enim', 'ipsam', 'voluptatem', 'quia', 'voluptas',
  18. 'sit', 'suscipit', 'laboriosam', 'nisi', 'ut', 'aliquid', 'ex', 'ea',
  19. 'commodi', 'consequatur', 'quis', 'autem', 'vel', 'eum', 'iure',
  20. 'reprehenderit', 'qui', 'in', 'ea', 'voluptate', 'velit', 'esse',
  21. 'quam', 'nihil', 'molestiae', 'et', 'iusto', 'odio', 'dignissimos',
  22. 'ducimus', 'qui', 'blanditiis', 'praesentium', 'laudantium', 'totam',
  23. 'rem', 'voluptatum', 'deleniti', 'atque', 'corrupti', 'quos',
  24. 'dolores', 'et', 'quas', 'molestias', 'excepturi', 'sint',
  25. 'occaecati', 'cupiditate', 'non', 'provident', 'sed', 'ut',
  26. 'perspiciatis', 'unde', 'omnis', 'iste', 'natus', 'error',
  27. 'similique', 'sunt', 'in', 'culpa', 'qui', 'officia', 'deserunt',
  28. 'mollitia', 'animi', 'id', 'est', 'laborum', 'et', 'dolorum', 'fuga',
  29. 'et', 'harum', 'quidem', 'rerum', 'facilis', 'est', 'et', 'expedita',
  30. 'distinctio', 'nam', 'libero', 'tempore', 'cum', 'soluta', 'nobis',
  31. 'est', 'eligendi', 'optio', 'cumque', 'nihil', 'impedit', 'quo',
  32. 'porro', 'quisquam', 'est', 'qui', 'minus', 'id', 'quod', 'maxime',
  33. 'placeat', 'facere', 'possimus', 'omnis', 'voluptas', 'assumenda',
  34. 'est', 'omnis', 'dolor', 'repellendus', 'temporibus', 'autem',
  35. 'quibusdam', 'et', 'aut', 'consequatur', 'vel', 'illum', 'qui',
  36. 'dolorem', 'eum', 'fugiat', 'quo', 'voluptas', 'nulla', 'pariatur',
  37. 'at', 'vero', 'eos', 'et', 'accusamus', 'officiis', 'debitis', 'aut',
  38. 'rerum', 'necessitatibus', 'saepe', 'eveniet', 'ut', 'et',
  39. 'voluptates', 'repudiandae', 'sint', 'et', 'molestiae', 'non',
  40. 'recusandae', 'itaque', 'earum', 'rerum', 'hic', 'tenetur', 'a',
  41. 'sapiente', 'delectus', 'ut', 'aut', 'reiciendis', 'voluptatibus',
  42. 'maiores', 'doloribus', 'asperiores', 'repellat'
  43. );
  44. /**
  45. * @example 'Lorem'
  46. */
  47. public static function word()
  48. {
  49. return static::randomElement(static::$wordList);
  50. }
  51. /**
  52. * Generate an array of random words
  53. *
  54. * @example array('Lorem', 'ipsum', 'dolor')
  55. * @param integer $nb how many words to return
  56. * @param bool $asText if true the sentences are returned as one string
  57. * @return array|string
  58. */
  59. public static function words($nb = 3, $asText = false)
  60. {
  61. $words = array();
  62. for ($i=0; $i < $nb; $i++) {
  63. $words []= static::word();
  64. }
  65. return $asText ? join(' ', $words) : $words;
  66. }
  67. /**
  68. * Generate a random sentence
  69. *
  70. * @example 'Lorem ipsum dolor sit amet.'
  71. * @param integer $nbWords around how many words the sentence should contain
  72. * @param boolean $variableNbWords set to false if you want exactly $nbWords returned,
  73. * otherwise $nbWords may vary by +/-40% with a minimum of 1
  74. * @return string
  75. */
  76. public static function sentence($nbWords = 6, $variableNbWords = true)
  77. {
  78. if ($nbWords <= 0) {
  79. return '';
  80. }
  81. if ($variableNbWords) {
  82. $nbWords = self::randomizeNbElements($nbWords);
  83. }
  84. $words = static::words($nbWords);
  85. $words[0] = ucwords($words[0]);
  86. return join($words, ' ') . '.';
  87. }
  88. /**
  89. * Generate an array of sentences
  90. *
  91. * @example array('Lorem ipsum dolor sit amet.', 'Consectetur adipisicing eli.')
  92. * @param integer $nb how many sentences to return
  93. * @param bool $asText if true the sentences are returned as one string
  94. * @return array|string
  95. */
  96. public static function sentences($nb = 3, $asText = false)
  97. {
  98. $sentences = array();
  99. for ($i=0; $i < $nb; $i++) {
  100. $sentences []= static::sentence();
  101. }
  102. return $asText ? join(' ', $sentences) : $sentences;
  103. }
  104. /**
  105. * Generate a single paragraph
  106. *
  107. * @example 'Sapiente sunt omnis. Ut pariatur ad autem ducimus et. Voluptas rem voluptas sint modi dolorem amet.'
  108. * @param integer $nbSentences around how many sentences the paragraph should contain
  109. * @param boolean $variableNbSentences set to false if you want exactly $nbSentences returned,
  110. * otherwise $nbSentences may vary by +/-40% with a minimum of 1
  111. * @return string
  112. */
  113. public static function paragraph($nbSentences = 3, $variableNbSentences = true)
  114. {
  115. if ($nbSentences <= 0) {
  116. return '';
  117. }
  118. if ($variableNbSentences) {
  119. $nbSentences = self::randomizeNbElements($nbSentences);
  120. }
  121. return join(static::sentences($nbSentences), ' ');
  122. }
  123. /**
  124. * Generate an array of paragraphs
  125. *
  126. * @example array($paragraph1, $paragraph2, $paragraph3)
  127. * @param integer $nb how many paragraphs to return
  128. * @param bool $asText if true the paragraphs are returned as one string, separated by two newlines
  129. * @return array|string
  130. */
  131. public static function paragraphs($nb = 3, $asText = false)
  132. {
  133. $paragraphs = array();
  134. for ($i=0; $i < $nb; $i++) {
  135. $paragraphs []= static::paragraph();
  136. }
  137. return $asText ? join("\n\n", $paragraphs) : $paragraphs;
  138. }
  139. /**
  140. * Generate a text string.
  141. * Depending on the $maxNbChars, returns a string made of words, sentences, or paragraphs.
  142. *
  143. * @example 'Sapiente sunt omnis. Ut pariatur ad autem ducimus et. Voluptas rem voluptas sint modi dolorem amet.'
  144. * @param integer $maxNbChars Maximum number of characters the text should contain (minimum 5)
  145. * @return string
  146. */
  147. public static function text($maxNbChars = 200)
  148. {
  149. $text = array();
  150. if ($maxNbChars < 5) {
  151. throw new \InvalidArgumentException('text() can only generate text of at least 5 characters');
  152. } elseif ($maxNbChars < 25) {
  153. // join words
  154. while (empty($text)) {
  155. $size = 0;
  156. // determine how many words are needed to reach the $maxNbChars once;
  157. while ($size < $maxNbChars) {
  158. $word = ($size ? ' ' : '') . static::word();
  159. $text []= $word;
  160. $size += strlen($word);
  161. }
  162. array_pop($text);
  163. }
  164. $text[0][0] = static::toUpper($text[0][0]);
  165. $text[count($text) - 1] .= '.';
  166. } elseif ($maxNbChars < 100) {
  167. // join sentences
  168. while (empty($text)) {
  169. $size = 0;
  170. // determine how many sentences are needed to reach the $maxNbChars once;
  171. while ($size < $maxNbChars) {
  172. $sentence = ($size ? ' ' : '') . static::sentence();
  173. $text []= $sentence;
  174. $size += strlen($sentence);
  175. }
  176. array_pop($text);
  177. }
  178. } else {
  179. // join paragraphs
  180. while (empty($text)) {
  181. $size = 0;
  182. // determine how many paragraphs are needed to reach the $maxNbChars once;
  183. while ($size < $maxNbChars) {
  184. $paragraph = ($size ? "\n" : '') . static::paragraph();
  185. $text []= $paragraph;
  186. $size += strlen($paragraph);
  187. }
  188. array_pop($text);
  189. }
  190. }
  191. return join($text, '');
  192. }
  193. protected static function randomizeNbElements($nbElements)
  194. {
  195. return (int) ($nbElements * mt_rand(60, 140) / 100) + 1;
  196. }
  197. }