Added class Converter to \KimchiAPI\Utilities with static methods functionNameSafe() and truncateString()

This commit is contained in:
Zi Xing 2021-12-15 13:27:15 -05:00
parent e0502c7c28
commit 57fdf24155
2 changed files with 52 additions and 0 deletions

View File

@ -4,6 +4,9 @@
// Define server information for response headers
use KimchiAPI\Exceptions\MissingComponentsException;
use KimchiRPC\Abstracts\ServerMode;
use KimchiRPC\Abstracts\Types\ProtocolType;
use KimchiRPC\Utilities\Converter;
use RuntimeException;
if(defined("KIMCHI_API_SERVER") == false)
@ -24,5 +27,14 @@
class KimchiAPI
{
/**
* Server constructor.
* @param string $server_name
*/
public function __construct(string $server_name)
{
$this->methods = [];
$this->server_name = Converter::functionNameSafe($server_name);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace KimchiAPI\Utilities;
class Converter
{
/**
* Converts the string to a function safe name
*
* @param string $input
* @return string
*/
public static function functionNameSafe(string $input): string
{
$out = preg_replace("/(?>[^\w\.])+/", "_", $input);
// Replace any underscores at start or end of the string.
if ($out[0] == "_")
{
$out = substr($out, 1);
}
if ($out[-1] == "_")
{
$out = substr($out, 0, -1);
}
return $out;
}
/**
* Truncates a long string
*
* @param string $input
* @param int $length
* @return string
*/
public static function truncateString(string $input, int $length): string
{
return (strlen($input) > $length) ? substr($input,0, $length).'...' : $input;
}
}