Api\V1\Engine\Api::arrayToXML PHP Method

arrayToXML() private static method

Callback-method for elements in the return-array
private static arrayToXML ( mixed &$input, string $key, DOMElement $XML )
$input mixed The value.
$key string The key.
$XML DOMElement The root-element.
    private static function arrayToXML(&$input, $key, $XML)
    {
        // skip attributes
        if ($key == '@attributes') {
            return;
        }
        // create element
        $element = new \DOMElement($key);
        // append
        $XML->appendChild($element);
        // no value? just stop here
        if ($input === null) {
            return;
        }
        // is it an array and are there attributes
        if (is_array($input) && isset($input['@attributes'])) {
            // loop attributes
            foreach ((array) $input['@attributes'] as $name => $value) {
                $element->setAttribute($name, $value);
            }
            // remove attributes
            unset($input['@attributes']);
            // reset the input if it is a single value
            if (count($input) == 1) {
                // get keys
                $keys = array_keys($input);
                // reset
                $input = $input[$keys[0]];
            }
        }
        // the input isn't an array
        if (!is_array($input)) {
            // a string?
            if (is_string($input)) {
                // characters that require a cdata wrapper
                $illegalCharacters = array('&', '<', '>', '"', '\'');
                // default we don't wrap with cdata tags
                $wrapCdata = false;
                // find illegal characters in input string
                foreach ($illegalCharacters as $character) {
                    if (mb_stripos($input, $character) !== false) {
                        // wrap input with cdata
                        $wrapCdata = true;
                        // no need to search further
                        break;
                    }
                }
                // check if value contains illegal chars, if so wrap in CDATA
                if ($wrapCdata) {
                    $element->appendChild(new \DOMCdataSection($input));
                } else {
                    // just regular element
                    $element->appendChild(new \DOMText($input));
                }
            } else {
                // regular element
                $element->appendChild(new \DOMText($input));
            }
        } else {
            // the value is an array
            $isNonNumeric = false;
            // loop all elements
            foreach ($input as $index => $value) {
                // non numeric string as key?
                if (!is_numeric($index)) {
                    // reset var
                    $isNonNumeric = true;
                    // stop searching
                    break;
                }
            }
            // is there are named keys they should be handles as elements
            if ($isNonNumeric) {
                array_walk($input, array('Api\\V1\\Engine\\Api', 'arrayToXML'), $element);
            } else {
                // numeric elements means this a list of items
                // handle the value as an element
                foreach ($input as $value) {
                    if (is_array($value)) {
                        array_walk($value, array('Api\\V1\\Engine\\Api', 'arrayToXML'), $element);
                    }
                }
            }
        }
    }