TrackManiaControl/application/core/Libs/FML/Script/Builder.php

137 lines
3.6 KiB
PHP
Raw Normal View History

2014-01-03 17:18:14 +01:00
<?php
namespace FML\Script;
/**
* Builder Class offering Methods to build ManiaScript
*
2014-05-16 22:44:22 +02:00
* @author steeffeen
2014-04-13 18:21:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
2014-05-16 22:44:22 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-01-03 17:18:14 +01:00
*/
abstract class Builder {
/**
* Build a Label Implementation Block
*
2014-05-16 22:44:22 +02:00
* @param string $labelName Name of the Label
2014-01-12 00:51:46 +01:00
* @param string $implementationCode Label Implementation Coding (without declaration)
2014-05-16 22:44:22 +02:00
* @param bool $isolate Whether the Code should be isolated in an own Block
2014-01-03 17:18:14 +01:00
* @return string
*/
2014-04-27 14:44:40 +02:00
public static function getLabelImplementationBlock($labelName, $implementationCode, $isolate = true) {
if ($isolate) {
$implementationCode = 'if(True){' . $implementationCode . '}';
}
2014-01-03 17:18:14 +01:00
$labelText = PHP_EOL . "***{$labelName}***" . PHP_EOL . "***{$implementationCode}***" . PHP_EOL;
return $labelText;
}
2014-01-05 16:04:55 +01:00
2014-01-19 19:30:21 +01:00
/**
* Escape dangerous Characters in the given Text
*
2014-05-16 22:44:22 +02:00
* @param string $text Text to escape
* @param bool $addApostrophes (optional) Whether to add Apostrophes before and after the Text
2014-01-19 19:30:21 +01:00
* @return string
*/
2014-04-27 14:44:40 +02:00
public static function escapeText($text, $addApostrophes = false) {
2014-05-16 22:44:22 +02:00
$dangers = array('\\', '"', "\n");
2014-04-27 14:44:40 +02:00
$replacements = array('\\\\', '\\"', '\\n');
2014-05-16 22:44:22 +02:00
$escapedText = str_ireplace($dangers, $replacements, $text);
2014-04-27 14:44:40 +02:00
if ($addApostrophes) {
$escapedText = '"' . $escapedText . '"';
}
2014-01-19 19:30:21 +01:00
return $escapedText;
}
2014-01-05 16:04:55 +01:00
/**
* Get the Real String-Representation of the given Value
*
2014-01-19 19:30:21 +01:00
* @param float $value The Float Value to convert to a ManiaScript Real
2014-01-05 16:04:55 +01:00
* @return string
*/
public static function getReal($value) {
2014-05-16 22:44:22 +02:00
$value = (float)$value;
$stringVal = (string)$value;
2014-04-27 14:44:40 +02:00
if (!fmod($value, 1)) {
$stringVal .= '.';
}
2014-01-05 16:04:55 +01:00
return $stringVal;
}
2014-04-27 14:44:40 +02:00
2014-02-16 13:59:28 +01:00
/**
* Get the Boolean String-Representation of the given Value
2014-04-27 14:44:40 +02:00
*
2014-02-16 13:59:28 +01:00
* @param bool $value The Value to convert to a ManiaScript Boolean
* @return string
*/
public static function getBoolean($value) {
2014-05-16 22:44:22 +02:00
$bool = (bool)$value;
2014-02-16 13:59:28 +01:00
if ($bool) {
return "True";
}
return "False";
}
2014-04-27 14:44:40 +02:00
/**
* Get the String-Representation of the given Array
*
2014-05-16 22:44:22 +02:00
* @param array $array Array to convert to a ManiaScript Array
* @param bool $associative (optional) Whether the Array should be associative
2014-04-27 14:44:40 +02:00
* @return string
*/
public static function getArray(array $array, $associative = false) {
$arrayText = '[';
2014-05-16 22:44:22 +02:00
$index = 0;
$count = count($array);
2014-04-27 14:44:40 +02:00
foreach ($array as $key => $value) {
if ($associative) {
if (is_string($key)) {
$arrayText .= '"' . self::escapeText($key) . '"';
2014-05-16 22:44:22 +02:00
} else {
2014-04-27 14:44:40 +02:00
$arrayText .= $key;
}
$arrayText .= ' => ';
}
if (is_string($value)) {
$arrayText .= '"' . self::escapeText($value) . '"';
2014-05-16 22:44:22 +02:00
} else {
2014-04-27 14:44:40 +02:00
$arrayText .= $value;
}
if ($index < $count - 1) {
$arrayText .= ', ';
$index++;
}
}
$arrayText .= ']';
return $arrayText;
}
/**
* Get the Include Command for the given File and Namespace
*
2014-05-16 22:44:22 +02:00
* @param string $file Include File
2014-04-27 14:44:40 +02:00
* @param string $namespace Include Namespace
* @return string
*/
public static function getInclude($file, $namespace) {
$includeText = "#Include \"{$file}\" as {$namespace}" . PHP_EOL;
return $includeText;
}
/**
* Get the Constant Command for the given Name and Value
*
2014-05-16 22:44:22 +02:00
* @param string $name Constant Name
2014-04-27 14:44:40 +02:00
* @param string $value Constant Value
* @return string
*/
public static function getConstant($name, $value) {
if (is_string($value)) {
$value = '"' . $value . '"';
}
$constantText = "#Const {$name} {$value}" . PHP_EOL;
return $constantText;
}
2013-12-31 02:55:19 +01:00
}