-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLoremIpsum.php
More file actions
362 lines (322 loc) · 11.7 KB
/
LoremIpsum.php
File metadata and controls
362 lines (322 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* Lorem Ipsum Generator
*
* PHP version 5.3+
*
* Licensed under The MIT License.
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <hello@joshtronic.com>
* @copyright Copyright 2014-2022 Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @link https://github.com/joshtronic/php-loremipsum
*
* Only changes are purely cosmetic, typings, resilience and bugfixes.
*/
declare (strict_types=1);
namespace MakinaCorpus\DbToolsBundle\Helper;
class LoremIpsum
{
/**
* Whether or not we should be starting the string with "Lorem ipsum...".
*/
private ?array $first = null;
/**
* A lorem ipsum vocabulary of sorts. Not a complete list as I'm unsure if
* a complete list exists and if so, where to get it.
*/
private array $words = [
// Lorem ipsum...
'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit',
// and the rest of the vocabulary
'a', 'ac', 'accumsan', 'ad', 'aenean', 'aliquam', 'aliquet', 'ante',
'aptent', 'arcu', 'at', 'auctor', 'augue', 'bibendum', 'blandit',
'class', 'commodo', 'condimentum', 'congue', 'consequat', 'conubia',
'convallis', 'cras', 'cubilia', 'curabitur', 'curae', 'cursus',
'dapibus', 'diam', 'dictum', 'dictumst', 'dignissim', 'dis', 'donec',
'dui', 'duis', 'efficitur', 'egestas', 'eget', 'eleifend', 'elementum',
'enim', 'erat', 'eros', 'est', 'et', 'etiam', 'eu', 'euismod', 'ex',
'facilisi', 'facilisis', 'fames', 'faucibus', 'felis', 'fermentum',
'feugiat', 'finibus', 'fringilla', 'fusce', 'gravida', 'habitant',
'habitasse', 'hac', 'hendrerit', 'himenaeos', 'iaculis', 'id',
'imperdiet', 'in', 'inceptos', 'integer', 'interdum', 'justo',
'lacinia', 'lacus', 'laoreet', 'lectus', 'leo', 'libero', 'ligula',
'litora', 'lobortis', 'luctus', 'maecenas', 'magna', 'magnis',
'malesuada', 'massa', 'mattis', 'mauris', 'maximus', 'metus', 'mi',
'molestie', 'mollis', 'montes', 'morbi', 'mus', 'nam', 'nascetur',
'natoque', 'nec', 'neque', 'netus', 'nibh', 'nisi', 'nisl', 'non',
'nostra', 'nulla', 'nullam', 'nunc', 'odio', 'orci', 'ornare',
'parturient', 'pellentesque', 'penatibus', 'per', 'pharetra',
'phasellus', 'placerat', 'platea', 'porta', 'porttitor', 'posuere',
'potenti', 'praesent', 'pretium', 'primis', 'proin', 'pulvinar',
'purus', 'quam', 'quis', 'quisque', 'rhoncus', 'ridiculus', 'risus',
'rutrum', 'sagittis', 'sapien', 'scelerisque', 'sed', 'sem', 'semper',
'senectus', 'sociosqu', 'sodales', 'sollicitudin', 'suscipit',
'suspendisse', 'taciti', 'tellus', 'tempor', 'tempus', 'tincidunt',
'torquent', 'tortor', 'tristique', 'turpis', 'ullamcorper', 'ultrices',
'ultricies', 'urna', 'ut', 'varius', 'vehicula', 'vel', 'velit',
'venenatis', 'vestibulum', 'vitae', 'vivamus', 'viverra', 'volutpat',
'vulputate',
];
/**
* Generates a single word of lorem ipsum.
*
* @param null|string|array $tags
* String or array of HTML tags to wrap output with.
*/
public function word(null|string|array $tags = null): string
{
return $this->words(1, $tags);
}
/**
* Generates an array of lorem ipsum words.
*
* @param int $count
* How many words to generate.
* @return array<string>
* Generated lorem ipsum words
*/
public function wordsArray(int $count = 1): array
{
$count = (int) $count;
$words = [];
$word_count = 0;
// Shuffles and appends the word list to compensate for count
// arguments that exceed the size of our vocabulary list
while ($word_count < $count) {
$shuffle = true;
while ($shuffle) {
$this->shuffle();
// Checks that the last word of the list and the first word of
// the list that's about to be appended are not the same
if (!$word_count || $words[$word_count - 1] != $this->words[0]) {
$words = \array_merge($words, $this->words);
$word_count = \count($words);
$shuffle = false;
}
}
}
return \array_slice($words, 0, $count);
}
/**
* Generates words of lorem ipsum.
*
* @param int $count
* How many words to generate.
* @param null|string|array $tags
* String or array of HTML tags to wrap output with.
* @return string
* Generated lorem ipsum words.
*/
public function words($count = 1, null|string|array $tags = null): string
{
return $this->output($this->wordsArray($count), $tags);
}
/**
* Generates a full sentence of lorem ipsum.
*
* @param null|string|array $tags
* String or array of HTML tags to wrap output with.
* @return string
* Generated lorem ipsum sentence.
*/
public function sentence(null|string|array $tags = null)
{
return $this->sentences(1, $tags);
}
/**
* Generates an array of lorem ipsum sentences.
*
* @param int $count
* How many sentences to generate.
* @return array<string>
* Generated lorem ipsum sentences.
*/
public function sentencesArray($count = 1): array
{
$sentences = [];
for ($i = 0; $i < $count; $i++) {
$sentences[] = $this->punctuate($this->wordsArray($this->gauss(24.46, 5.08)));
}
return $sentences;
}
/**
* Generates sentences of lorem ipsum.
*
* @param int $count
* How many sentences to generate.
* @param null|string|array $tags
* String or array of HTML tags to wrap output with.
* @return string
* Generated lorem ipsum sentences.
*/
public function sentences($count = 1, null|string|array $tags = null): string
{
return $this->output($this->sentencesArray($count), $tags);
}
/**
* Generates a full paragraph of lorem ipsum.
*
* @param null|string|array $tags
* String or array of HTML tags to wrap output with.
* @return string
* Generated lorem ipsum paragraph.
*/
public function paragraph(null|string|array $tags = null): string
{
return $this->paragraphs(1, $tags);
}
/**
* Generates an array of lorem ipsum paragraphs.
*
* @param int $count
* How many paragraphs to generate.
* @return array<string>
* Generated lorem ipsum paragraphs.
*/
public function paragraphsArray($count = 1)
{
$paragraphs = [];
for ($i = 0; $i < $count; $i++) {
$paragraphs[] = $this->sentences($this->gauss(5.8, 1.93));
}
return $paragraphs;
}
/**
* Generates paragraphs of lorem ipsum.
*
* @param int $count
* How many paragraphs to generate.
* @param null|string|array $tags
* String or array of HTML tags to wrap output with.
* @return string
* Generated lorem ipsum paragraphs.
*/
public function paragraphs($count = 1, null|string|array $tags = null): string
{
return $this->output($this->paragraphsArray($count), $tags, "\n\n");
}
/**
* Gaussian Distribution.
*
* This is some smart kid stuff. I went ahead and combined the N(0,1) logic
* with the N(m,s) logic into this single function. Used to calculate the
* number of words in a sentence, the number of sentences in a paragraph
* and the distribution of commas in a sentence.
*
* @param float $mean
* Average value.
* @param float $std_dev
* Standard deviation.
* @return int
* Calculated distribution.
*/
private function gauss(int|float $mean, int|float $std_dev): int
{
$x = \mt_rand() / \mt_getrandmax();
$y = \mt_rand() / \mt_getrandmax();
$z = \sqrt(-2 * \log($x)) * \cos(2 * \pi() * $y);
return \intval($z * $std_dev + $mean);
}
/**
* Shuffles the words, forcing "Lorem ipsum..." at the beginning if it is
* the first time we are generating the text.
*/
private function shuffle(): void
{
if ($this->first) {
$this->first = \array_slice($this->words, 0, 8);
$this->words = \array_slice($this->words, 8);
\shuffle($this->words);
$this->words = $this->first + $this->words;
$this->first = null;
} else {
\shuffle($this->words);
}
}
/**
* Applies punctuation to a sentence. This includes a period at the end,
* the injection of commas as well as capitalizing the first letter of the
* first word of the sentence.
*
* @param array<string> $words
* Word array.
* @return string
* Punctuated sentence.
*/
private function punctuate(array $words): string
{
$word_count = \count($words);
// Only worry about commas on sentences longer than 4 words.
if ($word_count > 4) {
$mean = \log($word_count, 6);
$std_dev = $mean / 6;
$commas = $this->gauss($mean, $std_dev);
for ($i = 1; $i <= $commas; $i++) {
$word = (int) \round($i * $word_count / ($commas + 1));
if ($word < ($word_count - 1) && $word > 0) {
$words[$word] .= ',';
}
}
}
return \ucfirst(\implode(' ', $words) . '.');
}
/**
* Does the rest of the processing of the strings. This includes wrapping
* the strings in HTML tags, handling transformations with the ability of
* back referencing and determining if the passed array should be converted
* into a string or not.
*
* @param array<string> $strings
* An array of generated strings.
* @param null|string|array $tags
* String or array of HTML tags to wrap output with.
* @return array
* Generated lorem ipsum text.
*/
private function outputArray(array $strings, null|string|array $tags): array
{
if ($tags) {
if (!\is_array($tags)) {
$tags = [$tags];
} else {
// Flips the array so we can work from the inside out
$tags = \array_reverse($tags);
}
foreach ($strings as $key => $string) {
foreach ($tags as $tag) {
if (\is_string($tag)) {
// Detects / applies back reference
if ($tag[0] == '<') {
$string = \str_replace('$1', $string, $tag);
} else {
$string = \sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
}
}
$strings[$key] = $string;
}
}
}
return $strings;
}
/**
* Does the rest of the processing of the strings. This includes wrapping
* the strings in HTML tags, handling transformations with the ability of
* back referencing and determining if the passed array should be converted
* into a string or not.
*
* @param array<string> $strings
* An array of generated strings.
* @param null|string|array $tags
* String or array of HTML tags to wrap output with.
* @param string $delimiter
* The string to use when calling implode().
* @return string
* Generated lorem ipsum text.
*/
private function output(array $strings, null|string|array $tags, $delimiter = ' '): string
{
return \implode($delimiter, $this->outputArray($strings, $tags));
}
}