1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZLoremIpsumBundle\Faker\Provider; |
4
|
|
|
|
5
|
|
|
class RichText extends Base |
6
|
|
|
{ |
7
|
|
|
const P_TAG = "para"; |
8
|
|
|
const A_TAG = "link"; |
9
|
|
|
const E_TAG = "ezembed"; |
10
|
|
|
const TABLE_TAG = "informaltable"; |
11
|
|
|
const TR_TAG = "tr"; |
12
|
|
|
const TD_TAG = "td"; |
13
|
|
|
const UL_TAG = "itemizedlist"; |
14
|
|
|
const OL_TAG = "orderedlist"; |
15
|
|
|
const LI_TAG = "listitem"; |
16
|
|
|
const H_TAG = "title"; |
17
|
|
|
const EMPHASIS_TAG = "emphasis"; |
18
|
|
|
const CUSTOM_TAG = "custom"; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param integer $maxDepth |
22
|
|
|
* @param integer $maxWidth |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function randomRichText($maxDepth = 4, $maxWidth = 4) |
27
|
|
|
{ |
28
|
|
|
$document = new \DOMDocument(); |
29
|
|
|
|
30
|
|
|
$body = $document->createElementNS("http://docbook.org/ns/docbook", "section"); |
31
|
|
|
$body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', 'http://www.w3.org/1999/xlink'); |
32
|
|
|
$body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ezxhtml', 'http://ez.no/xmlns/ezpublish/docbook/xhtml'); |
33
|
|
|
$body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ezcustom', 'http://ez.no/xmlns/ezpublish/docbook/custom'); |
34
|
|
|
$body->setAttribute('version', '5.0-variant ezpublish-1.0'); |
35
|
|
|
|
36
|
|
|
$this->addRandomSubTree($body, $maxDepth, $maxWidth); |
37
|
|
|
|
38
|
|
|
$document->appendChild($body); |
39
|
|
|
|
40
|
|
|
$out = $document->saveXML(); |
41
|
|
|
|
42
|
|
|
return $out; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function addRandomSubTree(\DOMElement $root, $maxDepth, $maxWidth) |
46
|
|
|
{ |
47
|
|
|
$maxDepth--; |
48
|
|
|
if ($maxDepth <= 0) { |
49
|
|
|
$this->addRandomLeaf($root); |
50
|
|
|
return $root; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$siblings = mt_rand(1, $maxWidth); |
54
|
|
|
for ($i = 0; $i < $siblings; $i++) { |
55
|
|
|
if ($maxDepth == 1) { |
56
|
|
|
$this->addRandomLeaf($root); |
57
|
|
|
} else { |
58
|
|
|
$this->addRandomSubTree($root, mt_rand(1, $maxDepth), $maxWidth); |
59
|
|
|
} |
60
|
|
|
}; |
61
|
|
|
return $root; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function addRandomLeaf(\DOMElement $node) |
65
|
|
|
{ |
66
|
|
|
$rand = mt_rand(1, 10); |
67
|
|
|
switch($rand) { |
68
|
|
|
case 1: |
69
|
|
|
$this->addRandomE($node); |
70
|
|
|
break; |
71
|
|
|
case 2: |
72
|
|
|
$this->addRandomA($node); |
73
|
|
|
break; |
74
|
|
|
case 3: |
75
|
|
|
$this->addRandomOL($node); |
76
|
|
|
break; |
77
|
|
|
case 4: |
78
|
|
|
$this->addRandomUL($node); |
79
|
|
|
break; |
80
|
|
|
case 5: |
81
|
|
|
$this->addRandomH($node); |
82
|
|
|
break; |
83
|
|
|
case 6: |
84
|
|
|
$this->addRandomB($node); |
85
|
|
|
break; |
86
|
|
|
case 7: |
87
|
|
|
$this->addRandomI($node); |
88
|
|
|
break; |
89
|
|
|
case 8: |
90
|
|
|
$this->addRandomTable($node); |
91
|
|
|
break; |
92
|
|
|
case 9: |
93
|
|
|
$this->addRandomU($node); |
94
|
|
|
break; |
95
|
|
|
default: |
96
|
|
|
$this->addRandomP($node); |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function addRandomE(\DOMElement $element) |
102
|
|
|
{ |
103
|
|
|
$node = $element->ownerDocument->createElement(static::E_TAG); |
|
|
|
|
104
|
|
|
$node->setAttribute("view", "embed"); |
105
|
|
|
/// @todo improve: generation of a radmon object id |
106
|
|
|
$node->setAttribute("xlink:href", 'ezcontent://' . mt_rand(1, 100000)); |
107
|
|
|
$element->appendChild($node); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @todo add random align |
112
|
|
|
* @param \DOMElement $element |
113
|
|
|
* @param int $maxLength |
114
|
|
|
*/ |
115
|
|
|
private function addRandomP(\DOMElement $element, $maxLength = 50) |
116
|
|
|
{ |
117
|
|
|
$p = $element->ownerDocument->createElement(static::P_TAG); |
118
|
|
|
$text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
119
|
|
|
// left-aligned paragraphs have double frequency |
120
|
|
|
switch (mt_rand(1, 4)) { |
121
|
|
|
case 1: |
122
|
|
|
$p->setAttribute("ezxhtml:textalign", "right"); |
123
|
|
|
break; |
124
|
|
|
case 2: |
125
|
|
|
$p->setAttribute("ezxhtml:textalign", "center"); |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
$p->appendChild($text); |
129
|
|
|
$element->appendChild($p); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function addRandomA(\DOMElement $element, $maxLength = 10) |
133
|
|
|
{ |
134
|
|
|
$text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
135
|
|
|
$node = $element->ownerDocument->createElement(static::A_TAG); |
136
|
|
|
$node->setAttribute("xlink:href", 'ezurl://28'); |
137
|
|
|
$node->setAttribute('xlink:show', 'none'); |
138
|
|
|
$node->appendChild($text); |
139
|
|
|
$this->wrapInParagraph($node, $element); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function addRandomH(\DOMElement $element, $maxLength = 10) |
143
|
|
|
{ |
144
|
|
|
$h = static::H_TAG; |
145
|
|
|
$text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
146
|
|
|
$node = $element->ownerDocument->createElement($h); |
147
|
|
|
$node->setAttribute('ezxhtml:level', mt_rand(1, 6)); |
148
|
|
|
$node->appendChild($text); |
149
|
|
|
$element->appendChild($node); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function addRandomB(\DOMElement $element, $maxLength = 10) |
153
|
|
|
{ |
154
|
|
|
$text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
155
|
|
|
$node = $element->ownerDocument->createElement(static::EMPHASIS_TAG); |
156
|
|
|
$node->setAttribute('role', 'strong'); |
157
|
|
|
$node->appendChild($text); |
158
|
|
|
$this->wrapInParagraph($node, $element); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function addRandomU(\DOMElement $element, $maxLength = 10) |
162
|
|
|
{ |
163
|
|
|
$text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
164
|
|
|
$node = $element->ownerDocument->createElement(static::EMPHASIS_TAG); |
165
|
|
|
$node->setAttribute('role', 'underlined'); |
166
|
|
|
$node->appendChild($text); |
167
|
|
|
$this->wrapInParagraph($node, $element); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function addRandomI(\DOMElement $element, $maxLength = 10) |
171
|
|
|
{ |
172
|
|
|
$text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength))); |
173
|
|
|
$node = $element->ownerDocument->createElement(static::EMPHASIS_TAG); |
174
|
|
|
$node->setAttribute('role', 'emphasis'); |
175
|
|
|
$node->appendChild($text); |
176
|
|
|
$this->wrapInParagraph($node, $element); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function addRandomTable(\DOMElement $element, $maxRows = 10, $maxCols = 6, $maxLength = 10) |
180
|
|
|
{ |
181
|
|
|
$rows = mt_rand(1, $maxRows); |
182
|
|
|
$cols = mt_rand(1, $maxCols); |
183
|
|
|
|
184
|
|
|
$table = $element->ownerDocument->createElement(static::TABLE_TAG); |
185
|
|
|
|
186
|
|
|
for ($i = 0; $i < $rows; $i++) { |
187
|
|
|
$tr = $element->ownerDocument->createElement(static::TR_TAG); |
188
|
|
|
$table->appendChild($tr); |
189
|
|
|
for ($j = 0; $j < $cols; $j++) { |
190
|
|
|
$th = $element->ownerDocument->createElement(static::TD_TAG); |
191
|
|
|
$th->textContent = $this->getSentence(mt_rand(1, $maxLength)); |
192
|
|
|
$tr->appendChild($th); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
$this->wrapInParagraph($table, $element); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
private function addRandomUL(\DOMElement $element, $maxItems = 11, $maxLength = 4) |
199
|
|
|
{ |
200
|
|
|
$num = mt_rand(1, $maxItems); |
201
|
|
|
$ul = $element->ownerDocument->createElement(static::UL_TAG); |
202
|
|
|
for ($i = 0; $i < $num; $i++) { |
203
|
|
|
$li = $element->ownerDocument->createElement(static::LI_TAG); |
204
|
|
|
$lip = $element->ownerDocument->createElement(static::P_TAG); |
205
|
|
|
$lip->textContent = $this->getSentence(mt_rand(1, $maxLength)); |
206
|
|
|
$li->appendChild($lip); |
207
|
|
|
$ul->appendChild($li); |
208
|
|
|
} |
209
|
|
|
$element->appendChild($ul); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
private function addRandomOL(\DOMElement $element, $maxItems = 11, $maxLength = 4) |
213
|
|
|
{ |
214
|
|
|
$num = mt_rand(1, $maxItems); |
215
|
|
|
$ul = $element->ownerDocument->createElement(static::OL_TAG); |
216
|
|
|
for ($i = 0; $i < $num; $i++) { |
217
|
|
|
$li = $element->ownerDocument->createElement(static::LI_TAG); |
218
|
|
|
$lip = $element->ownerDocument->createElement(static::P_TAG); |
219
|
|
|
$lip->textContent = $this->getSentence(mt_rand(1, $maxLength)); |
220
|
|
|
$li->appendChild($lip); |
221
|
|
|
$ul->appendChild($li); |
222
|
|
|
} |
223
|
|
|
$element->appendChild($ul); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
protected function wrapInParagraph(\DOMElement $element, \DOMElement $parent, $maxLength = 10) |
227
|
|
|
{ |
228
|
|
|
$p = $element->ownerDocument->createElement(static::P_TAG); |
229
|
|
|
$chance = mt_rand(1, 4); |
230
|
|
|
if ($chance == 1 || $chance == 4) { |
231
|
|
|
$text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)) . ' '); |
232
|
|
|
$p->appendChild($text); |
233
|
|
|
} |
234
|
|
|
$p->appendChild($element); |
235
|
|
|
if ($chance == 2 || $chance == 4) { |
236
|
|
|
$text = $element->ownerDocument->createTextNode(' ' . $this->getSentence(mt_rand(1, $maxLength))); |
237
|
|
|
$p->appendChild($text); |
238
|
|
|
} |
239
|
|
|
$parent->appendChild($p); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
protected function getSentence($nbWords = 6, $variableNbWords = true) |
243
|
|
|
{ |
244
|
|
|
return $this->generator->sentence($nbWords, $variableNbWords); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
protected function getUrl() |
248
|
|
|
{ |
249
|
|
|
return $this->generator->url; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.