Skip to content

Commit

Permalink
Add support for filling ez xml-text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaetano Giunta committed May 15, 2018
1 parent 986876c commit 9297043
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Faker/Provider/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
258 changes: 258 additions & 0 deletions Faker/Provider/XmlText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
<?php

namespace Kaliop\eZLoremIpsumBundle\Faker\Provider;

use Faker\Provider\Base;
use Faker\UniqueGenerator;
use Faker\Generator;

/**
* NB: A very crude implementation for now
* @see https://github.com/ezsystems/ezpublish-kernel/tree/master/eZ/Publish/Core/FieldType/Tests/RichText/Converter/Xslt/_fixtures/ezxml
*/
class XmlText extends Base
{
const P_TAG = "paragraph";
const A_TAG = "link";
const E_TAG = "embed";
const TABLE_TAG = "table";
const TR_TAG = "tr";
const TD_TAG = "td";
const UL_TAG = "ul";
const OL_TAG = "ol";
const LI_TAG = "li";
const H_TAG = "header";
const B_TAG = "strong";
const I_TAG = "emphasize";
const CUSTOM_TAG = "custom";

private $idGenerator;

public function __construct(Generator $generator)
{
parent::__construct($generator);
}

/**
* @param integer $maxDepth
* @param integer $maxWidth
*
* @return string
*/
public function randomXmlText($maxDepth = 4, $maxWidth = 4)
{
$document = new \DOMDocument();
$this->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);
}
}
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions Resources/doc/DSL/Faker.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 9 additions & 1 deletion WHATSNEW.md
Original file line number Diff line number Diff line change
@@ -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
===========

Expand Down Expand Up @@ -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.

0 comments on commit 9297043

Please sign in to comment.