TrackManiaControl/libs/FML/ManiaCode/JoinServer.php

156 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2014-01-12 00:51:46 +01:00
<?php
namespace FML\ManiaCode;
/**
2014-06-21 03:18:21 +02:00
* ManiaCode Element for joining a server
2014-01-12 00:51:46 +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-18 19:45:50 +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 JoinServer implements Element
{
/**
* @var string $login Server login
*/
protected $login = null;
/**
* @var string $ip Server ip
*/
protected $ip = null;
/**
* @var int $port Server port
*/
protected $port = null;
/**
* Create a new JoinServer Element
*
* @api
* @param string $loginOrIp (optional) Server login or ip
* @param int $port (optional) Server port
* @return static
*/
public static function create($loginOrIp = null, $port = null)
{
return new static($loginOrIp, $port);
}
/**
* Construct a new JoinServer Element
*
* @api
* @param string $loginOrIp (optional) Server login or ip
* @param int $port (optional) Server port
*/
public function __construct($loginOrIp = null, $port = null)
{
if ($port) {
$this->setIp($loginOrIp, $port);
} elseif ($loginOrIp) {
$this->setLogin($loginOrIp);
}
}
/**
* Get the server login
*
* @api
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* Set the server login
*
* @api
* @param string $login Server login
* @return static
*/
public function setLogin($login)
{
$this->login = (string)$login;
$this->ip = null;
$this->port = null;
return $this;
}
/**
* Get the server ip
*
* @api
* @return string
*/
public function getIp()
{
return $this->ip;
}
/**
* Set the server ip and port
*
* @api
* @param string $ip Server ip
* @param int $port (optional) Server port
* @return static
*/
public function setIp($ip, $port = null)
{
$this->login = null;
$this->ip = (string)$ip;
if ($port) {
$this->setPort($port);
}
return $this;
}
/**
* Get the server port
*
* @api
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* Set the server port
*
* @param int $port Server port
* @return static
*/
public function setPort($port)
{
$this->port = (int)$port;
return $this;
}
/**
* @see Element::render()
*/
public function render(\DOMDocument $domDocument)
{
$domElement = $domDocument->createElement("join_server");
if ($this->login) {
$loginElement = $domDocument->createElement("login", $this->login);
$domElement->appendChild($loginElement);
} else {
$ipElement = $domDocument->createElement("ip", $this->ip . ":" . $this->port);
$domElement->appendChild($ipElement);
}
return $domElement;
}
2014-01-12 00:51:46 +01:00
}