diff --git a/Faker/Provider/Picture.php b/Faker/Provider/Picture.php index a8d8d14..fcb789a 100644 --- a/Faker/Provider/Picture.php +++ b/Faker/Provider/Picture.php @@ -6,7 +6,7 @@ /** * Almost the same as Image from Faker, but uses picsum.photos - * @todo add support for https://loremflickr.com/ as an alternative + * @todo add support for https://loremflickr.com/ as an alternative, or see the lst at https://www.johanbostrom.se/blog/the-best-image-placeholder-services-on-the-web */ class Picture extends Base { diff --git a/Faker/Provider/XmlText.php b/Faker/Provider/XmlText.php new file mode 100644 index 0000000..a4d2cdc --- /dev/null +++ b/Faker/Provider/XmlText.php @@ -0,0 +1,258 @@ +idGenerator = new UniqueGenerator($this->generator); + + $body = $document->createElement("section"); + $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xhtml', 'http://ez.no/namespaces/ezpublish3/xhtml/'); + $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:image', 'http://ez.no/namespaces/ezpublish3/image/'); + $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:custom', 'http://ez.no/namespaces/ezpublish3/custom/'); + $this->addRandomSubTree($body, $maxDepth, $maxWidth); + + $document->appendChild($body); + + $out = $document->saveXML(); + + return $out; + } + + 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("section"); + $root->appendChild($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->addRandomE($node); + break; + case 2: + $this->addRandomA($node); + break; + case 3: + $this->addRandomOL($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; + case 9: + $this->addRandomU($node); + break; + default: + $this->addRandomP($node); + break; + } + } + + private function addRandomE(\DOMElement $element) + { + $p = $element->ownerDocument->createElement(static::P_TAG); + $node = $element->ownerDocument->createElement(static::E_TAG); + $node->setAttribute("view", "embed"); + $node->setAttribute("size", "medium"); + /// @todo improve: generation of a radmon object id + $node->setAttribute("object_id", mt_rand(1, 100000)); + $p->appendChild($node); + $element->appendChild($p); + } + + /** + * @todo add random align + * @param \DOMElement $element + * @param int $maxLength + */ + private function addRandomP(\DOMElement $element, $maxLength = 30) + { + $p = $element->ownerDocument->createElement(static::P_TAG); + $text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength))); + // left-aligned paragraphs have double frequency + switch (mt_rand(1, 4)) { + case 1: + $p->setAttribute("align", "right"); + break; + case 2: + $p->setAttribute("align", "center"); + break; + } + $p->appendChild($text); + $element->appendChild($p); + } + + private function addRandomA(\DOMElement $element, $maxLength = 10) + { + $p = $element->ownerDocument->createElement(static::P_TAG); + $text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength))); + $node = $element->ownerDocument->createElement(static::A_TAG); + $node->setAttribute("url", $this->generator->url); + $node->appendChild($text); + $this->wrapInParagraph($node, $element); + } + + private function addRandomH(\DOMElement $element, $maxLength = 10) + { + $h = static::H_TAG; + $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); + $this->wrapInParagraph($node, $element); + } + + private function addRandomU(\DOMElement $element, $maxLength = 10) + { + $text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength))); + $node = $element->ownerDocument->createElement(static::CUSTOM_TAG); + $node->setAttribute('name', 'underline'); + $node->appendChild($text); + $this->wrapInParagraph($node, $element); + } + + 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); + $this->wrapInParagraph($node, $element); + } + + 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); + + for ($i = 0; $i < $rows; $i++) { + $tr = $element->ownerDocument->createElement(static::TR_TAG); + $table->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); + } + } + $this->wrapInParagraph($table, $element); + } + + 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); + $lip = $element->ownerDocument->createElement(static::P_TAG); + $lip->textContent = $this->generator->sentence(mt_rand(1, $maxLength)); + $li->appendChild($lip); + $ul->appendChild($li); + } + $this->wrapInParagraph($ul, $element); + } + + private function addRandomOL(\DOMElement $element, $maxItems = 11, $maxLength = 4) + { + $num = mt_rand(1, $maxItems); + $ul = $element->ownerDocument->createElement(static::OL_TAG); + for ($i = 0; $i < $num; $i++) { + $li = $element->ownerDocument->createElement(static::LI_TAG); + $lip = $element->ownerDocument->createElement(static::P_TAG); + $lip->textContent = $this->generator->sentence(mt_rand(1, $maxLength)); + $li->appendChild($lip); + $ul->appendChild($li); + } + $this->wrapInParagraph($ul, $element); + } + + private function wrapInParagraph(\DOMElement $element, \DOMElement $parent, $maxLength = 10) + { + $p = $element->ownerDocument->createElement(static::P_TAG); + $chance = mt_rand(1, 3); + if ($chance == 1) { + $text = $element->ownerDocument->createTextNode($this->generator->sentence(mt_rand(1, $maxLength)) . ' '); + $p->appendChild($text); + } + $p->appendChild($element); + if ($chance == 2) { + $text = $element->ownerDocument->createTextNode(' ' . $this->generator->sentence(mt_rand(1, $maxLength))); + $p->appendChild($text); + } + $parent->appendChild($p); + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 1734bda..12615a4 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -5,6 +5,7 @@ parameters: # add custom providers to faker ez_loremipsum_bundle.faker.providers: - "Kaliop\\eZLoremIpsumBundle\\Faker\\Provider\\Picture" + - "Kaliop\\eZLoremIpsumBundle\\Faker\\Provider\\XmlText" ez_loremipsum_bundle.reference_resolver.faker.class: Kaliop\eZLoremIpsumBundle\Core\ReferenceResolver\FakerResolver diff --git a/Resources/doc/DSL/Faker.md b/Resources/doc/DSL/Faker.md index 5ad72af..3ff2e49 100644 --- a/Resources/doc/DSL/Faker.md +++ b/Resources/doc/DSL/Faker.md @@ -38,8 +38,14 @@ you can use this instead: * The full list of default providers is documented at https://github.com/fzaninotto/Faker -* Coming with the bundle is a provider that registers the `picture` and `pictureUrl` properties. - Those behave almost exactly like the native `image` and `imageUrl` ones, except for not supporting the 'category'. - They retrieve the images from service picsum.photos instead of lorempixel. +* Coming with the bundle are providers that register: + + * the `picture` and `pictureUrl` properties. + Those behave almost exactly like the native `image` and `imageUrl` ones, except for not supporting the 'category'. + They retrieve the images from service picsum.photos instead of lorempixel. + + * the `randomXmlText($maxDepth=4, $maxWidth=4)` property. + It works as the original `randomHtml` property, except that it generates rich text compatible with the XmlText + field type. * It is also possible to register custom providers from your own code. diff --git a/WHATSNEW.md b/WHATSNEW.md index cf17f64..59021a7 100644 --- a/WHATSNEW.md +++ b/WHATSNEW.md @@ -1,3 +1,11 @@ +Version 1.2 +=========== + +* New: the bundle now includes a custom Faker provider that registers the `randomXmlText($maxDepth=4, $maxWidth=4)` property. + It works as the original `randomHtml` provider, except that it generates rich text compatible with the XmlText + field type. + + Version 1.1 =========== @@ -40,6 +48,6 @@ Version 1.1 * New: it is now possible register custom Faker providers via the parameter `ez_loremipsum_bundle.faker.providers` -* New: the bundle now includes a custom Faker providers that registers the `picture` and `pictureUrl` properties. +* New: the bundle now includes a custom Faker provider that registers the `picture` and `pictureUrl` properties. Those behave almost exactly like the native `image` and `imageUrl` ones, except for not supporting the 'category'. They retrieve the images from service picsum.photos instead of lorempixel.