TrackManiaControl/application/core/Libs/FML/ManiaLinks.php

125 lines
2.7 KiB
PHP
Raw Normal View History

2013-12-03 23:02:31 +01:00
<?php
namespace FML;
/**
2014-01-12 00:51:46 +01:00
* Class holding several ManiaLinks at once
2013-12-03 23:02:31 +01:00
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
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
2013-12-03 23:02:31 +01:00
*/
class ManiaLinks {
2014-01-21 20:30:40 +01:00
/*
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';
2014-06-21 03:18:21 +02:00
/** @var ManiaLink[] $children */
2013-12-03 23:02:31 +01:00
protected $children = array();
2014-05-14 23:24:00 +02:00
/** @var CustomUI $customUI */
2014-01-03 17:18:14 +01:00
protected $customUI = null;
2013-12-03 23:02:31 +01:00
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Create a new ManiaLinks object
2014-01-19 19:30:21 +01:00
*
2014-07-03 22:34:47 +02:00
* @return static
2014-01-19 19:30:21 +01:00
*/
public static function create() {
2014-06-21 03:18:21 +02:00
return new static();
2014-01-19 19:30:21 +01:00
}
2013-12-03 23:02:31 +01:00
/**
2014-06-21 03:18:21 +02:00
* Set XML encoding
2013-12-03 23:02:31 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $encoding XML encoding
2014-07-03 22:34:47 +02:00
* @return static
2013-12-03 23:02:31 +01:00
*/
public function setXmlEncoding($encoding) {
2014-05-14 23:24:00 +02:00
$this->encoding = (string)$encoding;
2013-12-03 23:02:31 +01:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Add a child ManiaLink
2013-12-03 23:02:31 +01:00
*
2014-02-16 13:59:28 +01:00
* @param ManiaLink $child Child ManiaLink
2014-07-03 22:34:47 +02:00
* @return static
2013-12-03 23:02:31 +01:00
*/
public function add(ManiaLink $child) {
2014-01-12 14:47:17 +01:00
if (!in_array($child, $this->children, true)) {
2013-12-31 02:55:19 +01:00
array_push($this->children, $child);
}
2013-12-03 23:02:31 +01:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Remove all child ManiaLinks
2013-12-03 23:02:31 +01:00
*
2014-07-03 22:34:47 +02:00
* @return static
2013-12-03 23:02:31 +01:00
*/
public function removeChildren() {
$this->children = array();
return $this;
}
2014-01-03 17:18:14 +01:00
/**
* Set the CustomUI
*
2014-06-21 03:18:21 +02:00
* @param CustomUI $customUI CustomUI object
2014-07-03 22:34:47 +02:00
* @return static
2014-01-03 17:18:14 +01:00
*/
public function setCustomUI(CustomUI $customUI) {
$this->customUI = $customUI;
return $this;
}
2014-01-19 19:30:21 +01:00
/**
2014-06-21 03:18:21 +02:00
* Get the CustomUI
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @param bool $createIfEmpty (optional) Whether the CustomUI object should be created if it's not set
2014-01-19 19:30:21 +01:00
* @return \FML\CustomUI
*/
public function getCustomUI($createIfEmpty = true) {
if (!$this->customUI && $createIfEmpty) {
2014-04-13 18:21:40 +02:00
$this->setCustomUI(new CustomUI());
2014-01-19 19:30:21 +01:00
}
return $this->customUI;
}
2013-12-03 23:02:31 +01:00
/**
2014-06-21 03:18:21 +02:00
* Render the XML document
2013-12-03 23:02:31 +01:00
*
2014-06-21 03:18:21 +02:00
* @param bool (optional) $echo Whether the XML text 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) {
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-05-14 23:24:00 +02:00
$maniaLinks = $domDocument->createElement($this->tagName);
2014-01-12 00:51:46 +01:00
$domDocument->appendChild($maniaLinks);
2013-12-03 23:02:31 +01:00
foreach ($this->children as $child) {
$childXml = $child->render(false, $domDocument);
2014-01-12 00:51:46 +01:00
$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);
2014-01-12 00:51:46 +01:00
$maniaLinks->appendChild($customUIXml);
2014-01-03 17:18:14 +01:00
}
2013-12-03 23:02:31 +01:00
if ($echo) {
2014-01-19 19:30:21 +01:00
header('Content-Type: application/xml; charset=utf-8;');
2013-12-03 23:02:31 +01:00
echo $domDocument->saveXML();
}
return $domDocument;
}
2014-01-05 19:15:49 +01:00
/**
2014-06-21 03:18:21 +02:00
* Get string representation
2014-01-05 19:15:49 +01:00
*
* @return string
*/
public function __toString() {
2014-06-21 03:18:21 +02:00
return $this->render()->saveXML();
2014-01-05 19:15:49 +01:00
}
2013-12-03 23:02:31 +01:00
}