Functions

True\Functions is the framework's grab-bag of static utility methods — encryption, URL parsing, browser detection, file-size formatting, and a few common string/array helpers. Reach for it instead of rolling your own when one of these patterns comes up.

Encryption & tokens

$encrypted = True\Functions::encrypt('Hello World!', 'mySecretKey');
$plain     = True\Functions::decrypt($encrypted, 'mySecretKey'); // null on failure

$token     = True\Functions::genToken();    // default 64 chars
$shorter   = True\Functions::genToken(32);
  • encrypt / decrypt — AES-128-CBC with a Base64-encoded payload.
  • genToken — cryptographically secure random token.

URL helpers

echo True\Functions::host();
// => https://www.example.com

$u = True\Functions::parseUrl('https://user:pass@www.example.com:8080/path?query=123#fragment');
// Returns an object of URL components. Pass `true` as the 2nd arg to also
// include the port and user-credentials parts.

echo True\Functions::getLastPartOfURL('https://www.example.com/path/to/page');
// => "page"
// Pass `true` instead of a URL to use the current REQUEST_URI.

Browser detection

$browser = True\Functions::getBrowser();
// => ['name' => 'Chrome', 'version' => '127']

if (True\Functions::supportsGrid($browser)) {
    echo 'CSS Grid is supported!';
}

Numbers & sizes

echo True\Functions::humanFileSize(1048576);   // => "1.00MB"
echo True\Functions::humanFileSize(1048576, 'KB'); // force the unit

echo True\Functions::dollars(1234.56);          // => "$1,234.56"

Strings & arrays

if (True\Functions::contains('Hello World', 'world')) {
    // case-insensitive by default; pass `false` as 3rd arg for case-sensitive
}

$keys  = ['name', 'email'];
$array = ['name' => 'John', 'email' => 'john@example.com'];

if (True\Functions::keys_exist($keys, $array)) {
    // all requested keys are present
}

API quick reference

MethodReturns
encrypt(string $str, string $key): stringBase64-encoded ciphertext.
decrypt(string $str, string $key): ?stringPlaintext, or null on failure.
genToken(int $length = 64): stringSecure random token.
host(): stringScheme + host of the current request.
parseUrl(string $url, bool $extra = false): objectObject of URL components.
getLastPartOfURL(string|bool $str): stringLast path segment.
getBrowser(): array['name', 'version'] from the UA string.
supportsGrid(array $params): boolWhether the given browser supports CSS Grid.
humanFileSize(int $size, string $unit = ''): stringHuman-readable size string.
contains(string $haystack, string $needle, bool $ci = true): boolSubstring check.
keys_exist(array $keys, array $array): boolAll-keys-present check.
dollars(float $amount): stringUSD-formatted string.