@@ -35,12 +35,16 @@ public function getParagraphs(int $count = 3): string
35
35
return implode ("\n\n" , $ paragraphs );
36
36
}
37
37
38
- public function getSentences (int $ count ): string
38
+ public function getSentences (int $ count = 1 ): string
39
39
{
40
+ $ count = $ count < 1 ? 1 : $ count ;
40
41
$ sentences = array ();
41
42
42
43
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 );
44
48
}
45
49
46
50
$ sentences = $ this ->punctuate ($ sentences );
@@ -58,24 +62,26 @@ public function getSentences(int $count): string
58
62
* @param boolean $asArray whether an array or a string should be returned
59
63
* @return mixed string or array of generated lorem ipsum words
60
64
*/
61
- public function words (int $ count = 1 , bool $ asArray = false )
65
+ public function getWords (int $ count = 1 , bool $ asArray = false )
62
66
{
67
+ $ count = $ count < 1 ? 1 : $ count ;
68
+
63
69
$ words = array ();
64
- $ word_count = 0 ;
70
+ $ wordCount = 0 ;
65
71
$ wordList = $ this ->getWordList ();
66
72
67
73
// Shuffles and appends the word list to compensate for count
68
74
// arguments that exceed the size of our vocabulary list
69
- while ($ word_count < $ count ) {
75
+ while ($ wordCount < $ count ) {
70
76
$ shuffle = true ;
71
77
while ($ shuffle ) {
72
78
shuffle ($ wordList );
73
79
74
80
// Checks that the last word of the list and the first word of
75
81
// 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 ]) {
77
83
$ words = array_merge ($ words , $ wordList );
78
- $ word_count = count ($ words );
84
+ $ wordCount = count ($ words );
79
85
$ shuffle = false ;
80
86
}
81
87
}
@@ -150,16 +156,43 @@ private function punctuate(array $sentences): array
150
156
*/
151
157
private function addJoy (string $ wordsString ): string
152
158
{
159
+ $ unicornKey = null ;
153
160
if ($ this ->unicornsAreReal && false === stripos ($ wordsString , 'unicorn ' )) {
154
161
$ words = explode (' ' , $ wordsString );
155
- $ words [array_rand ($ words )] = 'unicorn ' ;
162
+ $ unicornKey = array_rand ($ words );
163
+ $ words [$ unicornKey ] = 'unicorn ' ;
164
+
156
165
157
166
$ wordsString = implode (' ' , $ words );
158
167
}
159
168
160
- while (substr_count (strtolower ($ wordsString ), strtolower ( 'sunshine ' ) ) < $ this ->minSunshine ) {
169
+ while (substr_count (strtolower ($ wordsString ), 'sunshine ' ) < $ this ->minSunshine ) {
161
170
$ 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 ' ;
163
196
164
197
$ wordsString = implode (' ' , $ words );
165
198
}
0 commit comments