JSON Array to convert fix, bool/false return fix

This commit is contained in:
2023-08-02 16:34:57 +09:00
parent daf2706e7e
commit 53c7dda9a0
6 changed files with 112 additions and 39 deletions

View File

@@ -16,7 +16,7 @@
], ],
"minimum-stability": "dev", "minimum-stability": "dev",
"require": { "require": {
"php": ">=8.1", "php": ">=8.2",
"psr/log": "^3.0@dev" "psr/log": "^3.0@dev"
}, },
"require-dev": { "require-dev": {

View File

@@ -169,10 +169,10 @@ class Email
* @param string $email email string * @param string $email email string
* @param bool $short default false, if true, * @param bool $short default false, if true,
* returns only short type (pc instead of pc_html) * 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 * 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 // trip if there is no email address
if (!$email) { if (!$email) {
@@ -200,9 +200,9 @@ class Email
* gets the short email type from a long email type * gets the short email type from a long email type
* *
* @param string $email_type email string * @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 // check if the short email type exists
if (isset(self::$mobile_email_type_short[$email_type])) { if (isset(self::$mobile_email_type_short[$email_type])) {

View File

@@ -28,7 +28,7 @@ class Colors
* @param int $green green 0-255 * @param int $green green 0-255
* @param int $blue blue 0-255 * @param int $blue blue 0-255
* @param bool $hex_prefix default true, prefix with "#" * @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 * false for invalid color
*/ */
public static function rgb2hex( public static function rgb2hex(
@@ -36,7 +36,7 @@ class Colors
int $green, int $green,
int $blue, int $blue,
bool $hex_prefix = true bool $hex_prefix = true
): string|bool { ): string|false {
$hex_color = ''; $hex_color = '';
if ($hex_prefix === true) { if ($hex_prefix === true) {
$hex_color = '#'; $hex_color = '#';
@@ -58,14 +58,14 @@ class Colors
* @param string $hexStr RGB hexstring * @param string $hexStr RGB hexstring
* @param bool $return_as_string flag to return as string * @param bool $return_as_string flag to return as string
* @param string $seperator string seperator: default: "," * @param string $seperator string seperator: default: ","
* @return string|array<string,float|int>|bool false on error or array with RGB * @return string|array<string,float|int>|false false on error or array with RGB
* or a string with the seperator * or a string with the seperator
*/ */
public static function hex2rgb( public static function hex2rgb(
string $hexStr, string $hexStr,
bool $return_as_string = false, bool $return_as_string = false,
string $seperator = ',' string $seperator = ','
): string|array|bool { ): string|array|false {
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
if (!is_string($hexStr)) { if (!is_string($hexStr)) {
return false; return false;
@@ -97,13 +97,13 @@ class Colors
* returns: * returns:
* array with hue (0-360), sat (0-100%), brightness/value (0-100%) * array with hue (0-360), sat (0-100%), brightness/value (0-100%)
* *
* @param int $red red 0-255 * @param int $red red 0-255
* @param int $green green 0-255 * @param int $green green 0-255
* @param int $blue blue 0-255 * @param int $blue blue 0-255
* @return array<int|float>|bool Hue, Sat, Brightness/Value * @return array<int|float>|false Hue, Sat, Brightness/Value
* false for input value error * 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 // check that rgb is from 0 to 255
foreach (['red', 'green', 'blue'] as $c) { foreach (['red', 'green', 'blue'] as $c) {
@@ -144,13 +144,13 @@ class Colors
* converts HSB/V to RGB values RGB is full INT * converts HSB/V to RGB values RGB is full INT
* if HSB/V value is invalid, sets this value to 0 * if HSB/V value is invalid, sets this value to 0
* *
* @param float $H hue 0-360 (int) * @param float $H hue 0-360 (int)
* @param float $S saturation 0-100 (int) * @param float $S saturation 0-100 (int)
* @param float $V brightness/value 0-100 (int) * @param float $V brightness/value 0-100 (int)
* @return array<int>|bool 0 red/1 green/2 blue array as 0-255 * @return array<int>|false 0 red/1 green/2 blue array as 0-255
* false for input value error * 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 // check that H is 0 to 359, 360 = 0
// and S and V are 0 to 1 // and S and V are 0 to 1
@@ -230,13 +230,13 @@ class Colors
* return: * return:
* array with hue (0-360), saturation (0-100%) and luminance (0-100%) * array with hue (0-360), saturation (0-100%) and luminance (0-100%)
* *
* @param int $red red 0-255 * @param int $red red 0-255
* @param int $green green 0-255 * @param int $green green 0-255
* @param int $blue blue 0-255 * @param int $blue blue 0-255
* @return array<float>|bool hue/sat/luminance * @return array<float>|false hue/sat/luminance
* false for input value error * 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 // check that rgb is from 0 to 255
foreach (['red', 'green', 'blue'] as $c) { foreach (['red', 'green', 'blue'] as $c) {
@@ -284,12 +284,12 @@ class Colors
* converts an HSL to RGB * converts an HSL to RGB
* if HSL value is invalid, set this value to 0 * if HSL value is invalid, set this value to 0
* *
* @param float $hue hue: 0-360 (degrees) * @param float $hue hue: 0-360 (degrees)
* @param float $sat saturation: 0-100 * @param float $sat saturation: 0-100
* @param float $lum luminance: 0-100 * @param float $lum luminance: 0-100
* @return array<int,float|int>|bool red/blue/green 0-255 each * @return array<int,float|int>|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) { if ($hue == 360) {
$hue = 0; $hue = 0;

View File

@@ -48,8 +48,26 @@ class Json
return (array)$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<mixed> $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 * 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 * @param bool $return_string [default=false] if set to true
* it will return the message string and not * it will return the message string and not
@@ -80,6 +98,15 @@ class Json
case JSON_ERROR_UTF8: case JSON_ERROR_UTF8:
$json_error_string = 'Malformed UTF-8 characters, possibly incorrectly encoded'; $json_error_string = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break; 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: case JSON_ERROR_INVALID_PROPERTY_NAME:
$json_error_string = 'A key starting with \u0000 character was in the string'; $json_error_string = 'A key starting with \u0000 character was in the string';
break; break;

View File

@@ -2779,7 +2779,7 @@ class IO
* @param string $query Query to find in cursor_ext * @param string $query Query to find in cursor_ext
* @param array<mixed> $params If the query is params type we need params * @param array<mixed> $params If the query is params type we need params
* data to create a unique call one, optional * 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 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 * Either a single element for a single insert or an array
* if multiple insert values where used. * if multiple insert values where used.
* *
* @return array<mixed>|string|int|null Current insert query primary key * @return array<mixed>|string|int|null Current insert query primary key, null on not set
*/ */
public function dbGetInsertPK(): array|string|int|null public function dbGetInsertPK(): array|string|int|null
{ {

View File

@@ -16,7 +16,7 @@ final class CoreLibsConvertJsonTest extends TestCase
/** /**
* test list for json convert tests * test list for json convert tests
* *
* @return array * @return array<mixed>
*/ */
public function jsonProvider(): array public function jsonProvider(): array
{ {
@@ -54,10 +54,36 @@ final class CoreLibsConvertJsonTest extends TestCase
]; ];
} }
/**
* Undocumented function
*
* @return array<mixed>
*/
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 * json error list
* *
* @return array JSON error list * @return array<mixed> JSON error list
*/ */
public function jsonErrorProvider(): array public function jsonErrorProvider(): array
{ {
@@ -127,7 +153,7 @@ final class CoreLibsConvertJsonTest extends TestCase
* *
* @param string|null $input * @param string|null $input
* @param bool $flag * @param bool $flag
* @param array $expected * @param array<mixed> $expected
* @return void * @return void
*/ */
public function testJsonConvertToArray(?string $input, bool $flag, array $expected): 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] * @testdox jsonGetLastError $input will be $expected_i/$expected_s [$_dataName]
* *
* @param string|null $input * @param string|null $input
* @param string $expected * @param int $expected_i
* @param string $expected_s
* @return void * @return void
*/ */
public function testJsonGetLastError(?string $input, int $expected_i, string $expected_s): 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) \CoreLibs\Convert\Json::jsonGetLastError(true)
); );
} }
/**
* Undocumented function
*
* @covers ::jsonConvertArrayTo
* @dataProvider jsonArrayProvider
* @testdox jsonConvertArrayTo $input (Override: $flag) will be $expected [$_dataName]
*
* @param array<mixed> $input
* @param string $expected
* @return void
*/
public function testJsonConvertArrayto(array $input, string $expected): void
{
$this->assertEquals(
$expected,
\CoreLibs\Convert\Json::jsonConvertArrayTo($input)
);
}
} }
// __END__ // __END__