You are here

protected static function AbstractSolrEntity::buildXmlFromArrayRecursive in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Entity/AbstractSolrEntity.php \Drupal\search_api_solr\Entity\AbstractSolrEntity::buildXmlFromArrayRecursive()

Builds a SimpleXMLElement recursively from an array of attributes.

Parameters

\SimpleXMLElement $element: The root SimpleXMLElement.

array $attributes: An associative array of key/value attributes. Can be multi-level.

1 call to AbstractSolrEntity::buildXmlFromArrayRecursive()
AbstractSolrEntity::buildXmlFromArray in src/Entity/AbstractSolrEntity.php
Formats a given array to an XML string.

File

src/Entity/AbstractSolrEntity.php, line 91

Class

AbstractSolrEntity
Defines the abstract base class for Solr config entities.

Namespace

Drupal\search_api_solr\Entity

Code

protected static function buildXmlFromArrayRecursive(\SimpleXMLElement $element, array $attributes) {
  foreach ($attributes as $key => $value) {
    if (is_scalar($value)) {
      if (is_bool($value) === TRUE) {

        // SimpleXMLElement::addAtribute() converts booleans to integers 0
        // and 1. But Solr requires the strings 'false' and 'true'.
        $value = $value ? 'true' : 'false';
      }
      switch ($key) {
        case 'VALUE':

          // @see https://stackoverflow.com/questions/3153477
          $element[0] = $value;
          break;
        case 'CDATA':
          $element[0] = '<![CDATA[' . $value . ']]>';
          break;
        default:
          $element
            ->addAttribute($key, $value);
      }
    }
    elseif (is_array($value)) {
      if (array_key_exists(0, $value)) {
        $key = rtrim($key, 's');
        foreach ($value as $inner_attributes) {
          $child = $element
            ->addChild($key);
          self::buildXmlFromArrayRecursive($child, $inner_attributes);
        }
      }
      else {
        $child = $element
          ->addChild($key);
        self::buildXmlFromArrayRecursive($child, $value);
      }
    }
  }
}