TrackManiaControl/libs/FML/ManiaLinks.php
2017-03-25 18:40:15 +01:00

196 lines
4.1 KiB
PHP

<?php
namespace FML;
/**
* Class holding several ManiaLinks at once
*
* @author steeffeen <mail@steeffeen.com>
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ManiaLinks
{
/**
* @var ManiaLink[] $children ManiaLinks children
*/
protected $children = array();
/**
* @var CustomUI $customUI Custom UI
*/
protected $customUI = null;
/**
* Create a new ManiaLinks object
*
* @api
* @param ManiaLink[] $children ManiaLink children
* @return static
*/
public static function create(array $children = null)
{
return new static($children);
}
/**
* Construct a new ManiaLinks object
*
* @api
* @param ManiaLink[] $children ManiaLink children
*/
public function __construct(array $children = null)
{
if ($children) {
$this->setChildren($children);
}
}
/**
* Get all child ManiaLinks
*
* @api
* @return ManiaLink[]
*/
public function getChildren()
{
return $this->children;
}
/**
* Add a child ManiaLink
*
* @api
* @param ManiaLink $child Child ManiaLink
* @return static
* @deprecated use addChild() instead
*/
public function add(ManiaLink $child)
{
return $this->addChild($child);
}
/**
* Add a child ManiaLink
*
* @api
* @param ManiaLink $child Child ManiaLink
* @return static
*/
public function addChild(ManiaLink $child)
{
if (!in_array($child, $this->children, true)) {
array_push($this->children, $child);
}
return $this;
}
/**
* Set ManiaLink children
*
* @api
* @param ManiaLink[] $children ManiaLink children
* @return static
*/
public function setChildren(array $children)
{
$this->children = array();
foreach ($children as $child) {
$this->addChild($child);
}
return $this;
}
/**
* Remove all child ManiaLinks
*
* @api
* @return static
* @deprecated use removeAllChildren instead
*/
public function removeChildren()
{
return $this->removeAllChildren();
}
/**
* Remove all child ManiaLinks
*
* @api
* @return static
*/
public function removeAllChildren()
{
$this->children = array();
return $this;
}
/**
* Get the CustomUI
*
* @api
* @return CustomUI
*/
public function getCustomUI()
{
return $this->customUI;
}
/**
* Set the CustomUI
*
* @api
* @param CustomUI $customUI CustomUI object
* @return static
*/
public function setCustomUI(CustomUI $customUI = null)
{
$this->customUI = $customUI;
return $this;
}
/**
* Render the ManiaLinks object
*
* @param bool (optional) $echo If the XML text should be echoed and the Content-Type header should be set
* @return \DOMDocument
*/
public function render($echo = false)
{
$domDocument = new \DOMDocument("1.0", "utf-8");
$domDocument->xmlStandalone = true;
$maniaLinks = $domDocument->createElement("manialinks");
$domDocument->appendChild($maniaLinks);
foreach ($this->children as $child) {
$childXml = $child->render(false, $domDocument);
$maniaLinks->appendChild($childXml);
}
if ($this->customUI) {
$customUIElement = $this->customUI->render($domDocument);
$maniaLinks->appendChild($customUIElement);
}
if ($echo) {
header("Content-Type: application/xml; charset=utf-8;");
echo $domDocument->saveXML();
}
return $domDocument;
}
/**
* Get string representation
*
* @return string
*/
public function __toString()
{
return $this->render()
->saveXML();
}
}