| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Faker\Provider;
|
|
|
4 |
|
|
|
5 |
use Faker\Generator;
|
|
|
6 |
use Faker\UniqueGenerator;
|
|
|
7 |
|
|
|
8 |
class HtmlLorem extends Base
|
|
|
9 |
{
|
|
|
10 |
public const HTML_TAG = 'html';
|
|
|
11 |
public const HEAD_TAG = 'head';
|
|
|
12 |
public const BODY_TAG = 'body';
|
|
|
13 |
public const DIV_TAG = 'div';
|
|
|
14 |
public const P_TAG = 'p';
|
|
|
15 |
public const A_TAG = 'a';
|
|
|
16 |
public const SPAN_TAG = 'span';
|
|
|
17 |
public const TABLE_TAG = 'table';
|
|
|
18 |
public const THEAD_TAG = 'thead';
|
|
|
19 |
public const TBODY_TAG = 'tbody';
|
|
|
20 |
public const TR_TAG = 'tr';
|
|
|
21 |
public const TD_TAG = 'td';
|
|
|
22 |
public const TH_TAG = 'th';
|
|
|
23 |
public const UL_TAG = 'ul';
|
|
|
24 |
public const LI_TAG = 'li';
|
|
|
25 |
public const H_TAG = 'h';
|
|
|
26 |
public const B_TAG = 'b';
|
|
|
27 |
public const I_TAG = 'i';
|
|
|
28 |
public const TITLE_TAG = 'title';
|
|
|
29 |
public const FORM_TAG = 'form';
|
|
|
30 |
public const INPUT_TAG = 'input';
|
|
|
31 |
public const LABEL_TAG = 'label';
|
|
|
32 |
|
|
|
33 |
private $idGenerator;
|
|
|
34 |
|
|
|
35 |
public function __construct(Generator $generator)
|
|
|
36 |
{
|
|
|
37 |
parent::__construct($generator);
|
|
|
38 |
$generator->addProvider(new Lorem($generator));
|
|
|
39 |
$generator->addProvider(new Internet($generator));
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @param int $maxDepth
|
|
|
44 |
* @param int $maxWidth
|
|
|
45 |
*
|
|
|
46 |
* @return string
|
|
|
47 |
*/
|
|
|
48 |
public function randomHtml($maxDepth = 4, $maxWidth = 4)
|
|
|
49 |
{
|
|
|
50 |
if (!class_exists(\DOMDocument::class, false)) {
|
|
|
51 |
throw new \RuntimeException('ext-dom is required to use randomHtml.');
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
$document = new \DOMDocument();
|
|
|
55 |
$this->idGenerator = new UniqueGenerator($this->generator);
|
|
|
56 |
|
|
|
57 |
$head = $document->createElement('head');
|
|
|
58 |
$this->addRandomTitle($head);
|
|
|
59 |
|
|
|
60 |
$body = $document->createElement('body');
|
|
|
61 |
$this->addLoginForm($body);
|
|
|
62 |
$this->addRandomSubTree($body, $maxDepth, $maxWidth);
|
|
|
63 |
|
|
|
64 |
$html = $document->createElement('html');
|
|
|
65 |
$html->appendChild($head);
|
|
|
66 |
$html->appendChild($body);
|
|
|
67 |
|
|
|
68 |
$document->appendChild($html);
|
|
|
69 |
|
|
|
70 |
return $document->saveHTML();
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
private function addRandomSubTree(\DOMElement $root, $maxDepth, $maxWidth)
|
|
|
74 |
{
|
|
|
75 |
--$maxDepth;
|
|
|
76 |
|
|
|
77 |
if ($maxDepth <= 0) {
|
|
|
78 |
return $root;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$siblings = self::numberBetween(1, $maxWidth);
|
|
|
82 |
|
|
|
83 |
for ($i = 0; $i < $siblings; ++$i) {
|
|
|
84 |
if ($maxDepth == 1) {
|
|
|
85 |
$this->addRandomLeaf($root);
|
|
|
86 |
} else {
|
|
|
87 |
$sibling = $root->ownerDocument->createElement('div');
|
|
|
88 |
$root->appendChild($sibling);
|
|
|
89 |
$this->addRandomAttribute($sibling);
|
|
|
90 |
$this->addRandomSubTree($sibling, self::numberBetween(0, $maxDepth), $maxWidth);
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return $root;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private function addRandomLeaf(\DOMElement $node): void
|
|
|
98 |
{
|
|
|
99 |
$rand = self::numberBetween(1, 10);
|
|
|
100 |
|
|
|
101 |
switch ($rand) {
|
|
|
102 |
case 1:
|
|
|
103 |
$this->addRandomP($node);
|
|
|
104 |
|
|
|
105 |
break;
|
|
|
106 |
|
|
|
107 |
case 2:
|
|
|
108 |
$this->addRandomA($node);
|
|
|
109 |
|
|
|
110 |
break;
|
|
|
111 |
|
|
|
112 |
case 3:
|
|
|
113 |
$this->addRandomSpan($node);
|
|
|
114 |
|
|
|
115 |
break;
|
|
|
116 |
|
|
|
117 |
case 4:
|
|
|
118 |
$this->addRandomUL($node);
|
|
|
119 |
|
|
|
120 |
break;
|
|
|
121 |
|
|
|
122 |
case 5:
|
|
|
123 |
$this->addRandomH($node);
|
|
|
124 |
|
|
|
125 |
break;
|
|
|
126 |
|
|
|
127 |
case 6:
|
|
|
128 |
$this->addRandomB($node);
|
|
|
129 |
|
|
|
130 |
break;
|
|
|
131 |
|
|
|
132 |
case 7:
|
|
|
133 |
$this->addRandomI($node);
|
|
|
134 |
|
|
|
135 |
break;
|
|
|
136 |
|
|
|
137 |
case 8:
|
|
|
138 |
$this->addRandomTable($node);
|
|
|
139 |
|
|
|
140 |
break;
|
|
|
141 |
|
|
|
142 |
default:
|
|
|
143 |
$this->addRandomText($node);
|
|
|
144 |
|
|
|
145 |
break;
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
private function addRandomAttribute(\DOMElement $node): void
|
|
|
150 |
{
|
|
|
151 |
$rand = self::numberBetween(1, 2);
|
|
|
152 |
|
|
|
153 |
switch ($rand) {
|
|
|
154 |
case 1:
|
|
|
155 |
$node->setAttribute('class', $this->generator->word());
|
|
|
156 |
|
|
|
157 |
break;
|
|
|
158 |
|
|
|
159 |
case 2:
|
|
|
160 |
$node->setAttribute('id', (string) $this->idGenerator->randomNumber(5));
|
|
|
161 |
|
|
|
162 |
break;
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
private function addRandomP(\DOMElement $element, $maxLength = 10): void
|
|
|
167 |
{
|
|
|
168 |
$node = $element->ownerDocument->createElement(static::P_TAG);
|
|
|
169 |
$node->textContent = $this->generator->sentence(self::numberBetween(1, $maxLength));
|
|
|
170 |
$element->appendChild($node);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
private function addRandomText(\DOMElement $element, $maxLength = 10): void
|
|
|
174 |
{
|
|
|
175 |
$text = $element->ownerDocument->createTextNode($this->generator->sentence(self::numberBetween(1, $maxLength)));
|
|
|
176 |
$element->appendChild($text);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
private function addRandomA(\DOMElement $element, $maxLength = 10): void
|
|
|
180 |
{
|
|
|
181 |
$text = $element->ownerDocument->createTextNode($this->generator->sentence(self::numberBetween(1, $maxLength)));
|
|
|
182 |
$node = $element->ownerDocument->createElement(static::A_TAG);
|
|
|
183 |
$node->setAttribute('href', $this->generator->safeEmailDomain());
|
|
|
184 |
$node->appendChild($text);
|
|
|
185 |
$element->appendChild($node);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
private function addRandomTitle(\DOMElement $element, $maxLength = 10): void
|
|
|
189 |
{
|
|
|
190 |
$text = $element->ownerDocument->createTextNode($this->generator->sentence(self::numberBetween(1, $maxLength)));
|
|
|
191 |
$node = $element->ownerDocument->createElement(static::TITLE_TAG);
|
|
|
192 |
$node->appendChild($text);
|
|
|
193 |
$element->appendChild($node);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private function addRandomH(\DOMElement $element, $maxLength = 10): void
|
|
|
197 |
{
|
|
|
198 |
$h = static::H_TAG . (string) self::numberBetween(1, 3);
|
|
|
199 |
$text = $element->ownerDocument->createTextNode($this->generator->sentence(self::numberBetween(1, $maxLength)));
|
|
|
200 |
$node = $element->ownerDocument->createElement($h);
|
|
|
201 |
$node->appendChild($text);
|
|
|
202 |
$element->appendChild($node);
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
private function addRandomB(\DOMElement $element, $maxLength = 10): void
|
|
|
206 |
{
|
|
|
207 |
$text = $element->ownerDocument->createTextNode($this->generator->sentence(self::numberBetween(1, $maxLength)));
|
|
|
208 |
$node = $element->ownerDocument->createElement(static::B_TAG);
|
|
|
209 |
$node->appendChild($text);
|
|
|
210 |
$element->appendChild($node);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
private function addRandomI(\DOMElement $element, $maxLength = 10): void
|
|
|
214 |
{
|
|
|
215 |
$text = $element->ownerDocument->createTextNode($this->generator->sentence(self::numberBetween(1, $maxLength)));
|
|
|
216 |
$node = $element->ownerDocument->createElement(static::I_TAG);
|
|
|
217 |
$node->appendChild($text);
|
|
|
218 |
$element->appendChild($node);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
private function addRandomSpan(\DOMElement $element, $maxLength = 10): void
|
|
|
222 |
{
|
|
|
223 |
$text = $element->ownerDocument->createTextNode($this->generator->sentence(self::numberBetween(1, $maxLength)));
|
|
|
224 |
$node = $element->ownerDocument->createElement(static::SPAN_TAG);
|
|
|
225 |
$node->appendChild($text);
|
|
|
226 |
$element->appendChild($node);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
private function addLoginForm(\DOMElement $element): void
|
|
|
230 |
{
|
|
|
231 |
$textInput = $element->ownerDocument->createElement(static::INPUT_TAG);
|
|
|
232 |
$textInput->setAttribute('type', 'text');
|
|
|
233 |
$textInput->setAttribute('id', 'username');
|
|
|
234 |
|
|
|
235 |
$textLabel = $element->ownerDocument->createElement(static::LABEL_TAG);
|
|
|
236 |
$textLabel->setAttribute('for', 'username');
|
|
|
237 |
$textLabel->textContent = $this->generator->word();
|
|
|
238 |
|
|
|
239 |
$passwordInput = $element->ownerDocument->createElement(static::INPUT_TAG);
|
|
|
240 |
$passwordInput->setAttribute('type', 'password');
|
|
|
241 |
$passwordInput->setAttribute('id', 'password');
|
|
|
242 |
|
|
|
243 |
$passwordLabel = $element->ownerDocument->createElement(static::LABEL_TAG);
|
|
|
244 |
$passwordLabel->setAttribute('for', 'password');
|
|
|
245 |
$passwordLabel->textContent = $this->generator->word();
|
|
|
246 |
|
|
|
247 |
$submit = $element->ownerDocument->createElement(static::INPUT_TAG);
|
|
|
248 |
$submit->setAttribute('type', 'submit');
|
|
|
249 |
$submit->setAttribute('value', $this->generator->word());
|
|
|
250 |
|
|
|
251 |
$submit = $element->ownerDocument->createElement(static::FORM_TAG);
|
|
|
252 |
$submit->setAttribute('action', $this->generator->safeEmailDomain());
|
|
|
253 |
$submit->setAttribute('method', 'POST');
|
|
|
254 |
$submit->appendChild($textLabel);
|
|
|
255 |
$submit->appendChild($textInput);
|
|
|
256 |
$submit->appendChild($passwordLabel);
|
|
|
257 |
$submit->appendChild($passwordInput);
|
|
|
258 |
$element->appendChild($submit);
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
private function addRandomTable(\DOMElement $element, $maxRows = 10, $maxCols = 6, $maxTitle = 4, $maxLength = 10): void
|
|
|
262 |
{
|
|
|
263 |
$rows = self::numberBetween(1, $maxRows);
|
|
|
264 |
$cols = self::numberBetween(1, $maxCols);
|
|
|
265 |
|
|
|
266 |
$table = $element->ownerDocument->createElement(static::TABLE_TAG);
|
|
|
267 |
$thead = $element->ownerDocument->createElement(static::THEAD_TAG);
|
|
|
268 |
$tbody = $element->ownerDocument->createElement(static::TBODY_TAG);
|
|
|
269 |
|
|
|
270 |
$table->appendChild($thead);
|
|
|
271 |
$table->appendChild($tbody);
|
|
|
272 |
|
|
|
273 |
$tr = $element->ownerDocument->createElement(static::TR_TAG);
|
|
|
274 |
$thead->appendChild($tr);
|
|
|
275 |
|
|
|
276 |
for ($i = 0; $i < $cols; ++$i) {
|
|
|
277 |
$th = $element->ownerDocument->createElement(static::TH_TAG);
|
|
|
278 |
$th->textContent = $this->generator->sentence(self::numberBetween(1, $maxTitle));
|
|
|
279 |
$tr->appendChild($th);
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
for ($i = 0; $i < $rows; ++$i) {
|
|
|
283 |
$tr = $element->ownerDocument->createElement(static::TR_TAG);
|
|
|
284 |
$tbody->appendChild($tr);
|
|
|
285 |
|
|
|
286 |
for ($j = 0; $j < $cols; ++$j) {
|
|
|
287 |
$th = $element->ownerDocument->createElement(static::TD_TAG);
|
|
|
288 |
$th->textContent = $this->generator->sentence(self::numberBetween(1, $maxLength));
|
|
|
289 |
$tr->appendChild($th);
|
|
|
290 |
}
|
|
|
291 |
}
|
|
|
292 |
$element->appendChild($table);
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
private function addRandomUL(\DOMElement $element, $maxItems = 11, $maxLength = 4): void
|
|
|
296 |
{
|
|
|
297 |
$num = self::numberBetween(1, $maxItems);
|
|
|
298 |
$ul = $element->ownerDocument->createElement(static::UL_TAG);
|
|
|
299 |
|
|
|
300 |
for ($i = 0; $i < $num; ++$i) {
|
|
|
301 |
$li = $element->ownerDocument->createElement(static::LI_TAG);
|
|
|
302 |
$li->textContent = $this->generator->sentence(self::numberBetween(1, $maxLength));
|
|
|
303 |
$ul->appendChild($li);
|
|
|
304 |
}
|
|
|
305 |
$element->appendChild($ul);
|
|
|
306 |
}
|
|
|
307 |
}
|