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-04-27 14:44:40 +02:00
|
|
|
use FML\Types\ScriptFeatureable;
|
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
|
|
|
*
|
2014-05-14 23:24:00 +02:00
|
|
|
* @author steeffeen
|
2014-04-13 18:21:40 +02:00
|
|
|
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
|
2014-05-14 23:24:00 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
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
|
|
|
|
*/
|
2014-05-14 23:24:00 +02:00
|
|
|
const BACKGROUND_0 = '0';
|
|
|
|
const BACKGROUND_1 = '1';
|
|
|
|
const BACKGROUND_STARS = 'stars';
|
2014-01-19 19:30:21 +01:00
|
|
|
const BACKGROUND_STATIONS = 'stations';
|
2014-05-14 23:24:00 +02:00
|
|
|
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 = '';
|
2014-04-27 14:44:40 +02:00
|
|
|
protected $navigable3d = 1;
|
2014-01-03 17:18:14 +01:00
|
|
|
protected $timeout = 0;
|
|
|
|
protected $children = array();
|
2014-05-14 23:24:00 +02:00
|
|
|
/** @var Dico $dico */
|
2014-01-19 19:30:21 +01:00
|
|
|
protected $dico = null;
|
2014-05-14 23:24:00 +02:00
|
|
|
/** @var Stylesheet $stylesheet */
|
2014-01-19 19:30:21 +01:00
|
|
|
protected $stylesheet = null;
|
2014-05-14 23:24:00 +02:00
|
|
|
/** @var Script $script */
|
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-02-16 13:59:28 +01:00
|
|
|
* @param string $id (optional) ManiaLink Id
|
2014-01-19 19:30:21 +01:00
|
|
|
* @return \FML\ManiaLink
|
|
|
|
*/
|
|
|
|
public static function create($id = null) {
|
|
|
|
$maniaLink = new ManiaLink($id);
|
|
|
|
return $maniaLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new ManiaLink Object
|
|
|
|
*
|
2014-02-16 13:59:28 +01:00
|
|
|
* @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-05-14 23:24:00 +02:00
|
|
|
$this->encoding = (string)$encoding;
|
2014-01-03 17:18:14 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-16 13:59:28 +01:00
|
|
|
* Set ManiaLink Id
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-02-16 13:59:28 +01:00
|
|
|
* @param string $id ManiaLink Id
|
2014-01-03 17:18:14 +01:00
|
|
|
* @return \FML\ManiaLink
|
|
|
|
*/
|
|
|
|
public function setId($id) {
|
2014-05-14 23:24:00 +02:00
|
|
|
$this->id = (string)$id;
|
2014-01-03 17:18:14 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-02-16 13:59:28 +01:00
|
|
|
/**
|
|
|
|
* Get ManiaLink Id
|
2014-04-27 14:44:40 +02:00
|
|
|
*
|
2014-02-16 13:59:28 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:18:14 +01:00
|
|
|
/**
|
|
|
|
* 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-05-14 23:24:00 +02: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-05-14 23:24:00 +02: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
|
|
|
*
|
2014-04-27 14:44:40 +02:00
|
|
|
* @param Renderable $child Child Element to add
|
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) {
|
2014-04-13 18:21:40 +02:00
|
|
|
$this->setDico(new Dico());
|
2014-01-19 19:30:21 +01:00
|
|
|
}
|
|
|
|
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) {
|
2014-04-13 18:21:40 +02:00
|
|
|
$this->setStylesheet(new Stylesheet());
|
2014-01-19 19:30:21 +01:00
|
|
|
}
|
|
|
|
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) {
|
2014-04-13 18:21:40 +02:00
|
|
|
$this->setScript(new Script());
|
2014-01-05 16:04:55 +01:00
|
|
|
}
|
|
|
|
return $this->script;
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:18:14 +01:00
|
|
|
/**
|
|
|
|
* Render the XML Document
|
|
|
|
*
|
2014-05-14 23:24:00 +02: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-05-14 23:24:00 +02:00
|
|
|
$isChild = (bool)$domDocument;
|
2014-01-03 17:18:14 +01:00
|
|
|
if (!$isChild) {
|
2014-05-14 23:24:00 +02:00
|
|
|
$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-04-27 14:44:40 +02:00
|
|
|
if (!$this->navigable3d) {
|
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
|
|
|
}
|
2014-01-19 19:30:21 +01:00
|
|
|
if ($this->dico) {
|
|
|
|
$dicoXml = $this->dico->render($domDocument);
|
|
|
|
$maniaLink->appendChild($dicoXml);
|
|
|
|
}
|
2014-04-27 14:44:40 +02:00
|
|
|
$scriptFeatures = array();
|
|
|
|
foreach ($this->children as $child) {
|
2014-05-14 23:24:00 +02:00
|
|
|
/** @var Renderable $child */
|
2014-04-27 14:44:40 +02:00
|
|
|
$childXml = $child->render($domDocument, $this->getScript());
|
|
|
|
$maniaLink->appendChild($childXml);
|
|
|
|
if ($child instanceof ScriptFeatureable) {
|
|
|
|
$scriptFeatures = array_merge($scriptFeatures, $child->getScriptFeatures());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($scriptFeatures) {
|
|
|
|
$this->getScript()->loadFeatures($scriptFeatures);
|
|
|
|
}
|
2014-01-19 19:30:21 +01:00
|
|
|
if ($this->stylesheet) {
|
|
|
|
$stylesheetXml = $this->stylesheet->render($domDocument);
|
|
|
|
$maniaLink->appendChild($stylesheetXml);
|
|
|
|
}
|
2014-04-27 16:31:46 +02:00
|
|
|
if ($this->script) {
|
|
|
|
if ($this->script->needsRendering()) {
|
|
|
|
$scriptXml = $this->script->render($domDocument);
|
|
|
|
$maniaLink->appendChild($scriptXml);
|
|
|
|
}
|
|
|
|
$this->script->resetGenericScriptLabels();
|
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();
|
2014-05-14 23:24:00 +02:00
|
|
|
$xmlText = $domDocument->saveXML();
|
2014-01-03 17:18:14 +01:00
|
|
|
return $xmlText;
|
|
|
|
}
|
2013-11-25 00:02:07 +01:00
|
|
|
}
|