<?php

class XmlScreen
{

    public $xw;

    public function __construct($type = "TiptelIPPhoneTextScreen", $attributes = array())
    {
        $this->xw = new xmlWriter();
        $this->xw->openMemory();
        $this->xw->setIndent(TRUE);
        $this->xw->startDocument('1.0', 'UTF-8'); // start document

        $this->xw->startElement($type);
        foreach ($attributes as $key => $value) {
            $this->xw->writeAttribute($key, $value);
        }
    }

    private function finishDocument()
    {
        $this->xw->endElement(); // close node <book>
        $this->xw->endDocument(); // end document
    }

    public function printScreen()
    {
        $this->finishDocument();
        echo $this->xw->outputMemory();
    }

    public function setTitle($title, $wrap = "yes")
    {
        $this->xw->startElement('Title');  // <title> with content
        $this->xw->writeAttribute("wrap", $wrap);
        $this->xw->text($title);
        $this->xw->endElement();
    }

    public function addText($text)
    {
        $this->xw->writeElement("Text", $text);
    }

    public function addButton($index, $label, $URI = "SoftKey:Select")
    {
        $this->xw->startElement("SoftKey");
        $this->xw->writeAttribute("index", $index);
        $this->xw->writeElement("Label", $label);
        $this->xw->writeElement("URI", $URI);
        $this->xw->endElement();
    }

    public function removeDefaultButtons()
    {
        foreach (range(1, 4) as $key) {
            $this->addButton($key, "", "");
        }
    }
}
