PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/fakerphp/faker/src/Faker/Provider/HtmlLorem.php

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