You are here

protected static function TreeEncodeTrait::encodeTree in Extensible BBCode 4.0.x

Same name and namespace in other branches
  1. 8.3 standard/src/TreeEncodeTrait.php \Drupal\xbbcode_standard\TreeEncodeTrait::encodeTree()

Concatenate the top-level text of the tree.

The text elements are concatenated, inserting placeholders for each tag element contained in the children.

Parameters

array $children: A sequence of elements representing the children of a single element.

Return value

string[] An array with two values:

  • A unique delimiter token.
  • A concatenated string, in which the i-th tag element is replaced with a placeholder string "{token:i}", and text elements are left in place.
2 calls to TreeEncodeTrait::encodeTree()
ListTagPlugin::splitContent in standard/src/Plugin/XBBCode/ListTagPlugin.php
Split the tag's children into list items.
TableTagPlugin::tabulateTree in standard/src/Plugin/XBBCode/TableTagPlugin.php
Helper that turns a parse tree into a table of cells.

File

standard/src/TreeEncodeTrait.php, line 37

Class

TreeEncodeTrait
Static helper functions for tag plugins that parse their top-level text.

Namespace

Drupal\xbbcode_standard

Code

protected static function encodeTree(array $children) : array {
  $output = [];
  foreach ($children as $i => $child) {
    if ($child instanceof TextElement) {
      $output[] = $child
        ->getText();
    }
    else {
      $output[] = $i;
    }
  }
  $text = implode('', $output);
  $token = 100000;
  while (strpos($text, (string) $token) !== FALSE) {
    $token++;
  }
  foreach ($output as $i => $item) {
    if (is_int($item)) {
      $output[$i] = "{tag:{$token}:{$item}}";
    }
  }
  return [
    $token,
    implode('', $output),
  ];
}