You are here

private static function Braintree_Xml_Generator::_createElementsFromArray in Commerce Braintree 7

Construct XML elements with attributes from an associative array.

@access protected @static

Parameters

object $writer XMLWriter object:

array $aData contains attributes and values:

Return value

none

1 call to Braintree_Xml_Generator::_createElementsFromArray()
Braintree_Xml_Generator::arrayToXml in braintree_php/lib/Braintree/Xml/Generator.php
arrays passed to this method should have a single root element with an array as its value

File

braintree_php/lib/Braintree/Xml/Generator.php, line 57

Class

Braintree_Xml_Generator
Generates XML output from arrays using PHP's built-in XMLWriter

Code

private static function _createElementsFromArray(&$writer, $aData) {
  if (!is_array($aData)) {
    if (is_bool($aData)) {
      $writer
        ->text($aData ? 'true' : 'false');
    }
    else {
      $writer
        ->text($aData);
    }
    return;
  }
  foreach ($aData as $index => $element) {

    // convert the style back to gateway format
    $elementName = Braintree_Util::camelCaseToDelimiter($index, '-');

    // handle child elements
    $writer
      ->startElement($elementName);
    if (is_array($element)) {
      if (array_key_exists(0, $element) || empty($element)) {
        $writer
          ->writeAttribute('type', 'array');
        foreach ($element as $ignored => $itemInArray) {
          $writer
            ->startElement('item');
          self::_createElementsFromArray($writer, $itemInArray);
          $writer
            ->endElement();
        }
      }
      else {
        self::_createElementsFromArray($writer, $element);
      }
    }
    else {

      // generate attributes as needed
      $attribute = self::_generateXmlAttribute($element);
      if (is_array($attribute)) {
        $writer
          ->writeAttribute($attribute[0], $attribute[1]);
        $element = $attribute[2];
      }
      $writer
        ->text($element);
    }
    $writer
      ->endElement();
  }
}