Repository navigation
Expand file tree
/
Copy pathHtmlLorem.php
More file actions
277 lines (240 loc) · 9.35 KB
/
Copy pathHtmlLorem.php
File metadata and controls
277 lines (240 loc) · 9.35 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
<?php
namespace Faker\Provider;
use Faker\Generator;
use Faker\UniqueGenerator;
class HtmlLorem extends Base
{
const HTML_TAG = "html";
const HEAD_TAG = "head";
const BODY_TAG = "body";
const DIV_TAG = "div";
const P_TAG = "p";
const A_TAG = "a";
const SPAN_TAG = "span";
const TABLE_TAG = "table";
const THEAD_TAG = "thead";
const TBODY_TAG = "tbody";
const TR_TAG = "tr";
const TD_TAG = "td";
const TH_TAG = "th";
const UL_TAG = "ul";
const LI_TAG = "li";
const H_TAG = "h";
const B_TAG = "b";
const I_TAG = "i";
const TITLE_TAG = "title";
const FORM_TAG = "form";
const INPUT_TAG = "input";
const LABEL_TAG = "label";
private $idGenerator;
public function __construct(Generator $generator)
{
parent::__construct($generator);
$generator->addProvider(new Lorem($generator));
$generator->addProvider(new Internet($generator));
}
/**
* @param integer $maxDepth
* @param integer $maxWidth
*
* @return string
*/
public function randomHtml($maxDepth = 4, $maxWidth = 4)
{
$document = new \DOMDocument();
$this->idGenerator = new UniqueGenerator($this->generator);
$head = $document->createElement("head");
$this->addRandomTitle($head);
$body = $document->createElement("body");
$this->addLoginForm($body);
$this->addRandomSubTree($body, $maxDepth, $maxWidth);
$html = $document->createElement("html");
$html->appendChild($head);
$html->appendChild($body);
$document->appendChild($html);
return $document->saveHTML();
}
private function addRandomSubTree(\DOMElement $root, $maxDepth, $maxWidth)
{
$maxDepth--;
if ($maxDepth <= 0) {
return $root;
}
$siblings = mt_rand(1, $maxWidth);
for ($i = 0; $i < $siblings; $i++) {
if ($maxDepth == 1) {
$this->addRandomLeaf($root);
} else {
$sibling = $root->ownerDocument->createElement("div");
$root->appendChild($sibling);
$this->addRandomAttribute($sibling);
$this->addRandomSubTree($sibling, mt_rand(0, $maxDepth), $maxWidth);
}
}
return $root;
}
private function addRandomLeaf(\DOMElement $node)
{
$rand = mt_rand(1, 10);
switch($rand){
case 1:
$this->addRandomP($node);
break;
case 2:
$this->addRandomA($node);
break;
case 3:
$this->addRandomSpan($node);
break;
case 4:
$this->addRandomUL($node);
break;
case 5:
$this->addRandomH($node);
break;
case 6:
$this->addRandomB($node);
break;
case 7:
$this->addRandomI($node);
break;
case 8:
$this->addRandomTable($node);
break;
default:
$this->addRandomText($node);
break;
}
}
private function addRandomAttribute(\DOMElement $node)
{
$rand = mt_rand(1, 2);
switch ($rand) {
case 1:
$node->setAttribute("class", $this->generator->word);
break;
case 2:
$node->setAttribute("id", (string)$this->idGenerator->randomNumber(5));
break;
}
}
private function addRandomP(\DOMElement $element, $maxLength = 10)
{
$node = $element->ownerDocument->createElement(static::P_TAG);
$node->textContent = $this->generator->sentence(mt_rand(1, $maxLength));
$element->appendChild($node);
}
private function addRandomText(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength)));
$element->appendChild($text);
}
private function addRandomA(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(static::A_TAG);
$node->setAttribute("href", $this->generator->safeEmailDomain);
$node->appendChild($text);
$element->appendChild($node);
}
private function addRandomTitle(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(static::TITLE_TAG);
$node->appendChild($text);
$element->appendChild($node);
}
private function addRandomH(\DOMElement $element, $maxLength = 10)
{
$h = static::H_TAG . (string)mt_rand(1, 3);
$text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement($h);
$node->appendChild($text);
$element->appendChild($node);
}
private function addRandomB(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(static::B_TAG);
$node->appendChild($text);
$element->appendChild($node);
}
private function addRandomI(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(static::I_TAG);
$node->appendChild($text);
$element->appendChild($node);
}
private function addRandomSpan(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(static::SPAN_TAG);
$node->appendChild($text);
$element->appendChild($node);
}
private function addLoginForm(\DOMElement $element)
{
$textInput = $element->ownerDocument->createElement(static::INPUT_TAG);
$textInput->setAttribute("type", "text");
$textInput->setAttribute("id", "username");
$textLabel = $element->ownerDocument->createElement(static::LABEL_TAG);
$textLabel->setAttribute("for", "username");
$textLabel->textContent = $this->generator->word;
$passwordInput = $element->ownerDocument->createElement(static::INPUT_TAG);
$passwordInput->setAttribute("type", "password");
$passwordInput->setAttribute("id", "password");
$passwordLabel = $element->ownerDocument->createElement(static::LABEL_TAG);
$passwordLabel->setAttribute("for", "password");
$passwordLabel->textContent = $this->generator->word;
$submit = $element->ownerDocument->createElement(static::INPUT_TAG);
$submit->setAttribute("type", "submit");
$submit->setAttribute("value", $this->generator->word);
$submit = $element->ownerDocument->createElement(static::FORM_TAG);
$submit->setAttribute("action", $this->generator->safeEmailDomain);
$submit->setAttribute("method", "POST");
$submit->appendChild($textLabel);
$submit->appendChild($textInput);
$submit->appendChild($passwordLabel);
$submit->appendChild($passwordInput);
$element->appendChild($submit);
}
private function addRandomTable(\DOMElement $element, $maxRows = 10, $maxCols = 6, $maxTitle = 4, $maxLength = 10)
{
$rows = mt_rand(1, $maxRows);
$cols = mt_rand(1, $maxCols);
$table = $element->ownerDocument->createElement(static::TABLE_TAG);
$thead = $element->ownerDocument->createElement(static::THEAD_TAG);
$tbody = $element->ownerDocument->createElement(static::TBODY_TAG);
$table->appendChild($thead);
$table->appendChild($tbody);
$tr = $element->ownerDocument->createElement(static::TR_TAG);
$thead->appendChild($tr);
for ($i = 0; $i < $cols; $i++) {
$th = $element->ownerDocument->createElement(static::TH_TAG);
$th->textContent = $this->generator->sentence(mt_rand(1, $maxTitle));
$tr->appendChild($th);
}
for ($i = 0; $i < $rows; $i++) {
$tr = $element->ownerDocument->createElement(static::TR_TAG);
$tbody->appendChild($tr);
for ($j = 0; $j < $cols; $j++) {
$th = $element->ownerDocument->createElement(static::TD_TAG);
$th->textContent = $this->generator->sentence(mt_rand(1, $maxLength));
$tr->appendChild($th);
}
}
$element->appendChild($table);
}
private function addRandomUL(\DOMElement $element, $maxItems = 11, $maxLength = 4)
{
$num = mt_rand(1, $maxItems);
$ul = $element->ownerDocument->createElement(static::UL_TAG);
for ($i = 0; $i < $num; $i++) {
$li = $element->ownerDocument->createElement(static::LI_TAG);
$li->textContent = $this->generator->sentence(mt_rand(1, $maxLength));
$ul->appendChild($li);
}
$element->appendChild($ul);
}
}