Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 5 additions & 44 deletions src/JsonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,12 @@
*/
trait JsonTrait
{
/**
* @return mixed
*/
private static function decodeJson(string $json)
private static function decodeJson(string $json): string|int|bool|\stdClass
{
$result = json_decode($json, false);
$errorCode = json_last_error();

if ($errorCode === JSON_ERROR_NONE) {
return $result;
}

// Generate error message
switch ($errorCode) {
case JSON_ERROR_DEPTH:
$error = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = 'Invalid or malformed JSON';
break;
case JSON_ERROR_CTRL_CHAR:
$error = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = 'Syntax error';
break;
case JSON_ERROR_UTF8:
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
case JSON_ERROR_UTF16:
$error = 'Malformed UTF-16 characters, possibly incorrectly encoded';
break;
default:
$error = 'Unknown error';
// Find the const name
$constants = get_defined_constants(true);
foreach ($constants['json'] as $name => $value) {
if (!strncmp($name, 'JSON_ERROR_', 11) && $value === $errorCode) {
$error = $name;
break;
}
}
break;
try {
return json_decode($json, false, flags: JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new Exception\UnexpectedValueException("Problem decoding JSON : {$e->getMessage()} : '{$json}'");
}

throw new Exception\UnexpectedValueException("Problem decoding JSON : {$error} : '{$json}'");
}
}
4 changes: 1 addition & 3 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ public function __construct(

/**
* Call service method
*
* @return mixed
*/
public function __call(string $name, array $params)
public function __call(string $name, array $params): string|int|bool|\stdClass
{
$data = [
'method' => $name,
Expand Down