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