- split up Tools class in several smaller classes

SRP: Single Responsibility Principle!
This commit is contained in:
Steffen Schröder 2013-11-10 03:07:45 +01:00
parent d2744a5157
commit 7998898692
5 changed files with 111 additions and 102 deletions

View File

@ -6,6 +6,7 @@
<superadmin>
<!-- <login>login1</login> <login>login2</login> -->
<login>steeffeen</login>
<login>kremsy</login>
</superadmin>
<!-- Admins with almost full access -->
@ -27,7 +28,6 @@
<login>xcaliber</login>
<login>eole</login>
<login>fix</login>
<login>kremsy</login>
<login>papychampy</login>
<login>titishu</login>
<login>wurstigewurst</login>

View File

@ -0,0 +1,55 @@
<?php
/**
* Manialink utility class
*
* @author Steff
*/
class ManialinkUtil {
/**
* Build new simple xml element
*
* @param string $name
* @param string $id
* @return SimpleXMLElement
*/
public static function newManialinkXml($id = null) {
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><manialink/>');
$xml->addAttribute('version', '1');
if ($id) {
$xml->addAttribute('id', $id);
}
return $xml;
}
/**
* Add alignment attributes to an xml element
*
* @param SimpleXMLElement $xml
* @param string $halign
* @param string $valign
*/
public static function addAlignment(\SimpleXMLElement $xml, $halign = 'center', $valign = 'center2') {
if (!property_exists($xml, 'halign')) {
$xml->addAttribute('halign', $halign);
}
if (!property_exists($xml, 'valign')) {
$xml->addAttribute('valign', $valign);
}
}
/**
* Add translate attribute to an xml element
*
* @param SimpleXMLElement $xml
* @param bool $translate
*/
public static function addTranslate(\SimpleXMLElement $xml, $translate = true) {
if (!property_exists($xml, 'translate')) {
$xml->addAttribute('translate', ($translate ? 1 : 0));
}
}
}
?>

View File

@ -35,6 +35,7 @@ class Player {
public $rightLevel = 0;
// TODO: usefull construct player without rpc info?
// TODO: check isFakePlayer (probably server itself also "fakeplayer")
// TODO: add all attributes like, allies, clublink ... just make vardump on rpc infos
// TODO: READ ADDITIONAL INFOS FROM DATABASE
/**

View File

@ -0,0 +1,54 @@
<?php
/**
* Class offering methods to format times
*
* @author Steff
*/
class TimeFormatter {
/**
* Formats the given time (milliseconds)
*
* @param int $time
* @return string
*/
public static function formatTime($time) {
if (!is_int($time)) {
$time = (int) $time;
}
$milliseconds = $time % 1000;
$seconds = floor($time / 1000);
$minutes = floor($seconds / 60);
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
$seconds -= $hours * 60 + $minutes * 60;
$format = ($hours > 0 ? $hours . ':' : '');
$format .= ($hours > 0 && $minutes < 10 ? '0' : '') . $minutes . ':';
$format .= ($seconds < 10 ? '0' : '') . $seconds . ':';
$format .= ($milliseconds < 100 ? '0' : '') . ($milliseconds < 10 ? '0' : '') . $milliseconds;
return $format;
}
/**
* Formats the given time (seconds) to hh:mm:ss
*
* @param int $time
* @return string
*/
public static function formatTimeH($time) {
return gmdate("H:i:s", $seconds);
}
/**
* Convert the given time (seconds) to mysql timestamp
*
* @param int $time
* @return string
*/
public static function formatTimestamp($time) {
return date("Y-m-d H:i:s", $time);
}
}
?>

View File

@ -27,52 +27,6 @@ class Tools {
}
}
/**
* Check if the given data describes a player
*
* @param array $player
* @return bool
*/
public static function isPlayer($player) {
if (!$player || !is_array($player)) return false;
if (!array_key_exists('PlayerId', $player) || !is_int($player['PlayerId']) || $player['PlayerId'] <= 0) return false;
return true;
}
/**
* Convert the given time int to mysql timestamp
*
* @param int $time
* @return string
*/
public static function timeToTimestamp($time) {
return date("Y-m-d H:i:s", $time);
}
/**
* Add alignment attributes to an xml element
*
* @param simple_xml_element $xml
* @param string $halign
* @param string $valign
*/
public static function addAlignment($xml, $halign = 'center', $valign = 'center2') {
if (!is_object($xml) || !method_exists($xml, 'addAttribute')) return;
if (!property_exists($xml, 'halign')) $xml->addAttribute('halign', $halign);
if (!property_exists($xml, 'valign')) $xml->addAttribute('valign', $valign);
}
/**
* Add translate attribute to an xml element
*
* @param simple_xml_element $xml
* @param bool $translate
*/
public static function addTranslate($xml, $translate = true) {
if (!is_object($xml) || !method_exists($xml, 'addAttribute')) return;
if (!property_exists($xml, 'translate')) $xml->addAttribute('translate', ($translate ? 1 : 0));
}
/**
* Load a remote file
*
@ -119,37 +73,6 @@ class Tools {
return $result[1];
}
/**
* Formats the given time (milliseconds)
*
* @param int $time
* @return string
*/
public static function formatTime($time) {
if (!is_int($time)) $time = (int) $time;
$milliseconds = $time % 1000;
$seconds = floor($time / 1000);
$minutes = floor($seconds / 60);
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
$seconds -= $hours * 60 + $minutes * 60;
$format = ($hours > 0 ? $hours . ':' : '');
$format .= ($hours > 0 && $minutes < 10 ? '0' : '') . $minutes . ':';
$format .= ($seconds < 10 ? '0' : '') . $seconds . ':';
$format .= ($milliseconds < 100 ? '0' : '') . ($milliseconds < 10 ? '0' : '') . $milliseconds;
return $format;
}
/**
* Formats the given time (seconds) to hh:mm:ss
*
* @param int $time
* @return string
*/
public static function formatTimeH($seconds) {
return gmdate("H:i:s", $seconds);
}
/**
* Convert given data to real boolean
*
@ -183,30 +106,6 @@ class Tools {
}
}
/**
* Converts the given boolean to an int representation
*
* @param bool $bool
* @return int
*/
public static function boolToInt($bool) {
return ($bool ? 1 : 0);
}
/**
* Build new simple xml element
*
* @param string $name
* @param string $id
* @return \SimpleXMLElement
*/
public static function newManialinkXml($id = null) {
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><manialink/>');
$xml->addAttribute('version', '1');
if ($id) $xml->addAttribute('id', $id);
return $xml;
}
/**
* Load config xml-file
*