Add nullable return types and improve exception handling in MotorAPI
- Updated `getCachedResult` methods across `motorapi`, `motorapi_i`, and `motorapi_lookups_o` to return `?object` for nullable results. - Enhanced error handling in `xlvask_parser_stor_bil` by wrapping `getLicensePlateInformation` in a try-catch block to handle API exceptions gracefully. - Introduced `cleanJSON` method in `motorapi` to sanitize and validate JSON data, ensuring robust parsing of cached responses. - Optimized `getCachedResult` logic to handle null and invalid JSON scenarios with detailed error reporting.
This commit is contained in:
@@ -49,6 +49,23 @@ class motorapi implements motorapi_i
|
||||
$this->license_plate_lookup = new license_plate_lookup_a();
|
||||
}
|
||||
|
||||
private static function cleanJSON(mixed $value): string
|
||||
{
|
||||
if (is_string($value)) {
|
||||
// Remove backslashes
|
||||
$value = stripslashes($value);
|
||||
$value = stripslashes($value);
|
||||
// Ensure the string is valid UTF-8
|
||||
$value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
|
||||
// Optionally, you can use regex to remove any non-printable characters
|
||||
$value = preg_replace('/[^\x20-\x7E]/', '', $value);
|
||||
} else {
|
||||
echo 'Unable to perform cleaning on value of type ' . gettype($value) . ': ' . print_r($value, true) . PHP_EOL;
|
||||
}
|
||||
// Remove any invalid UTF-8 characters
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the recommended products for a license plate
|
||||
* @param string $plate The license plate
|
||||
@@ -109,7 +126,10 @@ class motorapi implements motorapi_i
|
||||
// Check if the license plate information is cached
|
||||
if (!$ignoreCache && self::isCached($licensePlate)) {
|
||||
// Get the cached result
|
||||
return self::getCachedResult($licensePlate);
|
||||
$tmp_res = self::getCachedResult($licensePlate);
|
||||
if ($tmp_res !== null) {
|
||||
return $tmp_res;
|
||||
}
|
||||
}
|
||||
// Get the license plate information from the motorapi
|
||||
return $this->sendRequest($licensePlate, 'vehicles', [], 'GET');
|
||||
@@ -128,7 +148,7 @@ class motorapi implements motorapi_i
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function getCachedResult(string $licensePlate): object
|
||||
function getCachedResult(string $licensePlate): ?object
|
||||
{
|
||||
global /** @var response $response */
|
||||
$response;
|
||||
@@ -136,7 +156,13 @@ class motorapi implements motorapi_i
|
||||
$motorapi_lookups = new motorapi_lookups_o();
|
||||
// Add the cached value to the meta
|
||||
$response->add_meta('cached', true);
|
||||
return json_decode($motorapi_lookups->getCachedResult($licensePlate)->result->value());
|
||||
$cleaned_result = self::cleanJSON($motorapi_lookups->getCachedResult($licensePlate)->result->value());
|
||||
$object = json_decode($cleaned_result);
|
||||
if ($object === null) {
|
||||
print_r($cleaned_result);
|
||||
throw new Exception('Cached result is not valid JSON');
|
||||
}
|
||||
return json_decode($cleaned_result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user