TrackManiaControl/libs/FML/Script/Builder.php

151 lines
3.9 KiB
PHP
Raw Normal View History

2014-01-03 17:18:14 +01:00
<?php
namespace FML\Script;
/**
2014-06-21 03:18:21 +02:00
* ManiaScript Builder class
2014-01-03 17:18:14 +01:00
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
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 {
2014-06-21 03:18:21 +02:00
/*
* Constants
*/
const EMPTY_STRING = '""';
2014-01-03 17:18:14 +01:00
/**
2014-06-21 03:18:21 +02:00
* Build a label implementation block
2014-01-03 17:18:14 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $labelName Name of the label
* @param string $implementationCode Label implementation coding (without declaration)
* @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
/**
2014-06-21 03:18:21 +02:00
* Escape dangerous characters in the given text
2014-01-19 19:30:21 +01:00
*
2014-05-16 22:44:22 +02:00
* @param string $text Text to escape
2014-06-21 03:18:21 +02:00
* @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
/**
2014-06-21 03:18:21 +02:00
* Get the 'Real' string representation of the given value
2014-01-05 16:04:55 +01:00
*
2014-06-21 03:18:21 +02:00
* @param float $value 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
/**
2014-06-21 03:18:21 +02:00
* Get the 'Boolean' string representation of the given value
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param bool $value Value to convert to a ManiaScript 'Boolean'
2014-02-16 13:59:28 +01:00
* @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) {
2014-06-21 03:18:21 +02:00
return 'True';
2014-02-16 13:59:28 +01:00
}
2014-06-21 03:18:21 +02:00
return 'False';
2014-02-16 13:59:28 +01:00
}
2014-04-27 14:44:40 +02:00
/**
2014-06-21 03:18:21 +02:00
* Get the string representation of the given array
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +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)) {
2014-06-21 03:18:21 +02:00
$arrayText .= '"' . static::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)) {
2014-06-21 03:18:21 +02:00
$arrayText .= '"' . static::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;
}
/**
2014-06-21 03:18:21 +02:00
* Get the include command for the given file and namespace
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $file Include file
* @param string $namespace (optional) Include namespace
2014-04-27 14:44:40 +02:00
* @return string
*/
2014-05-20 15:44:45 +02:00
public static function getInclude($file, $namespace = null) {
if (!$namespace && stripos($file, '.') === false) {
$namespace = $file;
}
2014-06-21 03:18:21 +02:00
$file = static::escapeText($file, true);
$includeText = "#Include {$file}";
2014-05-20 15:44:45 +02:00
if ($namespace) {
$includeText .= " as {$namespace}";
}
$includeText .= PHP_EOL;
2014-04-27 14:44:40 +02:00
return $includeText;
}
/**
2014-06-21 03:18:21 +02:00
* Get the constant command for the given name and value
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $name Constant name
* @param string $value Constant value
2014-04-27 14:44:40 +02:00
* @return string
*/
public static function getConstant($name, $value) {
if (is_string($value)) {
2014-06-21 03:18:21 +02:00
$value = static::escapeText($value, true);
} else if (is_bool($value)) {
$value = static::getBoolean($value);
2014-04-27 14:44:40 +02:00
}
$constantText = "#Const {$name} {$value}" . PHP_EOL;
return $constantText;
}
2013-12-31 02:55:19 +01:00
}