TrackManiaControl/application/core/FML/ManiaLink.php

199 lines
4.3 KiB
PHP
Raw Normal View History

2014-01-03 17:18:14 +01:00
<?php
namespace FML;
use FML\Types\Container;
use FML\Types\Renderable;
use FML\Script\Script;
/**
2014-01-12 00:51:46 +01:00
* Class representing a ManiaLink
2014-01-03 17:18:14 +01:00
*
* @author steeffeen
*/
class ManiaLink implements Container {
/**
* Protected Properties
*/
protected $encoding = 'utf-8';
protected $tagName = 'manialink';
protected $id = '';
protected $version = 1;
protected $background = '';
protected $navigable3d = 0;
protected $timeout = 0;
protected $children = array();
protected $script = null;
/**
2014-01-12 00:51:46 +01:00
* Create a new ManiaLink
2014-01-03 17:18:14 +01:00
*
2014-01-05 16:04:55 +01:00
* @param string $id Manialink Id
2014-01-03 17:18:14 +01:00
*/
public function __construct($id = null) {
2014-01-12 00:51:46 +01:00
$this->setId($id);
2014-01-03 17:18:14 +01:00
}
/**
* Set XML Encoding
*
2014-01-05 16:04:55 +01:00
* @param string $encoding XML Encoding
2014-01-03 17:18:14 +01:00
* @return \FML\ManiaLink
*/
public function setXmlEncoding($encoding) {
2014-01-12 00:51:46 +01:00
$this->encoding = (string) $encoding;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Manialink Id
*
2014-01-05 16:04:55 +01:00
* @param string $id Manialink Id
2014-01-03 17:18:14 +01:00
* @return \FML\ManiaLink
*/
public function setId($id) {
2014-01-12 00:51:46 +01:00
$this->id = (string) $id;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Background
*
2014-01-05 16:04:55 +01:00
* @param string $background Background Value
2014-01-03 17:18:14 +01:00
* @return \FML\ManiaLink
*/
public function setBackground($background) {
2014-01-12 00:51:46 +01:00
$this->background = (string) $background;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Navigable3d
*
2014-01-12 00:51:46 +01:00
* @param bool $navigable3d Whether the manialink should be 3d navigable
2014-01-03 17:18:14 +01:00
* @return \FML\ManiaLink
*/
public function setNavigable3d($navigable3d) {
$this->navigable3d = ($navigable3d ? 1 : 0);
return $this;
}
/**
* Set Timeout
*
2014-01-05 16:04:55 +01:00
* @param int $timeout Timeout Duration
2014-01-03 17:18:14 +01:00
* @return \FML\ManiaLink
*/
public function setTimeout($timeout) {
2014-01-12 00:51:46 +01:00
$this->timeout = (int) $timeout;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
*
* @see \FML\Types\Container::add()
* @return \FML\ManiaLink
*/
public function add(Renderable $child) {
2014-01-12 14:47:17 +01:00
if (!in_array($child, $this->children, true)) {
array_push($this->children, $child);
}
2014-01-03 17:18:14 +01:00
return $this;
}
/**
*
* @see \FML\Types\Container::removeChildren()
* @return \FML\ManiaLink
*/
public function removeChildren() {
$this->children = array();
return $this;
}
/**
2014-01-05 16:04:55 +01:00
* Set the Script of the ManiaLink
2014-01-03 17:18:14 +01:00
*
2014-01-12 00:51:46 +01:00
* @param Script $script The Script for the ManiaLink
2014-01-03 17:18:14 +01:00
* @return \FML\ManiaLink
*/
public function setScript(Script $script) {
$this->script = $script;
return $this;
}
2014-01-05 16:04:55 +01:00
/**
* Get the current Script of the ManiaLink
*
2014-01-12 00:51:46 +01:00
* @param string $createIfEmpty (optional) Whether the Script Object should be created if it's not set yet
2014-01-05 16:04:55 +01:00
* @return \FML\Script\Script
*/
public function getScript($createIfEmpty = true) {
if (!$this->script && $createIfEmpty) {
$this->script = new Script();
}
return $this->script;
}
2014-01-03 17:18:14 +01:00
/**
* Render the XML Document
*
2014-01-12 00:51:46 +01:00
* @param bool (optional) $echo If the XML Text should be echoed and the Content-Type Header should be set
* @param \DOMDocument $domDocument (optional) DOMDocument for which the XML Element should be created
2014-01-03 17:18:14 +01:00
* @return \DOMDocument
*/
public function render($echo = false, $domDocument = null) {
2014-01-12 00:51:46 +01:00
$isChild = (bool) $domDocument;
2014-01-03 17:18:14 +01:00
if (!$isChild) {
$domDocument = new \DOMDocument('1.0', $this->encoding);
}
2014-01-12 00:51:46 +01:00
$maniaLink = $domDocument->createElement($this->tagName);
2014-01-03 17:18:14 +01:00
if (!$isChild) {
2014-01-12 00:51:46 +01:00
$domDocument->appendChild($maniaLink);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
if ($this->id !== null) {
$maniaLink->setAttribute('id', $this->id);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
if ($this->version !== null) {
$maniaLink->setAttribute('version', $this->version);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
if ($this->background !== null) {
$maniaLink->setAttribute('background', $this->background);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
if ($this->navigable3d !== null) {
$maniaLink->setAttribute('navigable3d', $this->navigable3d);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
if ($this->timeout !== null) {
2014-01-03 17:18:14 +01:00
$timeoutXml = $domDocument->createElement('timeout', $this->timeout);
2014-01-12 00:51:46 +01:00
$maniaLink->appendChild($timeoutXml);
2014-01-03 17:18:14 +01:00
}
foreach ($this->children as $child) {
$childXml = $child->render($domDocument);
2014-01-12 00:51:46 +01:00
$maniaLink->appendChild($childXml);
2014-01-03 17:18:14 +01:00
}
if ($this->script) {
$scriptXml = $this->script->render($domDocument);
2014-01-12 00:51:46 +01:00
$maniaLink->appendChild($scriptXml);
2014-01-03 17:18:14 +01:00
}
if ($isChild) {
2014-01-12 00:51:46 +01:00
return $maniaLink;
2014-01-03 17:18:14 +01:00
}
if ($echo) {
header('Content-Type: application/xml');
echo $domDocument->saveXML();
}
return $domDocument;
}
/**
* Get String Representation
*
* @return string
*/
public function __toString() {
$domDocument = $this->render();
$xmlText = $domDocument->saveXML();
return $xmlText;
}
}