You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.7 KiB

<?php
declare(strict_types=1);
namespace IOcornerstone\Framework;
use IOcornerstone\Framework\Enum\ExitOnDump as endDump;
use IOcornerstone\Framework\Console;
use IOcornerstone\Framework\String\StringFacade as F;
final class Common
{
/**
* Clear out from memory given variable by Reference!
* @param type $sensitive_data
*/
public static function wipe(#[\SensitiveParameter] &$sensitive_data): void
{
if (function_exists("sodium_memzero")) {
sodium_memzero($sensitive_data);
}
unset($sensitive_data);
}
public static function getCount($i): int
{
return (is_array($i) || is_object($i)) ? count($i) : 0;
}
public static function stringSubPart(string $string, int $offset = 0, ?int $length = null, $encoding = null) {
if ($length === null) {
return F::substr($string, $offset, strlen($string));
} else {
return F::substr($string, $offset, $length);
}
}
/**
* Will get only left part of string by length.
* @param string $str
* @param int $length
* @retval type string or false
*/
public static function getStringLeft(string $str, int $length): false | string {
return self::stringSubPart($str, 0, $length);
}
/**
* Will get only the right part of string by length.
* @param string $str
* @param int $length
* @retval type string or false
*/
public static function getStringRight(string $str, int $length): false | string {
return self::stringSubPart($str, -$length);
}
/**
* Variable Dump and exit
* Configure of security for show_dumps must be true for debugging.
* @param var - any type will display type and value of contents
* @param bool end - if true ends the script
*/
public static function dump(
$var = 'nothing',
endDump $end = endDump::EXIT_AND_STOP
): void {
if (\IOcornerstone\Framework\Configure::get('security', 'show_dumps') !== true) {
return;
}
$isConsole = Console::isConsole();
if (! $isConsole) {
echo "<details>\r\n<summary>Expand to see Var Dump:</summary>";
echo "<p>";
var_dump($var);
echo "</p>";
echo "</details>";
} else {
var_dump($var);
echo PHP_EOL;
}
if ($var === false) {
echo 'It is FALSE!';
} elseif ($var === true) {
echo 'It is TRUE!';
} elseif (is_resource($var)) {
echo 'VAR IS a RESOURCE';
} elseif (is_array($var) && self::getCount($var) == 0) {
echo 'VAR IS an EMPTY ARRAY!';
} elseif (is_numeric($var)) {
echo 'VAR is a NUMBER = ' . $var;
} elseif (empty($var) && !is_null($var)) {
echo 'VAR IS EMPTY!';
} elseif ($var == 'nothing') {
echo 'MISSING VAR!';
} elseif (is_null($var)) {
echo 'VAR IS NULL!';
} elseif (is_string($var)) {
if (! $isConsole) {
echo 'VAR is a STRING = ' . htmlentities($var);
} else {
echo 'VAR is a STRING = ' . $var;
}
} else {
if (! $isConsole) {
echo "<pre style=\"border: 1px solid green; overflow: auto; margin: 0.5em;\">";
print_r($var);
echo '</pre>';
} else {
print_r($var);
echo PHP_EOL;
}
}
if (! $isConsole) {
echo '<br><br>';
}
if ($end === endDump::EXIT_AND_STOP) {
exit;
}
}
/**
* Please note that not all web servers support: HTTP_X_REQUESTED_WITH
* So, you need to code more checks!
* @retval boolean true if AJAX request by JQuery, etc...
*/
public static function isAjax(): bool {
$http_x_requested_with = $_SERVER['HTTP_X_REQUESTED_WITH'] ?? "";
return (strtolower($http_x_requested_with) === 'xmlhttprequest');
}
/**
* site http://php.net/manual/en/function.base64-encode.php
*/
public static function base64urlEncode(string $data): string {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
public static function base64urlDecode(string $data): string {
//return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
return base64_decode( strtr( $data, '-_', '+/') . str_repeat('=', 3 - ( 3 + strlen( $data )) % 4 ));
}
public static function nl2br(string $text): string
{
return strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />'));
}
}