You are here

public function DomHelperTrait::getNodeAttributesAsArray in Embed 8

Convert the attributes on a DOMNode object to an array.

This will also un-serialize any attribute values stored as JSON.

Parameters

\DOMNode $node: A DOMNode object.

Return value

array The attributes as an associative array, keyed by the attribute names.

1 call to DomHelperTrait::getNodeAttributesAsArray()
DomHelperTraitTest::testGetNodeAttributesAsArray in tests/src/Unit/DomHelperTraitTest.php
Test DomHelperTrait::getNodeAttributesAsArray().

File

src/DomHelperTrait.php, line 115

Class

DomHelperTrait
Wrapper methods for manipulating DOM entries.

Namespace

Drupal\embed

Code

public function getNodeAttributesAsArray(\DOMNode $node) {
  $return = [];

  // Convert the data attributes to the context array.
  foreach ($node->attributes as $attribute) {
    $key = $attribute->nodeName;
    $return[$key] = $attribute->nodeValue;

    // Check for JSON-encoded attributes.
    $data = json_decode($return[$key], TRUE, 10);
    if ($data !== NULL && json_last_error() === JSON_ERROR_NONE) {
      $return[$key] = $data;
    }
  }
  return $return;
}