Fixes for CoreLibs based on phpunit tests
Add note that on change in lib/ folder (add/name/delete) 'composer dump-autoload' must be run to update the composer auto loader as this is currently on testing to not use internal autoloader update all composer/vender autoload configs Check\Basic: just apply updates in deprecated method calls Check\Jason: has been deprecaged and moved to Convert\Json. Primary issue was wrong name "Jason" instead of "Json" Check\Password: only Check\PhpVersion: fix version check for >9 numbers Combined\Array: variable name change to be more clear, all searches are strict for recoursive search, new option for recoursive search many to return only found array data and no control array info. for backwards compatible this is default set to ($old = true) and needs to be set to false to get the new format, array search normal has a new strict flag for forcing strict compare on search. remove some unneeded is_array checks, fixed the flatten array to key to not only use leave elements, but all array keys, if only leaves are wanted the new method flattenArrayKeyLeavesOnly only returns key from leaves Combined\DateTime: checkDateTime got more correct error checks on invalid data compareDate uses strtotimestamp for more easier compare like compareDateTime does, both to a check on inalid timestamp now calcDaysInterval also aborts on invalid data now Convert\Byte: str to bytes does not drop the minus sign anymore Convert\Colors: any error will now return false and not set to some neutral gray. also fix missing round on hsb/hsl special return groups Convert\Html: add constants for CHECKED/SELECTED options, fix remove linebreak to not add two spaces if \r\n was found Convert\Json: moved from Check\Jason and add two new error types Convert\MimeAppName: do not set if mime type or app name is empty Create\Hash: add crc32b to hash allows types so we can create a normal not reversed crc32b Create\Uids: move default hash type to var in class, fix defined constant check Debug\FileWriter: add log folder setting to override config constant settings and also check if we can actually write to the folder and if BASE and LOG constants are not empty Get\System: add constant for getPageName and fix getHostName to be more shorter and faster Language\L10n: remove \Basic class extends because we don't need it there at all Template\SmartyExtend: fix constant check
This commit is contained in:
@@ -124,22 +124,26 @@ class Byte
|
||||
$valid_units_ = 'bkmgtpezy';
|
||||
// detects up to exo bytes
|
||||
preg_match(
|
||||
"/([\d.,]*)\s?(eib|pib|tib|gib|mib|kib|eb|pb|tb|gb|mb|kb|e|p|t|g|m|k|b)$/i",
|
||||
"/(-)?([\d.,]*)\s?(eib|pib|tib|gib|mib|kib|eb|pb|tb|gb|mb|kb|e|p|t|g|m|k|b)$/i",
|
||||
strtolower((string)$number),
|
||||
$matches
|
||||
);
|
||||
if (isset($matches[1]) && isset($matches[2])) {
|
||||
if (isset($matches[2]) && isset($matches[3])) {
|
||||
// remove all non valid characters from the number
|
||||
$number = preg_replace('/[^0-9\.]/', '', $matches[1]);
|
||||
$number = preg_replace('/[^0-9\.]/', '', $matches[2]);
|
||||
// final clean up and convert to float
|
||||
$number = (float)trim($number);
|
||||
// convert any mb/gb/etc to single m/b
|
||||
$unit = preg_replace('/[^bkmgtpezy]/i', '', $matches[2]);
|
||||
$unit = preg_replace('/[^bkmgtpezy]/i', '', $matches[3]);
|
||||
if ($unit) {
|
||||
$number = $number * pow($si ? 1000 : 1024, stripos($valid_units_, $unit[0]) ?: 0);
|
||||
}
|
||||
// convert to INT to avoid +E output
|
||||
$number = (int)round($number);
|
||||
// if negative input, keep nnegative
|
||||
if (!empty($matches[1])) {
|
||||
$number *= -1;
|
||||
}
|
||||
}
|
||||
// if not matching return as is
|
||||
return $number;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
/*
|
||||
* Convert color spaces
|
||||
* hex to rgb
|
||||
* rgb to hex
|
||||
* hex to rgb
|
||||
* rgb to hsb
|
||||
* hsb to rgb
|
||||
* rgb to hsl
|
||||
@@ -16,59 +16,27 @@ namespace CoreLibs\Convert;
|
||||
|
||||
class Colors
|
||||
{
|
||||
/**
|
||||
* converts a hex RGB color to the int numbers
|
||||
* @param string $hexStr RGB hexstring
|
||||
* @param bool $returnAsString flag to return as string
|
||||
* @param string $seperator string seperator: default: ","
|
||||
* @return string|array<string,float|int>|bool false on error or array with RGB
|
||||
* or a string with the seperator
|
||||
*/
|
||||
public static function hex2rgb(string $hexStr, bool $returnAsString = false, string $seperator = ',')
|
||||
{
|
||||
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
|
||||
if (!is_string($hexStr)) {
|
||||
return false;
|
||||
}
|
||||
$rgbArray = [];
|
||||
if (strlen($hexStr) == 6) {
|
||||
// If a proper hex code, convert using bitwise operation. No overhead... faster
|
||||
$colorVal = hexdec($hexStr);
|
||||
$rgbArray['R'] = 0xFF & ($colorVal >> 0x10);
|
||||
$rgbArray['G'] = 0xFF & ($colorVal >> 0x8);
|
||||
$rgbArray['B'] = 0xFF & $colorVal;
|
||||
} elseif (strlen($hexStr) == 3) {
|
||||
// If shorthand notation, need some string manipulations
|
||||
$rgbArray['R'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
|
||||
$rgbArray['G'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
|
||||
$rgbArray['B'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
|
||||
} else {
|
||||
// Invalid hex color code
|
||||
return false;
|
||||
}
|
||||
// returns the rgb string or the associative array
|
||||
return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* converts the rgb values from int data to the valid rgb html hex string
|
||||
* optional can turn of leading #
|
||||
* if one value is invalid, will return false
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @param bool $hex_prefix default true, prefix with "#"
|
||||
* @return string rgb in hex values with leading # if set
|
||||
* @return string|bool rgb in hex values with leading # if set,
|
||||
* false for invalid color
|
||||
*/
|
||||
public static function rgb2hex(int $red, int $green, int $blue, bool $hex_prefix = true): string
|
||||
public static function rgb2hex(int $red, int $green, int $blue, bool $hex_prefix = true)
|
||||
{
|
||||
$hex_color = '';
|
||||
if ($hex_prefix === true) {
|
||||
$hex_color = '#';
|
||||
}
|
||||
foreach (['red', 'green', 'blue'] as $color) {
|
||||
// if not valid, set to gray
|
||||
// if not valid, abort
|
||||
if ($$color < 0 || $$color > 255) {
|
||||
$$color = 125;
|
||||
return false;
|
||||
}
|
||||
// pad left with 0
|
||||
$hex_color .= str_pad(dechex($$color), 2, '0', STR_PAD_LEFT);
|
||||
@@ -77,20 +45,59 @@ class Colors
|
||||
}
|
||||
|
||||
/**
|
||||
* converts a hex RGB color to the int numbers
|
||||
* @param string $hexStr RGB hexstring
|
||||
* @param bool $return_as_string flag to return as string
|
||||
* @param string $seperator string seperator: default: ","
|
||||
* @return string|array<string,float|int>|bool false on error or array with RGB
|
||||
* or a string with the seperator
|
||||
*/
|
||||
public static function hex2rgb(
|
||||
string $hexStr,
|
||||
bool $return_as_string = false,
|
||||
string $seperator = ','
|
||||
) {
|
||||
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
|
||||
if (!is_string($hexStr)) {
|
||||
return false;
|
||||
}
|
||||
$rgbArray = [];
|
||||
if (strlen($hexStr) == 6) {
|
||||
// If a proper hex code, convert using bitwise operation. No overhead... faster
|
||||
$colorVal = hexdec($hexStr);
|
||||
$rgbArray['r'] = 0xFF & ($colorVal >> 0x10);
|
||||
$rgbArray['g'] = 0xFF & ($colorVal >> 0x8);
|
||||
$rgbArray['b'] = 0xFF & $colorVal;
|
||||
} elseif (strlen($hexStr) == 3) {
|
||||
// If shorthand notation, need some string manipulations
|
||||
$rgbArray['r'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
|
||||
$rgbArray['g'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
|
||||
$rgbArray['b'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
|
||||
} else {
|
||||
// Invalid hex color code
|
||||
return false;
|
||||
}
|
||||
// returns the rgb string or the associative array
|
||||
return $return_as_string ? implode($seperator, $rgbArray) : $rgbArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* rgb2hsb does not clean convert back to rgb in a round trip
|
||||
* converts RGB to HSB/V values
|
||||
* returns:
|
||||
* array with hue (0-360), sat (0-100%), brightness/value (0-100%)
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<int|float> Hue, Sat, Brightness/Value
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<int|float>|bool Hue, Sat, Brightness/Value
|
||||
* false for input value error
|
||||
*/
|
||||
public static function rgb2hsb(int $red, int $green, int $blue): array
|
||||
public static function rgb2hsb(int $red, int $green, int $blue)
|
||||
{
|
||||
// check that rgb is from 0 to 255
|
||||
foreach (['red', 'green', 'blue'] as $c) {
|
||||
if ($$c < 0 || $$c > 255) {
|
||||
$$c = 0;
|
||||
return false;
|
||||
}
|
||||
$$c = $$c / 255;
|
||||
}
|
||||
@@ -122,31 +129,35 @@ class Colors
|
||||
}
|
||||
|
||||
/**
|
||||
* hsb2rgb does not clean convert back to hsb in a round trip
|
||||
* converts HSB/V to RGB values RGB is full INT
|
||||
* @param int $H hue 0-360
|
||||
* @param int $S saturation 0-100 (int)
|
||||
* @param int $V brightness/value 0-100 (int)
|
||||
* @return array<int> 0 red/1 green/2 blue array as 0-255
|
||||
* if HSB/V value is invalid, sets this value to 0
|
||||
* @param int $H hue 0-360 (int)
|
||||
* @param int $S saturation 0-100 (int)
|
||||
* @param int $V brightness/value 0-100 (int)
|
||||
* @return array<int>|bool 0 red/1 green/2 blue array as 0-255
|
||||
* false for input value error
|
||||
*/
|
||||
public static function hsb2rgb(int $H, int $S, int $V): array
|
||||
public static function hsb2rgb(int $H, int $S, int $V)
|
||||
{
|
||||
// check that H is 0 to 359, 360 = 0
|
||||
// and S and V are 0 to 1
|
||||
if ($H < 0 || $H > 359) {
|
||||
$H = 0;
|
||||
return false;
|
||||
}
|
||||
if ($S < 0 || $S > 100) {
|
||||
$S = 0;
|
||||
return false;
|
||||
}
|
||||
if ($V < 0 || $V > 100) {
|
||||
$V = 0;
|
||||
return false;
|
||||
}
|
||||
// convert to internal 0-1 format
|
||||
$S /= 100;
|
||||
$V /= 100;
|
||||
|
||||
if ($S == 0) {
|
||||
return [$V * 255, $V * 255, $V * 255];
|
||||
$V = (int)round($V * 255);
|
||||
return [$V, $V, $V];
|
||||
}
|
||||
|
||||
$Hi = floor($H / 60);
|
||||
@@ -203,17 +214,18 @@ class Colors
|
||||
* converts a RGB (0-255) to HSL
|
||||
* return:
|
||||
* array with hue (0-360), saturation (0-100%) and luminance (0-100%)
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<float> hue/sat/luminance
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<float>|bool hue/sat/luminance
|
||||
* false for input value error
|
||||
*/
|
||||
public static function rgb2hsl(int $red, int $green, int $blue): array
|
||||
public static function rgb2hsl(int $red, int $green, int $blue)
|
||||
{
|
||||
// check that rgb is from 0 to 255
|
||||
foreach (['red', 'green', 'blue'] as $c) {
|
||||
if ($$c < 0 || $$c > 255) {
|
||||
$$c = 0;
|
||||
return false;
|
||||
}
|
||||
$$c = $$c / 255;
|
||||
}
|
||||
@@ -254,21 +266,25 @@ class Colors
|
||||
|
||||
/**
|
||||
* converts an HSL to RGB
|
||||
* @param int $hue hue: 0-360 (degrees)
|
||||
* @param float $sat saturation: 0-100
|
||||
* @param float $lum luminance: 0-100
|
||||
* @return array<int,float|int> red/blue/green 0-255 each
|
||||
* if HSL value is invalid, set this value to 0
|
||||
* @param int|float $hue hue: 0-360 (degrees)
|
||||
* @param float $sat saturation: 0-100
|
||||
* @param float $lum luminance: 0-100
|
||||
* @return array<int,float|int>|bool red/blue/green 0-255 each
|
||||
*/
|
||||
public static function hsl2rgb(int $hue, float $sat, float $lum): array
|
||||
public static function hsl2rgb($hue, float $sat, float $lum)
|
||||
{
|
||||
if (!is_numeric($hue)) {
|
||||
return false;
|
||||
}
|
||||
if ($hue < 0 || $hue > 359) {
|
||||
$hue = 0;
|
||||
return false;
|
||||
}
|
||||
if ($sat < 0 || $sat > 100) {
|
||||
$sat = 0;
|
||||
return false;
|
||||
}
|
||||
if ($lum < 0 || $lum > 100) {
|
||||
$lum = 0;
|
||||
return false;
|
||||
}
|
||||
$hue = (1 / 360) * $hue; // calc to internal convert value for hue
|
||||
// convert to internal 0-1 format
|
||||
@@ -276,7 +292,8 @@ class Colors
|
||||
$lum /= 100;
|
||||
// if saturation is 0
|
||||
if ($sat == 0) {
|
||||
return [$lum * 255, $lum * 255, $lum * 255];
|
||||
$lum = (int)round($lum * 255);
|
||||
return [$lum, $lum, $lum];
|
||||
} else {
|
||||
$m2 = $lum < 0.5 ? $lum * ($sat + 1) : ($lum + $sat) - ($lum * $sat);
|
||||
$m1 = $lum * 2 - $m2;
|
||||
|
||||
@@ -10,6 +10,9 @@ namespace CoreLibs\Convert;
|
||||
|
||||
class Html
|
||||
{
|
||||
public const SELECTED = 0;
|
||||
public const CHECKED = 1;
|
||||
|
||||
/**
|
||||
* full wrapper for html entities
|
||||
* @param mixed $string string to html encode
|
||||
@@ -32,7 +35,7 @@ class Html
|
||||
*/
|
||||
public static function removeLB(string $string, string $replace = ' '): string
|
||||
{
|
||||
return str_replace(["\r", "\n"], $replace, $string);
|
||||
return str_replace(["\n\r", "\r", "\n"], $replace, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,10 +47,10 @@ class Html
|
||||
* @param int $type type: 0: returns selected, 1, returns checked
|
||||
* @return ?string returns checked or selected, else returns null
|
||||
*/
|
||||
public static function checked($haystack, $needle, int $type = 0): ?string
|
||||
public static function checked($haystack, string $needle, int $type = 0): ?string
|
||||
{
|
||||
if (is_array($haystack)) {
|
||||
if (in_array((string)$needle, $haystack)) {
|
||||
if (in_array($needle, $haystack)) {
|
||||
return $type ? 'checked' : 'selected';
|
||||
}
|
||||
} else {
|
||||
|
||||
95
www/lib/CoreLibs/Convert/Json.php
Normal file
95
www/lib/CoreLibs/Convert/Json.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Converts a json string to array and stores error for later checking
|
||||
* can also return empty array on demand
|
||||
* and self set json as is on error as array
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
class Json
|
||||
{
|
||||
/** @var int */
|
||||
private static $json_last_error;
|
||||
|
||||
/**
|
||||
* converts a json string to an array
|
||||
* or inits an empty array on null string
|
||||
* or failed convert to array
|
||||
* In ANY case it will ALWAYS return array.
|
||||
* Does not throw errors
|
||||
* @param string|null $json a json string, or null data
|
||||
* @param bool $override if set to true, then on json error
|
||||
* set original value as array
|
||||
* @return array<mixed> returns an array from the json values
|
||||
*/
|
||||
public static function jsonConvertToArray(?string $json, bool $override = false): array
|
||||
{
|
||||
if ($json !== null) {
|
||||
$_json = json_decode($json, true);
|
||||
if (self::$json_last_error = json_last_error()) {
|
||||
if ($override == true) {
|
||||
// init return as array with original as element
|
||||
$json = [$json];
|
||||
} else {
|
||||
$json = [];
|
||||
}
|
||||
} else {
|
||||
$json = $_json;
|
||||
}
|
||||
} else {
|
||||
$json = [];
|
||||
}
|
||||
// be sure that we return an array
|
||||
return (array)$json;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns human readable string for json errors thrown in jsonConvertToArray
|
||||
* @param bool|boolean $return_string [default=false] if set to true
|
||||
* it will return the message string and not
|
||||
* the error number
|
||||
* @return int|string Either error number (0 for no error)
|
||||
* or error string ('' for no error)
|
||||
*/
|
||||
public static function jsonGetLastError(bool $return_string = false)
|
||||
{
|
||||
$json_error_string = '';
|
||||
// valid errors as of php 8.0
|
||||
switch (self::$json_last_error) {
|
||||
case JSON_ERROR_NONE:
|
||||
$json_error_string = '';
|
||||
break;
|
||||
case JSON_ERROR_DEPTH:
|
||||
$json_error_string = 'Maximum stack depth exceeded';
|
||||
break;
|
||||
case JSON_ERROR_STATE_MISMATCH:
|
||||
$json_error_string = 'Underflow or the modes mismatch';
|
||||
break;
|
||||
case JSON_ERROR_CTRL_CHAR:
|
||||
$json_error_string = 'Unexpected control character found';
|
||||
break;
|
||||
case JSON_ERROR_SYNTAX:
|
||||
$json_error_string = 'Syntax error, malformed JSON';
|
||||
break;
|
||||
case JSON_ERROR_UTF8:
|
||||
$json_error_string = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||
break;
|
||||
case JSON_ERROR_INVALID_PROPERTY_NAME:
|
||||
$json_error_string = 'A key starting with \u0000 character was in the string';
|
||||
break;
|
||||
case JSON_ERROR_UTF16:
|
||||
$json_error_string = 'Single unpaired UTF-16 surrogate in unicode escape';
|
||||
break;
|
||||
default:
|
||||
$json_error_string = 'Unknown error';
|
||||
break;
|
||||
}
|
||||
return $return_string === true ? $json_error_string : self::$json_last_error;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -61,6 +61,10 @@ class MimeAppName
|
||||
*/
|
||||
public static function mimeSetAppName(string $mime, string $app): void
|
||||
{
|
||||
// if empty, don't set
|
||||
if (empty($mime) || empty($app)) {
|
||||
return;
|
||||
}
|
||||
self::$mime_apps[$mime] = $app;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user