PHP code example of torugo / util

1. Go to this page and download the library: Download torugo/util library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

torugo / util example snippets

 
use Torugo\Util\CDT\CDT;

$cdt = CDT::get(); // returns something like "SGVU9Z2WV"

$cdt = CDT::fromTimestamp(416410245.1234); // returns "6VX4790YA"

$dateTime = \DateTime::createFromFormat("Y-m-d H:i:s.u", "2017-08-01 14:45:56.789");
$cdt = CDT::fromDateTime($dateTime); // returns "OU0H0K0LX"

$micro = CDT::toMicrotime("6VX4790YA"); // returns 416410245.1234

$dateTime = CDT::toDateTime("6VX4790YA"); // returns an instance of DateTime

$dw = new DateWriter( \DateTime $dt, string $language );
$dw->write(string $format);

use Torugo\Util\DateWriter\DateWriter;

$dateTime = \DateTime::createFromFormat("Y-m-d H:i:s", "2017-08-01 15:30:45");
$dw = new DateWriter($dateTime, "pt");

$dw->write("[São Paulo,] j [de] %{F} [de] Y");
// São Paulo, 1 de agosto de 2017

$dw->write("*{l}");
// TERÇA-FEIRA

use Torugo\Util\SemVer\SemVer;

$version = new SemVer("1.0.0");

$version->compareTo("1.0.0");      // returns VersionComparison::Equal
$version->compareTo("1.0.1");      // returns VersionComparison::Smaller
$version->compareTo("1.0.0-beta"); // returns VersionComparison::Bigger

use Torugo\Util\TBase64\TBase64;

$b64 = TBase64::encode("My String"); // => "TXkgU3RyaW5n"
$decoded = TBase64::decode($b64); // => "My String"

use Torugo\Util\TEncrypt\TEncrypt;
use Torugo\Util\TEncrypt\Enums\TCipher;

$text = "May the force be with you!";
$key = "ye-PaJYnFPluROpIFo146zhQNvKHbUkIKNMc2rkd8rE";

$encrypted = TEncrypt::encrypt($text, $key,);  // Encrypts the text
$decrypt = TEncrypt::decrypt($encrypted,$key); // Decrypts the encrypted text

TEncrypt::setCipher(TCipher::CAMELLIA_256_OFB);

use Torugo\Util\TFile\TFile;

TFile::exists(string $path, bool $createIfNotExists): bool

TFile::create(string $path): bool

$file = new TFile(__DIR__ . "/file.txt");

$isWritable = $file->isWritable();

$file = new TFile(__DIR__ . "/file.txt");

$lines = $file->getLines();

$file = new TFile(__DIR__ . "/.env");

$env = $file->parseEnv();

$file = new TFile(__DIR__ . "/file.json");

$json = $file->parseJson();

$file = new TFile(__DIR__ . "/mykey.key");

$key = $file->parseKeyFile();

use Torugo\Util\TPass\TPass;

use Torugo\Util\TUID\TUID;

$short = TUID::short();
$medium = TUID::medium();
$long = TUID::long();

$tuid = "PVA4M433-20L5-K1HVUPLQW-TL0SHULDI0VT";
TUID::validate($tuid); // returns true

$tuid = "PVA4M433-20L5-K1HVUPLQW-TL0SHULDI0VT";
TUID::getDateTime($tuid); // returns a PHP DateTime instance

use Torugo\Util\TRandom\TRandom;

$tRandom = new TRandom;

$rnd = $tRandom->string(10); // Generates 10 chars long random string

$random->alpha = "ABCDEF";
$random->numbers = "123";
$random->symbols = "#$%&*";

$str = $random->string(10);
// Generates a random string with the given characters.

$tRandom->number(1001, 9999); // Generates a random number between 1001 and 9999

$tRandom->lzNumber(1, 9999, 4); // 0001 ... 9999
$tRandom->lzNumber(1001, 999999, null); // 001001 ... 999999

use Torugo\Util\Traits\EmptyValues;

class MyClass {
    use EmptyValues;

    function myFunction() {
        // ...
        $type = $this->getEmptyValueForType(gettype($var));
        // ...
    }
}


use Torugo\Util\Traits\FromArrayFactory;

class UserDto
{
    use FromArrayFactory;

    public string $name;
    public string $email;
    public string $password;
}

$payload = [
    "name" => "Full User Name",
    "email" => "[email protected]",
    "password" => "SuperStrongPassword!",
];

$instance = UserDto::fromArray($payload);
// $instance->name ==> "Full User Name"
// $instance->email ==> "[email protected]"
// $instance->password ==> "SuperStrongPassword!"


"123456" => 0,
"112233" => 0,
"admin" => 0,
"password" => 0,
"psw1223!A" => 1,
"NU$;K^9" => 2,
"NU$;k3+" => 3,
"NU$;K^+B#D!;+D%8nP" => 4,
"123456NU$;K^+B#D!;+D%8nP" => 4,