Basic utility functions for PHP
toStdClass(array $array): stdClassConverts array to an stdClass
$users = [
"value" => [
"users" => [
[
"username" => "user",
"password" => "********",
"email" => "user@example.com"
]
]
],
"token" => "********************************"
];
$users = ArrayUtils::toStdClass($users);
var_dump($users);
/* output
object(stdClass)#2 (2) {
["value"]=>
object(stdClass)#4 (1) {
["users"]=>
object(stdClass)#5 (1) {
["0"]=>
object(stdClass)#6 (3) {
["username"]=>
string(4) "user"
["password"]=>
string(8) "********"
["email"]=>
string(16) "user@example.com"
}
}
}
["token"]=>
string(32) "********************************"
}
*/randomize(array $array): arrayrandomizes the given array
$characters = str_split("Command_String");
$randomized_characters = ArrayUtils::randomize($characters);
foreach ($randomized_characters as $character) {
echo $character;
}getBetween(string $start, string $end, string $string, bool $include_start_end_with_response = false): stringGets text between to specified points in a string and returns it.
$greeting = "My name is Bob! What's yours?";
$name = StringUtils::getBetween("is ", "!", $greeting);
echo $name; // output: BobIf you want the start and end to be returned you can set the fourth argument to true, a good use case for this would be parsing a json string out of a blob of text
$text = 'fdsjhyasdfuygfdsuygfduygfsd{"name": "Bob"}asduygasduyauysduytasduy?';
$json = StringUtils::getBetween("{", "}", $text, true);
echo $json; // output: {"name": "Bob"}
var_dump(json_decode($json));
/* output:
object(stdClass)#3 (1) {
["name"]=>
string(5) "Bob"
}
*/uuid(int $length = 16, array $characters = []): stringGenerates a UUID.
Generically the method will return a 16 character alphanumeric string
echo GeneratorUtils::uuid(); // output: 6MwqCff0wdpUl1sdwYou can adjust the length as needed
echo GeneratorUtils::uuid(5); // output: 8zWgWwYou can also supply characters for the generator to use
echo GeneratorUtils::uuid(10, [1, 0]); // output: 11110100100getAllFiles(string $directory, bool $recursive = false): arrayGet all files in a directory, if the second parameter is true then files in subdirectories will be included in the returned array
getAllSubDirectories(string $directory, bool $recursive = false): arrayGet all subdirectories in a directory and if recursive is true all subdirectories of the subdirectories will be included
getAllFilesWithExtensions(string $directory, array $extensionsToFind, bool $recursive = false): arrayGet all files in a directory with one of the supplied extensions. If the third parameter is true then the directories' subdirectories will be searched as well.
RGBAtoHEX(int $red, int $blue, int $green, ?int $alpha = null): stringConverts a RGBA color code to a HEX color code
HEXtoRGBA(string $hex): arrayConverts a HEX color code to a RGBA color code.
convertFileSize(FileSizeUtils $from_type, FileSizeUtils $to_type, float $from_size): floatConvert a file size from one type to another
humanReadable(FileSizeUtils $type, float $size, int $decimals = 0): stringCreates reduces the format that appends to type abbreviation to the end.
echo FileSizeUtils::humanReadable(FileSizeUtils::MEGABYTE, 5000); // output: 5 GBreduceFileSize(FileSizeUtils $type, float $size): stdClassReduces a file size to the smallest it can be before being smaller than 1. An stdClass with a type property is then returned alongside a size property for the new size.
var_dump(FileSizeUtils::humanReadable(FileSizeUtils::MEGABYTE, 5000));
/**
* object(stdClass)#12 (2) {
* ["type"]=>
* enum(CommandString\Utils\FileSizeUtils::GIGABYTE)
* ["size"]=>
* float(5)
* }
*/