TrackManiaControl/libs/FML/ManiaCode.php

399 lines
9.0 KiB
PHP
Raw Permalink Normal View History

2014-01-12 00:51:46 +01:00
<?php
namespace FML;
2014-01-19 19:30:21 +01:00
use FML\ManiaCode\AddBuddy;
use FML\ManiaCode\AddFavorite;
2014-01-12 00:51:46 +01:00
use FML\ManiaCode\Element;
2014-01-19 19:30:21 +01:00
use FML\ManiaCode\GetSkin;
use FML\ManiaCode\Go_To;
2014-05-14 23:24:00 +02:00
use FML\ManiaCode\InstallMacroblock;
2014-01-12 00:51:46 +01:00
use FML\ManiaCode\InstallMap;
2014-01-19 19:30:21 +01:00
use FML\ManiaCode\InstallPack;
2014-01-12 00:51:46 +01:00
use FML\ManiaCode\InstallReplay;
2014-01-19 19:30:21 +01:00
use FML\ManiaCode\InstallScript;
2014-01-12 00:51:46 +01:00
use FML\ManiaCode\InstallSkin;
use FML\ManiaCode\JoinServer;
2014-01-19 19:30:21 +01:00
use FML\ManiaCode\PlayMap;
use FML\ManiaCode\PlayReplay;
use FML\ManiaCode\ShowMessage;
use FML\ManiaCode\ViewReplay;
2014-01-12 00:51:46 +01:00
/**
* Class representing a ManiaCode
*
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
2014-01-12 00:51:46 +01:00
*/
2017-03-25 18:40:15 +01:00
class ManiaCode
{
/**
* @var bool $disableConfirmation Disable the confirmation
*/
protected $disableConfirmation = null;
/**
* @var Element[] $elements ManiaCode Elements
*/
protected $elements = array();
/**
* Create a new ManiaCode
*
* @api
* @return static
*/
public static function create()
{
return new static();
}
/**
* Get if the confirmation is disabled
*
* @api
* @return bool
*/
public function getDisableConfirmation()
{
return $this->disableConfirmation;
}
/**
* Disable the showing of the confirmation at the end of the ManiaCode
*
* @api
* @param bool $disableConfirmation If the confirmation should be disabled
* @return static
*/
public function setDisableConfirmation($disableConfirmation)
{
$this->disableConfirmation = (bool)$disableConfirmation;
return $this;
}
/**
* Show a message
*
* @api
* @param string $message Message text
* @return static
*/
public function addShowMessage($message)
{
$messageElement = new ShowMessage($message);
return $this->addElement($messageElement);
}
/**
* Install a Macroblock
*
* @api
* @param string $name Macroblock name
* @param string $file Macroblock file
* @param string $url Macroblock url
* @return static
*/
public function addInstallMacroblock($name, $file, $url)
{
$macroblockElement = new InstallMacroblock($name, $file, $url);
return $this->addElement($macroblockElement);
}
/**
* Install a map
*
* @api
* @param string $name Map name
* @param string $url Map url
* @return static
*/
public function addInstallMap($name, $url)
{
$mapElement = new InstallMap($name, $url);
return $this->addElement($mapElement);
}
/**
* Play a map
*
* @api
* @param string $name Map name
* @param string $url Map url
* @return static
*/
public function addPlayMap($name, $url)
{
$mapElement = new PlayMap($name, $url);
return $this->addElement($mapElement);
}
/**
* Install a replay
*
* @api
* @param string $name Replay name
* @param string $url Replay url
* @return static
*/
public function addInstallReplay($name, $url)
{
$replayElement = new InstallReplay($name, $url);
return $this->addElement($replayElement);
}
/**
* View a replay
*
* @api
* @param string $name Replay name
* @param string $url Replay url
* @return static
*/
public function addViewReplay($name, $url)
{
$replayElement = new ViewReplay($name, $url);
return $this->addElement($replayElement);
}
/**
* Play a replay
*
* @api
* @param string $name Replay name
* @param string $url Replay url
* @return static
*/
public function addPlayReplay($name, $url)
{
$replayElement = new PlayReplay($name, $url);
return $this->addElement($replayElement);
}
/**
* Install a skin
*
* @api
* @param string $name Skin name
* @param string $file Skin file
* @param string $url Skin url
* @return static
*/
public function addInstallSkin($name, $file, $url)
{
$skinElement = new InstallSkin($name, $file, $url);
return $this->addElement($skinElement);
}
/**
* Get a skin
*
* @api
* @param string $name Skin name
* @param string $file Skin file
* @param string $url Skin url
* @return static
*/
public function addGetSkin($name, $file, $url)
{
$skinElement = new GetSkin($name, $file, $url);
return $this->addElement($skinElement);
}
/**
* Add a buddy
*
* @api
* @param string $login Buddy login
* @return static
*/
public function addAddBuddy($login)
{
$buddyElement = new AddBuddy($login);
return $this->addElement($buddyElement);
}
/**
* Go to a link
*
* @api
* @param string $link Goto link
* @return static
*/
public function addGoto($link)
{
$gotoElement = new Go_To($link);
return $this->addElement($gotoElement);
}
/**
* Join a server
*
* @api
2017-04-02 16:32:57 +02:00
* @param string $loginOrIp (optional) Server login or ip
* @param int $port (optional) Server port
2017-03-25 18:40:15 +01:00
* @return static
*/
2017-04-02 16:32:57 +02:00
public function addJoinServer($loginOrIp = null, $port = null)
2017-03-25 18:40:15 +01:00
{
2017-04-02 16:32:57 +02:00
$serverElement = new JoinServer($loginOrIp, $port);
2017-03-25 18:40:15 +01:00
return $this->addElement($serverElement);
}
/**
* Add a server as favorite
*
* @api
2017-04-02 16:32:57 +02:00
* @param string $loginOrIp (optional) Server login or ip
* @param int $port (optional) Server port
2017-03-25 18:40:15 +01:00
* @return static
*/
2017-04-02 16:32:57 +02:00
public function addAddFavorite($loginOrIp = null, $port = null)
2017-03-25 18:40:15 +01:00
{
2017-04-02 16:32:57 +02:00
$favoriteElement = new AddFavorite($loginOrIp, $port);
2017-03-25 18:40:15 +01:00
return $this->addElement($favoriteElement);
}
/**
* Install a script
*
* @api
* @param string $name Script name
* @param string $file Script file
* @param string $url Script url
* @return static
*/
public function addInstallScript($name, $file, $url)
{
$scriptElement = new InstallScript($name, $file, $url);
return $this->addElement($scriptElement);
}
/**
* Install a title pack
*
* @api
* @param string $name Pack name
* @param string $file Pack file
* @param string $url Pack url
* @return static
*/
public function addInstallPack($name, $file, $url)
{
$packElement = new InstallPack($name, $file, $url);
return $this->addElement($packElement);
}
/**
* Get all Elements
*
* @api
* @return Element[]
*/
public function getElements()
{
return $this->elements;
}
/**
* Add a ManiaCode Element
*
* @api
* @param Element $element Element to add
* @return static
*/
public function addElement(Element $element)
{
if (!in_array($element, $this->elements, true)) {
array_push($this->elements, $element);
}
return $this;
}
/**
2017-04-02 16:32:57 +02:00
* Add ManiaCode Elements
2017-03-25 18:40:15 +01:00
*
* @api
2017-04-02 16:32:57 +02:00
* @param Element[] $elements Elements to add
2017-03-25 18:40:15 +01:00
* @return static
*/
2017-04-02 16:32:57 +02:00
public function addElements(array $elements)
2017-03-25 18:40:15 +01:00
{
2017-04-02 16:32:57 +02:00
foreach ($elements as $element) {
$this->addElement($element);
}
return $this;
2017-03-25 18:40:15 +01:00
}
/**
* Remove all ManiaCode Elements
*
* @api
* @return static
*/
public function removeAllElements()
{
$this->elements = array();
return $this;
}
2017-04-02 16:32:57 +02:00
/**
* Remove all ManiaCode Elements
*
* @api
* @return static
* @deprecated Use removeAllElements()
* @see ManiaCode::removeAllElements()
*/
public function removeElements()
{
return $this->removeAllElements();
}
2017-03-25 18:40:15 +01:00
/**
* Render the ManiaCode
*
* @api
* @param bool $echo (optional) 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;
$domElement = $domDocument->createElement("maniacode");
$domDocument->appendChild($domElement);
if ($this->disableConfirmation) {
$domElement->setAttribute("noconfirmation", 1);
}
foreach ($this->elements as $element) {
$childDomElement = $element->render($domDocument);
$domElement->appendChild($childDomElement);
}
if ($echo) {
header("Content-Type: application/xml; charset=utf-8;");
echo $domDocument->saveXML();
}
return $domDocument;
}
/**
* Get the string representation
*
* @return string
*/
public function __toString()
{
return $this->render()
->saveXML();
}
2014-01-12 00:51:46 +01:00
}