Updated to ManiaLink v3

This commit is contained in:
Jocy Wolff
2017-03-25 18:40:15 +01:00
parent 1010c1db6b
commit 120a0e2169
133 changed files with 16194 additions and 8949 deletions

View File

@ -23,297 +23,358 @@ use FML\ManiaCode\ViewReplay;
* Class representing a ManiaCode
*
* @author steeffeen <mail@steeffeen.com>
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ManiaCode {
/*
* Protected properties
*/
protected $encoding = 'utf-8';
protected $tagName = 'maniacode';
protected $noConfirmation = null;
/** @var Element[] $elements */
protected $elements = array();
class ManiaCode
{
/**
* Create a new ManiaCode object
*
* @return static
*/
public static function create() {
return new static();
}
/**
* @var bool $disableConfirmation Disable the confirmation
*/
protected $disableConfirmation = null;
/**
* Set XML encoding
*
* @param string $encoding XML encoding
* @return static
*/
public function setXmlEncoding($encoding) {
$this->encoding = (string)$encoding;
return $this;
}
/**
* @var Element[] $elements ManiaCode Elements
*/
protected $elements = array();
/**
* Disable the showing of the confirmation at the end of the ManiaCode
*
* @param bool $disable Whether the confirmation should be shown
* @return static
*/
public function disableConfirmation($disable) {
$this->noConfirmation = ($disable ? 1 : 0);
return $this;
}
/**
* Create a new ManiaCode
*
* @api
* @return static
*/
public static function create()
{
return new static();
}
/**
* Show a message
*
* @param string $message Message text
* @return static
*/
public function addShowMessage($message) {
$messageElement = new ShowMessage($message);
$this->addElement($messageElement);
return $this;
}
/**
* Get if the confirmation is disabled
*
* @api
* @return bool
*/
public function getDisableConfirmation()
{
return $this->disableConfirmation;
}
/**
* Install a Macroblock
*
* @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);
$this->addElement($macroblockElement);
return $this;
}
/**
* 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;
}
/**
* Install a map
*
* @param string $name Map name
* @param string $url Map url
* @return static
*/
public function addInstallMap($name, $url) {
$mapElement = new InstallMap($name, $url);
$this->addElement($mapElement);
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);
}
/**
* Play a map
*
* @param string $name Map name
* @param string $url Map url
* @return static
*/
public function addPlayMap($name, $url) {
$mapElement = new PlayMap($name, $url);
$this->addElement($mapElement);
return $this;
}
/**
* 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 replay
*
* @param string $name Replay name
* @param string $url Replay url
* @return static
*/
public function addInstallReplay($name, $url) {
$replayElement = new InstallReplay($name, $url);
$this->addElement($replayElement);
return $this;
}
/**
* 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);
}
/**
* View a replay
*
* @param string $name Replay name
* @param string $url Replay url
* @return static
*/
public function addViewReplay($name, $url) {
$replayElement = new ViewReplay($name, $url);
$this->addElement($replayElement);
return $this;
}
/**
* 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);
}
/**
* Play a replay
*
* @param string $name Replay name
* @param string $url Replay url
* @return static
*/
public function addPlayReplay($name, $url) {
$replayElement = new PlayReplay($name, $url);
$this->addElement($replayElement);
return $this;
}
/**
* 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);
}
/**
* Install a skin
*
* @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);
$this->addElement($skinElement);
return $this;
}
/**
* 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);
}
/**
* Get a skin
*
* @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);
$this->addElement($skinElement);
return $this;
}
/**
* 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);
}
/**
* Add a buddy
*
* @param string $login Buddy login
* @return static
*/
public function addAddBuddy($login) {
$buddyElement = new AddBuddy($login);
$this->addElement($buddyElement);
return $this;
}
/**
* 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);
}
/**
* Go to a link
*
* @param string $link Goto link
* @return static
*/
public function addGoto($link) {
$gotoElement = new Go_To($link);
$this->addElement($gotoElement);
return $this;
}
/**
* 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);
}
/**
* Join a server
*
* @param string $login Server login
* @return static
*/
public function addJoinServer($login) {
$serverElement = new JoinServer($login);
$this->addElement($serverElement);
return $this;
}
/**
* Add a buddy
*
* @api
* @param string $login Buddy login
* @return static
*/
public function addAddBuddy($login)
{
$buddyElement = new AddBuddy($login);
return $this->addElement($buddyElement);
}
/**
* Add a server as favorite
*
* @param string $login Server login
* @return static
*/
public function addAddFavorite($login) {
$favoriteElement = new AddFavorite($login);
$this->addElement($favoriteElement);
return $this;
}
/**
* 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);
}
/**
* Install a script
*
* @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);
$this->addElement($scriptElement);
return $this;
}
/**
* Join a server
*
* @api
* @param string $login Server login
* @return static
*/
public function addJoinServer($login)
{
$serverElement = new JoinServer($login);
return $this->addElement($serverElement);
}
/**
* Install a title pack
*
* @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);
$this->addElement($packElement);
return $this;
}
/**
* Add a server as favorite
*
* @api
* @param string $login Server login
* @return static
*/
public function addAddFavorite($login)
{
$favoriteElement = new AddFavorite($login);
return $this->addElement($favoriteElement);
}
/**
* Add a ManiaCode element
*
* @param Element $element Element to add
* @return static
*/
public function addElement(Element $element) {
array_push($this->elements, $element);
return $this;
}
/**
* 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);
}
/**
* Remove all elements from the ManiaCode
*
* @return static
*/
public function removeElements() {
$this->elements = array();
return $this;
}
/**
* 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);
}
/**
* Render the XML document
*
* @param bool $echo (optional) Whether 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', $this->encoding);
$domDocument->xmlStandalone = true;
$maniaCode = $domDocument->createElement($this->tagName);
$domDocument->appendChild($maniaCode);
if ($this->noConfirmation) {
$maniaCode->setAttribute('noconfirmation', $this->noConfirmation);
}
foreach ($this->elements as $element) {
$xmlElement = $element->render($domDocument);
$maniaCode->appendChild($xmlElement);
}
if ($echo) {
header('Content-Type: application/xml; charset=utf-8;');
echo $domDocument->saveXML();
}
return $domDocument;
}
/**
* 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;
}
/**
* Remove all ManiaCode Elements
*
* @api
* @return static
* @deprecated use removeAllElements() instead
*/
public function removeElements()
{
return $this->removeAllElements();
}
/**
* Remove all ManiaCode Elements
*
* @api
* @return static
*/
public function removeAllElements()
{
$this->elements = array();
return $this;
}
/**
* 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();
}
/**
* Get string representation
*
* @return string
*/
public function __toString() {
return $this->render()->saveXML();
}
}