<?php

class MXMLFlow
{
    public $root;
    public $params;

    public function __construct()
    {
    }

    public function loadFile( $xmlFile, $params = NULL)
    {
        libxml_use_internal_errors(true);
        $this->root = simplexml_load_file($xmlFile);
        if (!$this->root)
        {
            foreach (libxml_get_errors() as $error)
            {
                mdump($error);
            }
            libxml_clear_errors();
        }
        $this->params = $params;
    }

    public function loadString( $xmlString, $params = NULL)
    {
        $this->root = simplexml_load_string($xmlString);
        $this->params = $params;
    }

    public function get($nodeName)
    {
        $node = $this->getNodesFromDOM($this->root->$nodeName);
        return $node;
    }

    private function getNodesFromDOM($node)
    {
        $actions = array();
        if ($node)
        {
            foreach($node->children() as $class=>$n)
            {
                $action = new MDataObject;
                $actions[] = $action;
                $this->getNodeFromDOM($action, $class, $n);
            }
        }
        return $actions;
    }

    private function getNodeFromDOM($action,$c,$node)
    {
        $params = $this->params;
        $flow = $this->params->__flow;
        foreach ($node->attributes() as $k => $v)
        {
            $v = utf8_decode((string)$v);
            if ($k == "method")
            {
                $v = substr(substr($v,2),0,-1);
                eval("{$v};");
            }
            else if (substr($v,0,1) == '$')
            {
                $v = substr(substr($v,2),0,-1);
                $action->$k = eval('return ' . $v .';');
            }
            else
            {
                $action->$k = $v;
            }
        }
        $i = 0;
        foreach($node->children() as $f=>$v)
        {
            $obj = new MDataObject;       
            $this->getNodeFromDOM($obj,$f,$v);
            $action->flow[$i++] = $obj;
        }
    }
}
?>
