You are here

protected function SolrFieldType::buildXmlFromArray in Search API Solr 8.2

2 calls to SolrFieldType::buildXmlFromArray()
SolrFieldType::getFieldTypeAsXml in src/Entity/SolrFieldType.php
Gets the Solr Field Type definition as XML fragment.
SolrFieldType::getSolrConfigsAsXml in src/Entity/SolrFieldType.php
Gets the Solr Field Type specific additions to solrconfig.xml as XML.

File

src/Entity/SolrFieldType.php, line 246

Class

SolrFieldType
Defines the SolrFieldType entity.

Namespace

Drupal\search_api_solr\Entity

Code

protected function buildXmlFromArray($root_element_name, array $attributes) {
  $root = new \SimpleXMLElement('<' . $root_element_name . '/>');
  $f = function (\SimpleXMLElement $element, array $attributes) use (&$f) {
    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);
            $f($child, $inner_attributes);
          }
        }
        else {
          $child = $element
            ->addChild($key);
          $f($child, $value);
        }
      }
    }
  };
  $f($root, $attributes);

  // Create formatted string.
  $dom = dom_import_simplexml($root)->ownerDocument;
  $dom->formatOutput = TRUE;
  $formatted_xml_string = $dom
    ->saveXML();

  // Remove the XML declaration before returning the XML fragment.
  return preg_replace('/<\\?.*?\\?>\\s*\\n?/', '', $formatted_xml_string);
}