Added class Converter to \KimchiAPI\Utilities with static methods functionNameSafe() and truncateString()
This commit is contained in:
parent
e0502c7c28
commit
57fdf24155
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue