diff --git a/composer.json b/composer.json index b688dfd..334effc 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ ], "minimum-stability": "dev", "require": { - "php": ">=8.1", + "php": ">=8.2", "psr/log": "^3.0@dev" }, "require-dev": { diff --git a/src/Check/Email.php b/src/Check/Email.php index 6e78bee..455a7d2 100644 --- a/src/Check/Email.php +++ b/src/Check/Email.php @@ -169,10 +169,10 @@ class Email * @param string $email email string * @param bool $short default false, if true, * returns only short type (pc instead of pc_html) - * @return string|bool email type, eg "pc", "docomo", etc, + * @return string|false email type, eg "pc", "docomo", etc, * false for invalid short type */ - public static function getEmailType(string $email, bool $short = false) + public static function getEmailType(string $email, bool $short = false): string|false { // trip if there is no email address if (!$email) { @@ -200,9 +200,9 @@ class Email * gets the short email type from a long email type * * @param string $email_type email string - * @return string|bool short string or false for invalid + * @return string|false short string or false for invalid */ - public static function getShortEmailType(string $email_type) + public static function getShortEmailType(string $email_type): string|false { // check if the short email type exists if (isset(self::$mobile_email_type_short[$email_type])) { diff --git a/src/Convert/Colors.php b/src/Convert/Colors.php index b88c6d3..1c6f4b4 100644 --- a/src/Convert/Colors.php +++ b/src/Convert/Colors.php @@ -28,7 +28,7 @@ class Colors * @param int $green green 0-255 * @param int $blue blue 0-255 * @param bool $hex_prefix default true, prefix with "#" - * @return string|bool rgb in hex values with leading # if set, + * @return string|false rgb in hex values with leading # if set, * false for invalid color */ public static function rgb2hex( @@ -36,7 +36,7 @@ class Colors int $green, int $blue, bool $hex_prefix = true - ): string|bool { + ): string|false { $hex_color = ''; if ($hex_prefix === true) { $hex_color = '#'; @@ -58,14 +58,14 @@ class Colors * @param string $hexStr RGB hexstring * @param bool $return_as_string flag to return as string * @param string $seperator string seperator: default: "," - * @return string|array|bool false on error or array with RGB - * or a string with the seperator + * @return string|array|false 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 = ',' - ): string|array|bool { + ): string|array|false { $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string if (!is_string($hexStr)) { return false; @@ -97,13 +97,13 @@ class Colors * 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|bool Hue, Sat, Brightness/Value - * false for input value error + * @param int $red red 0-255 + * @param int $green green 0-255 + * @param int $blue blue 0-255 + * @return array|false Hue, Sat, Brightness/Value + * false for input value error */ - public static function rgb2hsb(int $red, int $green, int $blue): array|bool + public static function rgb2hsb(int $red, int $green, int $blue): array|false { // check that rgb is from 0 to 255 foreach (['red', 'green', 'blue'] as $c) { @@ -144,13 +144,13 @@ class Colors * converts HSB/V to RGB values RGB is full INT * if HSB/V value is invalid, sets this value to 0 * - * @param float $H hue 0-360 (int) - * @param float $S saturation 0-100 (int) - * @param float $V brightness/value 0-100 (int) - * @return array|bool 0 red/1 green/2 blue array as 0-255 - * false for input value error + * @param float $H hue 0-360 (int) + * @param float $S saturation 0-100 (int) + * @param float $V brightness/value 0-100 (int) + * @return array|false 0 red/1 green/2 blue array as 0-255 + * false for input value error */ - public static function hsb2rgb(float $H, float $S, float $V): array|bool + public static function hsb2rgb(float $H, float $S, float $V): array|false { // check that H is 0 to 359, 360 = 0 // and S and V are 0 to 1 @@ -230,13 +230,13 @@ class Colors * 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|bool hue/sat/luminance - * false for input value error + * @param int $red red 0-255 + * @param int $green green 0-255 + * @param int $blue blue 0-255 + * @return array|false hue/sat/luminance + * false for input value error */ - public static function rgb2hsl(int $red, int $green, int $blue): array|bool + public static function rgb2hsl(int $red, int $green, int $blue): array|false { // check that rgb is from 0 to 255 foreach (['red', 'green', 'blue'] as $c) { @@ -284,12 +284,12 @@ class Colors * converts an HSL to RGB * if HSL value is invalid, set this value to 0 * - * @param float $hue hue: 0-360 (degrees) - * @param float $sat saturation: 0-100 - * @param float $lum luminance: 0-100 - * @return array|bool red/blue/green 0-255 each + * @param float $hue hue: 0-360 (degrees) + * @param float $sat saturation: 0-100 + * @param float $lum luminance: 0-100 + * @return array|false red/blue/green 0-255 each */ - public static function hsl2rgb(float $hue, float $sat, float $lum): array|bool + public static function hsl2rgb(float $hue, float $sat, float $lum): array|false { if ($hue == 360) { $hue = 0; diff --git a/src/Convert/Json.php b/src/Convert/Json.php index 9686715..7f885df 100644 --- a/src/Convert/Json.php +++ b/src/Convert/Json.php @@ -48,8 +48,26 @@ class Json return (array)$json; } + /** + * convert array to json + * Will set empty json {} on false/error + * Error can be read with jsonGetLastError + * Deos not throw errors + * + * @param array $data + * @param int $flags json_encode flags as is + * @return string JSON string or '{}' if false + */ + public static function jsonConvertArrayTo(array $data, int $flags = 0): string + { + $json_string = json_encode($data, $flags) ?: '{}'; + self::$json_last_error = json_last_error(); + return (string)$json_string; + } + /** * returns human readable string for json errors thrown in jsonConvertToArray + * Source: https://www.php.net/manual/en/function.json-last-error.php * * @param bool $return_string [default=false] if set to true * it will return the message string and not @@ -80,6 +98,15 @@ class Json case JSON_ERROR_UTF8: $json_error_string = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; + case JSON_ERROR_RECURSION: + $json_error_string = 'One or more recursive references in the value to be encoded'; + break; + case JSON_ERROR_INF_OR_NAN: + $json_error_string = 'One or more NAN or INF values in the value to be encoded'; + break; + case JSON_ERROR_UNSUPPORTED_TYPE: + $json_error_string = ' A value of a type that cannot be encoded was given'; + break; case JSON_ERROR_INVALID_PROPERTY_NAME: $json_error_string = 'A key starting with \u0000 character was in the string'; break; diff --git a/src/DB/IO.php b/src/DB/IO.php index eb70690..c92f80f 100644 --- a/src/DB/IO.php +++ b/src/DB/IO.php @@ -2779,7 +2779,7 @@ class IO * @param string $query Query to find in cursor_ext * @param array $params If the query is params type we need params * data to create a unique call one, optional - * @return int|false query position (row pos), false on error + * @return int|false numer of rows returned, false on error */ public function dbGetCursorNumRows(string $query, array $params = []): int|false { @@ -3736,7 +3736,7 @@ class IO * Either a single element for a single insert or an array * if multiple insert values where used. * - * @return array|string|int|null Current insert query primary key + * @return array|string|int|null Current insert query primary key, null on not set */ public function dbGetInsertPK(): array|string|int|null { diff --git a/test/phpunit/Convert/CoreLibsConvertJsonTest.php b/test/phpunit/Convert/CoreLibsConvertJsonTest.php index af6e7e3..74e7ec7 100644 --- a/test/phpunit/Convert/CoreLibsConvertJsonTest.php +++ b/test/phpunit/Convert/CoreLibsConvertJsonTest.php @@ -16,7 +16,7 @@ final class CoreLibsConvertJsonTest extends TestCase /** * test list for json convert tests * - * @return array + * @return array */ public function jsonProvider(): array { @@ -54,10 +54,36 @@ final class CoreLibsConvertJsonTest extends TestCase ]; } + /** + * Undocumented function + * + * @return array + */ + public function jsonArrayProvider(): array + { + return [ + 'valid json' => [ + [ + 'm' => 2, + 'f' => 'sub_2' + ], + '{"m":2,"f":"sub_2"}', + ], + 'empty json array' => [ + [], + '[]' + ], + 'empty json hash' => [ + ['' => ''], + '{"":""}' + ] + ]; + } + /** * json error list * - * @return array JSON error list + * @return array JSON error list */ public function jsonErrorProvider(): array { @@ -127,7 +153,7 @@ final class CoreLibsConvertJsonTest extends TestCase * * @param string|null $input * @param bool $flag - * @param array $expected + * @param array $expected * @return void */ public function testJsonConvertToArray(?string $input, bool $flag, array $expected): void @@ -146,7 +172,8 @@ final class CoreLibsConvertJsonTest extends TestCase * @testdox jsonGetLastError $input will be $expected_i/$expected_s [$_dataName] * * @param string|null $input - * @param string $expected + * @param int $expected_i + * @param string $expected_s * @return void */ public function testJsonGetLastError(?string $input, int $expected_i, string $expected_s): void @@ -161,6 +188,25 @@ final class CoreLibsConvertJsonTest extends TestCase \CoreLibs\Convert\Json::jsonGetLastError(true) ); } + + /** + * Undocumented function + * + * @covers ::jsonConvertArrayTo + * @dataProvider jsonArrayProvider + * @testdox jsonConvertArrayTo $input (Override: $flag) will be $expected [$_dataName] + * + * @param array $input + * @param string $expected + * @return void + */ + public function testJsonConvertArrayto(array $input, string $expected): void + { + $this->assertEquals( + $expected, + \CoreLibs\Convert\Json::jsonConvertArrayTo($input) + ); + } } // __END__