TrackManiaControl/libs/FML/Script/Builder.php

251 lines
6.7 KiB
PHP
Raw Normal View History

2014-01-03 17:18:14 +01:00
<?php
namespace FML\Script;
2017-03-25 18:40:15 +01:00
use FML\Types\Identifiable;
2014-01-03 17:18:14 +01:00
/**
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>
2017-03-25 18:40:15 +01:00
* @copyright FancyManiaLinks Copyright © 2017 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
*/
2017-03-25 18:40:15 +01:00
abstract class Builder
{
/*
* Constants
*/
const EMPTY_STRING = '""';
/**
* Build a script label implementation block
*
* @api
* @param string $labelName Name of the label
* @param string $implementationCode Label implementation coding (without declaration)
* @param bool $isolate (optional) If the code should be isolated in an own block
* @return string
*/
public static function getLabelImplementationBlock($labelName, $implementationCode, $isolate = true)
{
if ($isolate) {
$implementationCode = "if(True){{$implementationCode}}";
}
return "
***{$labelName}***
***{$implementationCode}***
";
}
/**
* Escape dangerous characters in the given text
*
* @api
* @param string $text Text to escape
* @param bool $addApostrophes (optional) Add apostrophes before and after the text
* @return string
*/
2023-01-16 16:15:32 +01:00
public static function escapeText($text = "", $addApostrophes = true)
2017-03-25 18:40:15 +01:00
{
$dangers = array('\\', '"', "\n");
$replacements = array('\\\\', '\\"', '\\n');
$escapedText = str_ireplace($dangers, $replacements, $text);
if ($addApostrophes) {
$escapedText = '"' . $escapedText . '"';
}
return $escapedText;
}
/**
* Get the escaped Id of the given Element
*
* @param Identifiable $element Element
* @return string
*/
public static function getId(Identifiable $element)
{
return static::escapeText($element->getId(), false);
}
2017-06-21 19:43:38 +02:00
/**
* Get the 'Text' string representation of the given value
*
* @api
* @param string $value String value to convert to a ManiaScript 'Text'
* @return string
*/
public static function getText($value)
{
return '"' . (string)$value . '"';
}
/**
* Get the 'Integer' string representation of the given value
*
* @api
* @param int $value Int value to convert to a ManiaScript 'Integer'
* @return string
*/
public static function getInteger($value)
{
return (string)(int)$value;
}
2017-03-25 18:40:15 +01:00
/**
* Get the 'Real' string representation of the given value
*
* @api
* @param float $value Float value to convert to a ManiaScript 'Real'
* @return string
*/
public static function getReal($value)
{
$value = (float)$value;
$stringVal = (string)$value;
if (!fmod($value, 1)) {
$stringVal .= ".";
}
return $stringVal;
}
/**
* Get the 'Boolean' string representation of the given value
*
* @api
* @param bool $value Value to convert to a ManiaScript 'Boolean'
* @return string
*/
public static function getBoolean($value)
{
$bool = (bool)$value;
if ($bool) {
return "True";
}
return "False";
}
/**
* Get the Vec3 representation for the given values
*
* @api
* @param float|float[] $valueX Value X
* @param float $valueY (optional) Value Y
* @return string
*/
public static function getVec2($valueX, $valueY = null)
{
if (is_array($valueX)) {
$valueY = (isset($valueX[1]) ? $valueX[1] : 0.);
$valueX = (isset($valueX[0]) ? $valueX[0] : 0.);
}
return "<" . static::getReal($valueX) . "," . static::getReal($valueY) . ">";
}
/**
* Get the Vec3 representation for the given values
*
* @api
* @param float|float[] $valueX Value X
* @param float $valueY (optional) Value Y
* @param float $valueZ (optional) Value Z
* @return string
*/
public static function getVec3($valueX, $valueY = null, $valueZ = null)
{
if (is_array($valueX)) {
$valueZ = (isset($valueX[2]) ? $valueX[2] : 0.);
$valueY = (isset($valueX[1]) ? $valueX[1] : 0.);
$valueX = (isset($valueX[0]) ? $valueX[0] : 0.);
}
return "<" . static::getReal($valueX) . "," . static::getReal($valueY) . "," . static::getReal($valueZ) . ">";
}
/**
* Get the string representation of the given array
*
* @api
* @param array $array Array to convert to a ManiaScript array
* @param bool $associative (optional) Whether the array should be associative
* @return string
*/
2017-06-21 19:43:38 +02:00
public static function getArray(array $array, $associative = true)
2017-03-25 18:40:15 +01:00
{
$arrayText = "[";
$index = 0;
$count = count($array);
foreach ($array as $key => $value) {
if ($associative) {
$arrayText .= static::getValue($key);
$arrayText .= " => ";
}
$arrayText .= static::getValue($value);
if ($index < $count - 1) {
$arrayText .= ", ";
$index++;
}
}
return $arrayText . "]";
}
/**
* Get the string representation for the given value
*
* @api
* @param mixed $value Value
* @return string
*/
public static function getValue($value)
{
if (is_string($value)) {
return static::escapeText($value);
}
if (is_bool($value)) {
return static::getBoolean($value);
2017-06-21 19:43:38 +02:00
}
if (is_array($value)) {
return static::getArray($value);
2017-03-25 18:40:15 +01:00
}
return $value;
}
/**
* Get the include command for the given file and namespace
*
* @api
* @param string $file Include file
* @param string $namespace (optional) Include namespace
* @return string
*/
public static function getInclude($file, $namespace = null)
{
if (!$namespace && stripos($file, ".") === false) {
$namespace = $file;
}
$file = static::escapeText($file);
$includeText = "#Include {$file}";
if ($namespace) {
$includeText .= " as {$namespace}";
}
return $includeText . "
";
}
/**
* Get the constant command for the given name and value
*
* @api
* @param string $name Constant name
* @param string $value Constant value
* @return string
*/
public static function getConstant($name, $value)
{
$value = static::getValue($value);
return "#Const {$name} {$value}
";
}
2013-12-31 02:55:19 +01:00
}