TrackManiaControl/application/core/FML/ManiaLink.php

281 lines
6.2 KiB
PHP
Raw Normal View History

2014-01-03 17:18:14 +01:00
<?php
namespace FML;
2014-01-19 19:30:21 +01:00
use FML\Elements\Dico;
2014-01-21 20:30:40 +01:00
use FML\Script\Script;
2014-01-19 19:30:21 +01:00
use FML\Stylesheet\Stylesheet;
2014-01-21 20:30:40 +01:00
use FML\Types\Renderable;
2014-01-03 17:18:14 +01:00
/**
2014-01-12 00:51:46 +01:00
* Class representing a ManiaLink
2014-01-03 17:18:14 +01:00
*
* @author steeffeen
*/
2014-01-19 19:30:21 +01:00
class ManiaLink {
2014-01-21 20:30:40 +01:00
/*
2014-01-19 19:30:21 +01:00
* Constants
*/
const BACKGROUND_0 = '0';
const BACKGROUND_STARS = 'stars';
const BACKGROUND_STATIONS = 'stations';
const BACKGROUND_TITLE = 'title';
2014-01-21 20:30:40 +01:00
/*
2014-01-03 17:18:14 +01:00
* 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();
2014-01-19 19:30:21 +01:00
protected $dico = null;
protected $stylesheet = null;
2014-01-03 17:18:14 +01:00
protected $script = null;
/**
2014-01-19 19:30:21 +01:00
* Create a new ManiaLink Object
2014-01-03 17:18:14 +01:00
*
2014-01-19 19:30:21 +01:00
* @param string $id (optional) Manialink Id
* @return \FML\ManiaLink
*/
public static function create($id = null) {
$maniaLink = new ManiaLink($id);
return $maniaLink;
}
/**
* Construct a new ManiaLink Object
*
* @param string $id (optional) Manialink Id
2014-01-03 17:18:14 +01:00
*/
public function __construct($id = null) {
2014-01-19 19:30:21 +01:00
if ($id !== null) {
$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;
}
/**
2014-01-19 19:30:21 +01:00
* Add an Element to the ManiaLink
2014-01-03 17:18:14 +01:00
*
* @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;
}
/**
2014-01-19 19:30:21 +01:00
* Remove all Elements from the ManiaLinks
2014-01-03 17:18:14 +01:00
*
* @return \FML\ManiaLink
*/
public function removeChildren() {
$this->children = array();
return $this;
}
2014-01-19 19:30:21 +01:00
/**
* Set the Dictionary of the ManiaLink
*
* @param Dico $dico The Dictionary to use
* @return \FML\ManiaLink
*/
public function setDico(Dico $dico) {
$this->dico = $dico;
return $this;
}
/**
* Get the current Dictionary of the ManiaLink
*
* @param bool $createIfEmpty (optional) Whether the Dico Object should be created if it's not set yet
* @return \FML\Elements\Dico
*/
public function getDico($createIfEmpty = true) {
if (!$this->dico && $createIfEmpty) {
$this->dico = new Dico();
}
return $this->dico;
}
/**
* Set the Stylesheet of the ManiaLink
*
* @param Stylesheet $stylesheet Stylesheet Object
* @return \FML\ManiaLink
*/
public function setStylesheet(Stylesheet $stylesheet) {
$this->stylesheet = $stylesheet;
return $this;
}
/**
* Get the Stylesheet of the ManiaLink
*
* @param bool $createIfEmpty (optional) Whether the Script Object should be created if it's not set yet
* @return \FML\Stylesheet\Stylesheet
*/
public function getStylesheet($createIfEmpty = true) {
if (!$this->stylesheet && $createIfEmpty) {
$this->stylesheet = new Stylesheet();
}
return $this->stylesheet;
}
2014-01-03 17:18:14 +01:00
/**
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-19 19:30:21 +01:00
* @param bool $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-19 19:30:21 +01:00
$domDocument->xmlStandalone = true;
2014-01-03 17:18:14 +01:00
}
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-21 20:30:40 +01:00
if (strlen($this->id) > 0) {
2014-01-12 00:51:46 +01:00
$maniaLink->setAttribute('id', $this->id);
2014-01-03 17:18:14 +01:00
}
2014-01-19 19:30:21 +01:00
if ($this->version) {
2014-01-12 00:51:46 +01:00
$maniaLink->setAttribute('version', $this->version);
2014-01-03 17:18:14 +01:00
}
2014-01-21 20:30:40 +01:00
if (strlen($this->background) > 0) {
2014-01-12 00:51:46 +01:00
$maniaLink->setAttribute('background', $this->background);
2014-01-03 17:18:14 +01:00
}
2014-01-21 20:30:40 +01:00
if ($this->navigable3d) {
// TODO: check default value
2014-01-12 00:51:46 +01:00
$maniaLink->setAttribute('navigable3d', $this->navigable3d);
2014-01-03 17:18:14 +01:00
}
2014-01-19 19:30:21 +01:00
if ($this->timeout) {
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
}
2014-01-19 19:30:21 +01:00
if ($this->dico) {
$dicoXml = $this->dico->render($domDocument);
$maniaLink->appendChild($dicoXml);
}
if ($this->stylesheet) {
$stylesheetXml = $this->stylesheet->render($domDocument);
$maniaLink->appendChild($stylesheetXml);
}
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) {
2014-01-19 19:30:21 +01:00
header('Content-Type: application/xml; charset=utf-8;');
2014-01-03 17:18:14 +01:00
echo $domDocument->saveXML();
}
return $domDocument;
}
/**
* Get String Representation
*
* @return string
*/
public function __toString() {
$domDocument = $this->render();
$xmlText = $domDocument->saveXML();
return $xmlText;
}
}