TrackManiaControl/libs/FML/ManiaLinks.php

214 lines
4.7 KiB
PHP
Raw Permalink 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>
2017-03-25 18:40:15 +01:00
* @copyright FancyManiaLinks Copyright © 2017 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
*/
2017-03-25 18:40:15 +01:00
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
2017-04-02 16:32:57 +02:00
* @deprecated Use addChild()
* @see ManiaLinks::addChild()
2017-03-25 18:40:15 +01:00
*/
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;
}
/**
2017-04-02 16:32:57 +02:00
* Add child ManiaLinks
2017-03-25 18:40:15 +01:00
*
* @api
2017-04-02 16:32:57 +02:00
* @param ManiaLink[] $children Child ManiaLinks
2017-03-25 18:40:15 +01:00
* @return static
*/
2017-04-02 16:32:57 +02:00
public function addChildren(array $children)
2017-03-25 18:40:15 +01:00
{
foreach ($children as $child) {
$this->addChild($child);
}
return $this;
}
/**
2017-04-02 16:32:57 +02:00
* Set ManiaLink children
2017-03-25 18:40:15 +01:00
*
* @api
2017-04-02 16:32:57 +02:00
* @param ManiaLink[] $children ManiaLink children
2017-03-25 18:40:15 +01:00
* @return static
*/
2017-04-02 16:32:57 +02:00
public function setChildren(array $children)
2017-03-25 18:40:15 +01:00
{
2017-04-02 16:32:57 +02:00
return $this->removeAllChildren()
->addChildren($children);
2017-03-25 18:40:15 +01:00
}
/**
* Remove all child ManiaLinks
*
* @api
* @return static
*/
public function removeAllChildren()
{
$this->children = array();
return $this;
}
2017-04-02 16:32:57 +02:00
/**
* Remove all child ManiaLinks
*
* @api
* @return static
* @deprecated Use removeAllChildren()
* @see ManiaLinks::removeAllChildren()
*/
public function removeChildren()
{
return $this->removeAllChildren();
}
2017-03-25 18:40:15 +01:00
/**
* Get the CustomUI
*
* @api
2017-04-02 16:32:57 +02:00
* @param bool $createIfEmpty (optional) If the Custom UI should be created if it doesn't exist yet
2017-03-25 18:40:15 +01:00
* @return CustomUI
*/
2017-04-02 16:32:57 +02:00
public function getCustomUI($createIfEmpty = true)
2017-03-25 18:40:15 +01:00
{
2017-04-02 16:32:57 +02:00
if (!$this->customUI && $createIfEmpty) {
$this->setCustomUI(new CustomUI());
}
2017-03-25 18:40:15 +01:00
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();
}
2013-12-03 23:02:31 +01:00
}