1
|
|
|
<?php |
2
|
|
|
namespace Vette\Lipsum\Fusion; |
3
|
|
|
|
4
|
|
|
use Neos\Fusion\FusionObjects\AbstractFusionObject; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Lipsum Implementation |
8
|
|
|
*/ |
9
|
|
|
class LipsumImplementation extends AbstractFusionObject { |
10
|
|
|
|
11
|
|
|
const MIN_WORDS_PER_SENTENCE = 4; |
12
|
|
|
const MAX_WORDS_PER_SENTENCE = 10; |
13
|
|
|
|
14
|
|
|
protected $latinWords = |
15
|
|
|
array('donec', 'at', 'posuere', 'est', 'in', 'volutpat', 'nam', 'egestas', 'tempus', 'turpis', |
16
|
|
|
'ac', 'gravida', 'morbi', 'faucibus', 'sapien', 'ut', 'erat', 'lacinia', 'commodo', 'nullam', |
17
|
|
|
'vestibulum', 'venenatis', 'leo', 'nec', 'pharetra', 'sed', 'blandit', 'urna', 'lorem', 'convallis', |
18
|
|
|
'quis', 'porta', 'rutrum', 'nulla', 'nibh', 'eros', 'etiam', 'magna', 'velit', 'tincidunt', 'ultrices', |
19
|
|
|
'et', 'libero', 'efficitur', 'felis', 'aliquet', 'ante', 'cursus', 'fusce', 'massa', 'non', 'odio', |
20
|
|
|
'id', 'fermentum', 'purus', 'maecenas', 'semper', 'dignissim', 'mauris', 'diam', 'neque', |
21
|
|
|
'suspendisse', 'arcu', 'sit', 'amet', 'elit', 'facilisi', 'bibendum', 'eget', 'ipsum', 'primis', |
22
|
|
|
'orci', 'luctus', 'cubilia', 'curae', 'vehicula', 'suscipit', 'viverra', 'pellentesque', 'ligula', |
23
|
|
|
'proin', 'ullamcorper', 'nisi', 'praesent', 'ultricies', 'lacus', 'scelerisque', 'maximus', 'vitae', |
24
|
|
|
'curabitur', 'sem', 'ex', 'hendrerit', 'euismod', 'nunc', 'tortor', 'eleifend', 'duis', 'eu', 'dui', |
25
|
|
|
'facilisis', 'dapibus', 'nisl', 'cras', 'rhoncus', 'consequat', 'aliquam', 'vel', 'quam', 'tristique', |
26
|
|
|
'sodales', 'tellus', 'phasellus', 'aenean', 'feugiat', 'vivamus', 'dictum', 'finibus', 'enim', |
27
|
|
|
'pulvinar', 'mi', 'malesuada', 'risus', 'auctor', 'pretium', 'augue', 'placerat', 'a', 'consectetur', |
28
|
|
|
'metus', 'justo', 'lectus', 'vulputate', 'congue', 'dolor', 'iaculis', 'varius', 'molestie', |
29
|
|
|
'condimentum', 'accumsan', 'porttitor', 'mollis', 'quisque', 'ornare', 'imperdiet', 'class', 'aptent', |
30
|
|
|
'taciti', 'sociosqu', 'ad', 'litora', 'torquent', 'per', 'conubia', 'nostra', 'inceptos', 'himenaeos', |
31
|
|
|
'interdum', 'mattis', 'lobortis', 'cum', 'sociis', 'natoque', 'penatibus', 'magnis', 'dis', |
32
|
|
|
'parturient', 'montes', 'nascetur', 'ridiculus', 'mus', 'tempor', 'sollicitudin', 'fringilla', |
33
|
|
|
'elementum', 'sagittis', 'integer'); |
34
|
|
|
|
35
|
|
|
protected $lastWord = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Evaluate this TypoScript object and return the result |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function evaluate() |
42
|
|
|
{ |
43
|
|
|
$wordCount = $this->fusionValue('wordCount'); |
44
|
|
|
$paragraphCount = $this->fusionValue('paragraphCount'); |
45
|
|
|
$sentencesPerParagraph = $this->fusionValue('sentencesPerParagraph'); |
46
|
|
|
$textAlign = $this->fusionValue('textAlign'); |
47
|
|
|
$startLipsum = $this->fusionValue('lipsum'); |
48
|
|
|
|
49
|
|
|
$sentences = $this->createSentences($wordCount, $startLipsum); |
50
|
|
|
$paragraphs = $this->createParagraphs($sentences, $paragraphCount, $sentencesPerParagraph, $textAlign); |
51
|
|
|
|
52
|
|
|
return implode('', $paragraphs); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Select a random word from array and make sure it isn't used twice in a row |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function randomWord() |
61
|
|
|
{ |
62
|
|
|
$word = $this->latinWords[rand(0, count($this->latinWords) - 1)]; |
63
|
|
|
|
64
|
|
|
// make sure next word is different |
65
|
|
|
if($word == $this->lastWord) { |
66
|
|
|
return $this->randomWord(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $word; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create a sentence consisting of randomly selected words |
74
|
|
|
* |
75
|
|
|
* @param $length |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
protected function createSentence($length) |
79
|
|
|
{ |
80
|
|
|
$sentence = array(); |
81
|
|
|
|
82
|
|
|
$commaPos = rand(3, $length - 4); |
83
|
|
|
for($i = 0; $i < $length; $i++) { |
84
|
|
|
$word = $this->randomWord(); |
85
|
|
|
|
86
|
|
|
// 50% chance of adding a comma to sentences longer than 6 words |
87
|
|
|
if($i == $commaPos && $length > 6 && rand(0,1) == 1) { |
88
|
|
|
$word .= ','; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$sentence[] = $word; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return ucfirst(implode(' ', $sentence)) . '.'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $wordCount |
99
|
|
|
* @param $usedWordsCount |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
protected function getSentenceLength($wordCount, $usedWordsCount) |
103
|
|
|
{ |
104
|
|
|
$length = rand($this::MIN_WORDS_PER_SENTENCE, $this::MAX_WORDS_PER_SENTENCE); |
105
|
|
|
|
106
|
|
|
//If there are less than MIN_WORDS_PER_SENTENCE words left, add them to the current sentence |
107
|
|
|
$rest = $wordCount - ($usedWordsCount + $length); |
108
|
|
|
if($rest < $this::MIN_WORDS_PER_SENTENCE) { |
109
|
|
|
$length += $rest; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $length; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Create an array of sentences with the given word count |
117
|
|
|
* |
118
|
|
|
* @param $wordCount |
119
|
|
|
* @param $startLipsum |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function createSentences($wordCount, $startLipsum) |
123
|
|
|
{ |
124
|
|
|
$usedWordsCount = 0; |
125
|
|
|
$sentences = array(); |
126
|
|
|
|
127
|
|
|
if($startLipsum) { |
128
|
|
|
$sentences[] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; |
129
|
|
|
$usedWordsCount += 8; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
do { |
133
|
|
|
$length = $this->getSentenceLength($wordCount, $usedWordsCount); |
134
|
|
|
$sentences[] = $this->createSentence($length); |
135
|
|
|
$usedWordsCount += $length; |
136
|
|
|
|
137
|
|
|
} while ($usedWordsCount < $wordCount); |
138
|
|
|
|
139
|
|
|
return $sentences; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Create a specified number of paragraphs from the given sentences array |
144
|
|
|
* |
145
|
|
|
* @param $sentences |
146
|
|
|
* @param $paragraphCount |
147
|
|
|
* @param $sentencesPerParagraph |
148
|
|
|
* @param $textAlign |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
protected function createParagraphs($sentences, $paragraphCount, $sentencesPerParagraph, $textAlign) |
152
|
|
|
{ |
153
|
|
|
$paragraphs = array(); |
154
|
|
|
for($i = 0; $i < $paragraphCount; $i++) { |
155
|
|
|
$length = $sentencesPerParagraph; |
156
|
|
|
|
157
|
|
|
if($i == ($paragraphCount - 1)) { |
158
|
|
|
$length = null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$paragraphSentences = implode(' ', array_slice($sentences, $i * $sentencesPerParagraph, $length)); |
162
|
|
|
$paragraphs[] = '<p style="text-align: ' . $textAlign . ';">' . $paragraphSentences . '</p>'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $paragraphs; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|