<?php

/**
 * @psalm-type ElementValue string|array
 * @psalm-type ElementValues array<array-key, ElementValue>
 */
class Awardit_Magento_EnvConfig_Data extends Mage_Core_Model_Config_Base {
    /**
     * @param ElementValues $data
     */
    public function __construct(array $data) {
        /** @psalm-suppress UnsafeInstantiation - possibly don't do what we expect */
        $config = new $this->_elementClass('<?xml version="1.0"?><config/>');

        self::createSimpleXML($config, $data);

        $this->_xml = $config;
    }

    /**
     * @param ElementValues $data
     */
    private static function createSimpleXML(SimpleXMLElement $el, array $data): void {
        if(isset($data[0])) {
            foreach($data as $value) {
                self::createSimpleXML($el, $value);
            }
        }
        else {
            foreach($data as $key => $value) {
                if(is_array($value)) {
                    self::createSimpleXML($el->addChild($key), $value);
                }
                else {
                    $el->addChild($key, $value);
                }
            }
        }
    }
}
