Skip to content
Thomas Weinert edited this page Aug 16, 2014 · 17 revisions

Creator

(FluentDOM 5.1)

The FluentDOM\Nodes\Creator is a functor, a class usable like a function. In PHP you implement the magic method __invoke to support this. It allows you to have a function with a state/configuration.

You can use the FluentDOM::create() method to get a new instance and assign it to a variable.

Basic Usage

The first argument of the function is the node name. The returned object can be cast to string.

$_ = FluentDOM::create();
echo $_('ul');

Output

<?xml version="1.0" encoding="UTF-8"?>
<ul/>

Attributes And Child Nodes

Other arguments can set attributes (arrays or attribute nodes) and add text nodes (string) or child elements.

$_ = FluentDOM::create();
echo $_(
  'ul',
  ['class' => 'navigation'],
  $_('li', 'FluentDOM')
);

Output

<?xml version="1.0" encoding="UTF-8"?>
<ul class="navigation"><li>FluentDOM</li></ul>

Creating Specific Nodes

XML supports several types of nodes. Here a several methods for creating specific nodes:

  • Creator::cdata() - CData Section
  • Creator::comment() - Comment
  • Creator::pi() - Processing Instruction
$_ = FluentDOM::create();

echo $_(
  'ul',
  ['class' => 'navigation'],
  $_(
    'li',
    $_->cdata('FluentDOM')
  )
);

Output

<?xml version="1.0" encoding="UTF-8"?>
<ul class="navigation"><li><![CDATA[FluentDOM]]></li></ul>

Accessing The Document

If you case the result into a string it will save it as XML. Reading the document property return the created nodes in a DOM document. One possible use case would be generating HTML.

$_ = FluentDOM::create();

echo $_(
  'ul',
  ['class' => 'navigation'],
  $_(
    'li',
    $_('a', ['href' => 'http://fluentdom.org'], 'FluentDOM')
  )
)->document->saveHtml();

Output

<ul class="navigation"><li><a href="http://fluentdom.org">FluentDOM</a></li></ul>

Iterating Data Sources

Creator::any() provides a way to iterate a data source. The first argument is the data source. This could be a list of nodes from another document or an array. The second argument is a mapping function. It will be called for each item from the data source. If you allow the creator into the mapping function you can use it to create nodes from items.

$_ = FluentDOM::create();
$_->formatOutput = TRUE;

$links = [
  'http://www.fluentdom.org' => 'FluentDOM',
  'http://www.php.net' => 'PHP'
];

echo $_(
  'ul',
  ['class' => 'navigation'],
  $_->any(
    $links,
    function($text, $href) use($_) {
      return $_('li', $_('a', ['href' => $href], $text));
    }
  )
)->document->saveHtml();

Output

<ul class="navigation">
<li><a href="http://www.fluentdom.org">FluentDOM</a></li>
<li><a href="http://www.php.net">PHP</a></li>
</ul>

Append The Result

The return value of the Creator functor implements FluentDOM\Appendable. This allows to appedn created fragments to existing documents.

$dom = new FluentDOM\Document();
$dom->loadHtml(
  '<html><body><div id="navigation"/></body></html>'
);

$_ = FluentDOM::create();
$dom
  ->getElementById('navigation')
  ->append(
    $_(
      'ul',
      ['class' => 'navigation'],
      $_(
        'li',
        $_('a', ['href' => 'http://fluentdom.org'], 'FluentDOM')
      )
    )
  );

echo $dom->saveHtml();

Output

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div id="navigation">
<ul class="navigation"><li><a href="http://fluentdom.org">FluentDOM</a></li></ul>
</div></body></html>
Clone this wiki locally