TrackManiaControl/application/core/FML/ManiaLinks.php

100 lines
2.0 KiB
PHP
Raw Normal View History

2013-12-03 23:02:31 +01:00
<?php
namespace FML;
/**
2013-12-09 13:05:05 +01:00
* Class holding several Manialinks at once
2013-12-03 23:02:31 +01:00
*
* @author steeffeen
*/
class ManiaLinks {
/**
2013-12-31 02:55:19 +01:00
* Protected Properties
2013-12-03 23:02:31 +01:00
*/
protected $encoding = 'utf-8';
protected $tagName = 'manialinks';
protected $children = array();
2014-01-03 17:18:14 +01:00
protected $customUI = null;
2013-12-03 23:02:31 +01:00
/**
2013-12-31 02:55:19 +01:00
* Set XML Encoding
2013-12-03 23:02:31 +01:00
*
2014-01-03 17:18:14 +01:00
* @param string $encoding XML Encoding
2013-12-03 23:02:31 +01:00
* @return \FML\ManiaLinks
*/
public function setXmlEncoding($encoding) {
$this->encoding = $encoding;
return $this;
}
/**
2013-12-31 02:55:19 +01:00
* Add a Child Manialink
2013-12-03 23:02:31 +01:00
*
2014-01-03 17:18:14 +01:00
* @param ManiaLink $child Child Manialink
2013-12-03 23:02:31 +01:00
* @return \FML\ManiaLinks
*/
public function add(ManiaLink $child) {
2013-12-31 02:55:19 +01:00
if (!in_array($child, $this->children)) {
array_push($this->children, $child);
}
2013-12-03 23:02:31 +01:00
return $this;
}
/**
2013-12-31 02:55:19 +01:00
* Remove all Child Manialinks
2013-12-03 23:02:31 +01:00
*
* @return \FML\ManiaLinks
*/
public function removeChildren() {
$this->children = array();
return $this;
}
2014-01-03 17:18:14 +01:00
/**
* Set the CustomUI
*
* @param CustomUI $customUI The CustomUI Object
* @return \FML\ManiaLinks
*/
public function setCustomUI(CustomUI $customUI) {
$this->customUI = $customUI;
return $this;
}
2013-12-03 23:02:31 +01:00
/**
2013-12-31 02:55:19 +01:00
* Render the XML Document
2013-12-03 23:02:31 +01:00
*
2014-01-03 17:18:14 +01:00
* @param bool $echo If the XML should be echoed and the Content-Type Header should be set
2013-12-03 23:02:31 +01:00
* @return \DOMDocument
*/
public function render($echo = false) {
$domDocument = new \DOMDocument('1.0', $this->encoding);
$manialinks = $domDocument->createElement($this->tagName);
$domDocument->appendChild($manialinks);
2013-12-03 23:02:31 +01:00
foreach ($this->children as $child) {
$childXml = $child->render(false, $domDocument);
$manialinks->appendChild($childXml);
2013-12-03 23:02:31 +01:00
}
2014-01-03 17:18:14 +01:00
if ($this->customUI) {
$customUIXml = $this->customUI->render($domDocument);
$manialinks->appendChild($customUIXml);
}
2013-12-03 23:02:31 +01:00
if ($echo) {
header('Content-Type: application/xml');
echo $domDocument->saveXML();
}
return $domDocument;
}
2014-01-05 19:15:49 +01:00
/**
* Get String Representation
*
* @return string
*/
public function __toString() {
$domDocument = $this->render();
$xmlText = $domDocument->saveXML();
return $xmlText;
}
2013-12-03 23:02:31 +01:00
}