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.
284 lines
8.8 KiB
284 lines
8.8 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* @author Robert Strutts
|
|
* @copyright Copyright (c) 2022, Robert Strutts.
|
|
* @license MIT
|
|
*/
|
|
|
|
namespace CodeHydrater;
|
|
use CodeHydrater\strings\string_facade as F;
|
|
use CodeHydrater\enums\exit_on_dump as endDump;
|
|
|
|
final class common {
|
|
|
|
protected function __construct() {
|
|
|
|
}
|
|
|
|
public static function return_bool_as_int_bit(bool $b_data): int {
|
|
return ($b_data) ? 1 : 0; // if true=1, else =0
|
|
}
|
|
|
|
/**
|
|
* @todo add object checking...
|
|
* @param type $ret option to check for false error condition
|
|
* @return bool true if false/error found
|
|
*/
|
|
public static function is_error($ret): bool {
|
|
if ($ret === false) {
|
|
return true;
|
|
}
|
|
if (is_string($ret)) {
|
|
$lr = F::strtolower(F::trim($ret));
|
|
return ($lr === 'false') ? true : false;
|
|
}
|
|
throw new \Exception("Unknown state of on error");
|
|
}
|
|
|
|
public static function return_bool_as_yes_no(bool $b_data): string {
|
|
return ($b_data) ? 'y' : 'n'; // if true=y, else =n
|
|
}
|
|
|
|
public static function get_bool($bool, $throw = true): ?bool {
|
|
if (is_bool($bool)) {
|
|
return $bool;
|
|
}
|
|
if (is_string($bool)) {
|
|
$bool = F::strtolower(F::trim($bool));
|
|
}
|
|
switch ($bool) {
|
|
case ':null':
|
|
case null:
|
|
return null;
|
|
case '0':
|
|
case 'false':
|
|
case ':false':
|
|
case 'no':
|
|
case 'n':
|
|
case 'disable':
|
|
case 'disabled':
|
|
return false;
|
|
case '1':
|
|
case 'true':
|
|
case ':true':
|
|
case 'yes':
|
|
case 'y':
|
|
case 'enable':
|
|
case 'enabled':
|
|
return true;
|
|
default:
|
|
if ($throw === true) {
|
|
throw new \CodeHydrater\exceptions\Bool_Exception("Value: ({$bool})");
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function get_count($i): int {
|
|
return (is_array($i) || is_object($i)) ? count($i) : 0;
|
|
}
|
|
|
|
public static function str_replace_first(string $search, string $replace, string $subject): string {
|
|
$pos = strpos($subject, $search);
|
|
if ($pos !== false) {
|
|
return substr_replace($subject, $replace, $pos, strlen($search));
|
|
}
|
|
return $subject;
|
|
}
|
|
|
|
public static function deprecated_error(string $msg): void {
|
|
if (\CodeHydrater\bootstrap\configure::get('security', 'show_dumps') !== true) {
|
|
return; // Avoid Live Deprecated Errors
|
|
}
|
|
if (\PHP_VERSION_ID < 80400) {
|
|
trigger_error($msg);
|
|
echo "<pre>";
|
|
print_r(debug_backtrace()[1]);
|
|
echo "</pre>";
|
|
}
|
|
}
|
|
|
|
// Begin Strings Functions here::
|
|
public static function string_position(string $string, string $needle, int $offset = 0, $encoding = null) {
|
|
return F::strpos($string, $needle, $offset);
|
|
}
|
|
|
|
public static function string_last_position(string $string, string $needle, int $offset = 0, $encoding = null) {
|
|
return F::strrpos($string, $needle, $offset);
|
|
}
|
|
|
|
public static function string_trim($string, $charlist = null) {
|
|
if (is_null($charlist)) {
|
|
return F::trim($string);
|
|
} else {
|
|
$charlist = preg_quote($charlist, '/');
|
|
return preg_replace("/(^[$charlist]+)|([$charlist]+$)/us", '', $string);
|
|
}
|
|
}
|
|
|
|
public static function string_rtrim($string, $charlist = null) {
|
|
if (is_null($charlist)) {
|
|
return F::rtrim($string);
|
|
} else {
|
|
$charlist = preg_quote($charlist, '/');
|
|
return preg_replace("/([$charlist]+$)/us", '', $string);
|
|
}
|
|
}
|
|
|
|
public static function string_ltrim($string, $charlist = null) {
|
|
if (is_null($charlist)) {
|
|
return F::ltrim($string);
|
|
} else {
|
|
$charlist = preg_quote($charlist, '/');
|
|
return preg_replace("/(^[$charlist]+)/us", '', $string);
|
|
}
|
|
}
|
|
|
|
public static function string_cmp($str1, $str2, $encoding = null) {
|
|
return strcmp(F::strtoupper($str1, $encoding), F::strtoupper($str2, $encoding));
|
|
}
|
|
|
|
/*
|
|
* Case-Insensitive Find
|
|
*/
|
|
|
|
public static function string_first_position(string $string, string $needle, int $offset = 0, $encoding = null) {
|
|
return F::stripos($string, $needle, $offset);
|
|
}
|
|
|
|
public static function string_sub_part(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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Not really needed as str_contains($string, $needle) now exists!
|
|
* Will determine if data was found in string find and returns true if found.
|
|
* @param string $data
|
|
* @param string $find
|
|
* @retval bool
|
|
*/
|
|
public static function is_string_found(string $data, string $find): bool {
|
|
return (self::string_first_position($data, $find) !== false);
|
|
}
|
|
|
|
/**
|
|
* Will get only left part of string by length.
|
|
* @param string $str
|
|
* @param int $length
|
|
* @retval type string or false
|
|
*/
|
|
public static function get_string_left(string $str, int $length): false | string {
|
|
return self::string_sub_part($str, 0, $length);
|
|
}
|
|
|
|
public static function is_json(string $maybeJSON): bool {
|
|
$version = (float) phpversion();
|
|
if ($version >= 8.3) {
|
|
return json_validate($maybeJSON);
|
|
} else {
|
|
$obj = json_decode($maybeJSON);
|
|
return (json_last_error() === JSON_ERROR_NONE) ? true : false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Will get only the right part of string by length.
|
|
* @param string $str
|
|
* @param int $length
|
|
* @retval type string or false
|
|
*/
|
|
public static function get_string_right(string $str, int $length): false | string {
|
|
return self::string_sub_part($str, -$length);
|
|
}
|
|
|
|
public static function real_time_output(): void {
|
|
header("Content-type: text/plain");
|
|
// Turn off output buffering
|
|
ini_set('output_buffering', 'off');
|
|
// Turn off PHP output compression
|
|
ini_set('zlib.output_compression', false);
|
|
|
|
// Implicitly flush the buffer(s)
|
|
ini_set('implicit_flush', true);
|
|
ob_implicit_flush(true);
|
|
while (ob_get_level() > 0) {
|
|
// Get the curent level
|
|
$level = ob_get_level();
|
|
// End the buffering
|
|
ob_end_clean();
|
|
// If the current level has not changed, abort
|
|
if (ob_get_level() == $level) break;
|
|
}
|
|
}
|
|
/**
|
|
* Clear out from memory given variable by Reference!
|
|
* @param type $sensitive_data
|
|
*/
|
|
public static function wipe(& $sensitive_data): void {
|
|
if (function_exists("sodium_memzero")) {
|
|
sodium_memzero($sensitive_data);
|
|
}
|
|
unset($sensitive_data);
|
|
}
|
|
|
|
/**
|
|
* 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 (\CodeHydrater\bootstrap\configure::get('security', 'show_dumps') !== true) {
|
|
return;
|
|
}
|
|
// if (!is_object($var)) {
|
|
var_dump($var);
|
|
echo '<br>';
|
|
// }
|
|
|
|
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::get_count($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)) {
|
|
echo 'VAR is a STRING = ' . $var;
|
|
} else {
|
|
echo "<pre style=\"border: 1px solid #000; overflow: auto; margin: 0.5em;\">";
|
|
print_r($var);
|
|
echo '</pre>';
|
|
}
|
|
echo '<br><br>';
|
|
|
|
if ($end === endDump::exit_and_stop) {
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public static function nl2br(string $text): string {
|
|
return strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />'));
|
|
}
|
|
|
|
}
|
|
|