Skip to content

Commit 2c41c6c

Browse files
committed
wow - my logic is actually quite complex!
1 parent 61cc05c commit 2c41c6c

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

src/Service/KnpUIpsum.php

+43-10
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ public function getParagraphs(int $count = 3): string
3535
return implode("\n\n", $paragraphs);
3636
}
3737

38-
public function getSentences(int $count): string
38+
public function getSentences(int $count = 1): string
3939
{
40+
$count = $count < 1 ? 1 : $count;
4041
$sentences = array();
4142

4243
for ($i = 0; $i < $count; $i++) {
43-
$sentences[] = $this->words($this->gauss(16, 5.08), true);
44+
$wordCount = $this->gauss(16, 5.08);
45+
// avoid very short sentences
46+
$wordCount = $wordCount < 4 ? 4 : $wordCount;
47+
$sentences[] = $this->getWords($wordCount, true);
4448
}
4549

4650
$sentences = $this->punctuate($sentences);
@@ -58,24 +62,26 @@ public function getSentences(int $count): string
5862
* @param boolean $asArray whether an array or a string should be returned
5963
* @return mixed string or array of generated lorem ipsum words
6064
*/
61-
public function words(int $count = 1, bool $asArray = false)
65+
public function getWords(int $count = 1, bool $asArray = false)
6266
{
67+
$count = $count < 1 ? 1 : $count;
68+
6369
$words = array();
64-
$word_count = 0;
70+
$wordCount = 0;
6571
$wordList = $this->getWordList();
6672

6773
// Shuffles and appends the word list to compensate for count
6874
// arguments that exceed the size of our vocabulary list
69-
while ($word_count < $count) {
75+
while ($wordCount < $count) {
7076
$shuffle = true;
7177
while ($shuffle) {
7278
shuffle($wordList);
7379

7480
// Checks that the last word of the list and the first word of
7581
// the list that's about to be appended are not the same
76-
if (!$word_count || $words[$word_count - 1] != $wordList[0]) {
82+
if (!$wordCount || $words[$wordCount - 1] != $wordList[0]) {
7783
$words = array_merge($words, $wordList);
78-
$word_count = count($words);
84+
$wordCount = count($words);
7985
$shuffle = false;
8086
}
8187
}
@@ -150,16 +156,43 @@ private function punctuate(array $sentences): array
150156
*/
151157
private function addJoy(string $wordsString): string
152158
{
159+
$unicornKey = null;
153160
if ($this->unicornsAreReal && false === stripos($wordsString, 'unicorn')) {
154161
$words = explode(' ', $wordsString);
155-
$words[array_rand($words)] = 'unicorn';
162+
$unicornKey = array_rand($words);
163+
$words[$unicornKey] = 'unicorn';
164+
156165

157166
$wordsString = implode(' ', $words);
158167
}
159168

160-
while (substr_count(strtolower($wordsString), strtolower('sunshine')) < $this->minSunshine) {
169+
while (substr_count(strtolower($wordsString), 'sunshine') < $this->minSunshine) {
161170
$words = explode(' ', $wordsString);
162-
$words[array_rand($words)] = 'sunshine';
171+
172+
// if there simply are not enough words, abort immediately
173+
if (count($words) < ($this->minSunshine + 1)) {
174+
break;
175+
}
176+
177+
$key = null;
178+
while (null === $key) {
179+
$key = array_rand($words);
180+
181+
// don't override the unicorn
182+
if ($unicornKey === $key) {
183+
$key = null;
184+
185+
continue;
186+
}
187+
188+
// if unicorn occurred naturally, don't override it
189+
if (null === $unicornKey && 0 === stripos($words[$key], 'unicorn')) {
190+
$key = null;
191+
192+
continue;
193+
}
194+
}
195+
$words[$key] = 'sunshine';
163196

164197
$wordsString = implode(' ', $words);
165198
}

0 commit comments

Comments
 (0)