Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b3ef52c5d | ||
|
|
f2271313fc | ||
|
|
a7f81766bf | ||
|
|
7a4c905ae3 | ||
|
|
0531b29749 |
@@ -1 +1 @@
|
||||
9.38.1
|
||||
9.40.0
|
||||
|
||||
@@ -27,10 +27,14 @@ class Json
|
||||
* 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
|
||||
public static function jsonConvertToArray(?string $json, bool $override = false, int $flags = 0): array
|
||||
{
|
||||
if ($json !== null) {
|
||||
$_json = json_decode($json, true);
|
||||
// if flags has JSON_THROW_ON_ERROR remove it
|
||||
if ($flags & JSON_THROW_ON_ERROR) {
|
||||
$flags = $flags & ~JSON_THROW_ON_ERROR;
|
||||
}
|
||||
$_json = json_decode($json, true, flags:$flags);
|
||||
if (self::$json_last_error = json_last_error()) {
|
||||
if ($override == true) {
|
||||
// init return as array with original as element
|
||||
@@ -65,6 +69,21 @@ class Json
|
||||
return (string)$json_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate if a json string could be decoded.
|
||||
* Weill set the internval last error state and info can be read with jsonGetLastError
|
||||
*
|
||||
* @param string $json
|
||||
* @param int $flags only JSON_INVALID_UTF8_IGNORE is currently allowed
|
||||
* @return bool
|
||||
*/
|
||||
public static function jsonValidate(string $json, int $flags = 0): bool
|
||||
{
|
||||
$json_valid = json_validate($json, flags:$flags);
|
||||
self::$json_last_error = json_last_error();
|
||||
return $json_valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns human readable string for json errors thrown in jsonConvertToArray
|
||||
* Source: https://www.php.net/manual/en/function.json-last-error.php
|
||||
|
||||
@@ -49,7 +49,7 @@ class RandomKey
|
||||
private static function validateRandomKeyData(array ...$key_range): string
|
||||
{
|
||||
$key_character_range = Strings::buildCharStringFromLists(...$key_range);
|
||||
if (strlen(self::$key_character_range) <= 1) {
|
||||
if (strlen($key_character_range) <= 1) {
|
||||
return '';
|
||||
}
|
||||
return $key_character_range;
|
||||
|
||||
@@ -164,6 +164,51 @@ final class CoreLibsConvertJsonTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* test with flags
|
||||
*
|
||||
* @covers ::jsonConvertToArray
|
||||
* @testdox jsonConvertToArray flag test, if flag is used
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testJsonConvertToArrayWithFlags(): void
|
||||
{
|
||||
$input = '{"valid":"json","invalid":"\xB1\x31"}';
|
||||
/* $expected_without_flag = [
|
||||
'valid' => 'json'
|
||||
];
|
||||
$expected_with_flag = [
|
||||
'valid' => 'json',
|
||||
'invalid' => "\xB1\x31"
|
||||
]; */
|
||||
// no idea why in both it throws an erro
|
||||
$expected_without_flag = [];
|
||||
$expected_with_flag = [];
|
||||
$this->assertEquals(
|
||||
$expected_without_flag,
|
||||
\CoreLibs\Convert\Json::jsonConvertToArray($input)
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected_with_flag,
|
||||
\CoreLibs\Convert\Json::jsonConvertToArray($input, flags:JSON_INVALID_UTF8_IGNORE)
|
||||
);
|
||||
}
|
||||
|
||||
public function testJsonConvertToArrayRemoveThrowFlag(): void
|
||||
{
|
||||
$input = '{"valid":"json","invalid":"\xB1\x31"}';
|
||||
// show NOT throw an exception
|
||||
try {
|
||||
$this->assertEquals(
|
||||
[],
|
||||
\CoreLibs\Convert\Json::jsonConvertToArray($input, flags:JSON_THROW_ON_ERROR)
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->fail('Exception was thrown despite flag removal');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test json error states
|
||||
*
|
||||
@@ -189,6 +234,49 @@ final class CoreLibsConvertJsonTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* test json error states
|
||||
*
|
||||
* @covers ::jsonValidate
|
||||
* @dataProvider jsonErrorProvider
|
||||
* @testdox jsonValidate $input will be $expected_i/$expected_s [$_dataName]
|
||||
*
|
||||
* @param string|null $input
|
||||
* @param int $expected_i
|
||||
* @param string $expected_s
|
||||
* @return void
|
||||
*/
|
||||
public function testJsonValidateGetLastError(?string $input, int $expected_i, string $expected_s): void
|
||||
{
|
||||
\CoreLibs\Convert\Json::jsonValidate($input);
|
||||
$this->assertEquals(
|
||||
$expected_i,
|
||||
\CoreLibs\Convert\Json::jsonGetLastError()
|
||||
);
|
||||
$this->assertEquals(
|
||||
$expected_s,
|
||||
\CoreLibs\Convert\Json::jsonGetLastError(true)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* test json validation
|
||||
*
|
||||
* @covers ::jsonValidate
|
||||
* @testdox jsonValidate test valid and invalid json
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testJsonValidate(): void
|
||||
{
|
||||
$this->assertTrue(
|
||||
\CoreLibs\Convert\Json::jsonValidate('{"valid": "json"}')
|
||||
);
|
||||
$this->assertFalse(
|
||||
\CoreLibs\Convert\Json::jsonValidate('not valid json')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user