public function parse($payload)
{
if (function_exists('MongoDB\\BSON\\toPHP') && !function_exists('bson_decode')) {
require_once __DIR__ . '/BSONPolyfill.php';
//@codeCoverageIgnore
} elseif (!(function_exists('bson_decode') || function_exists('MongoDB\\BSON\\toPHP'))) {
throw new ParserException('Failed To Parse BSON - Supporting Library Not Available');
// @codeCoverageIgnore
}
if ($payload) {
$prevHandler = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
throw new \Exception($errstr);
// @codeCoverageIgnore
});
try {
$bson = bson_decode(trim($payload, " \t\n\r\v"));
// Don't trim \0, as it has valid meaning in BSON
if (!$bson) {
throw new \Exception('Unknown error');
// @codeCoverageIgnore
}
} catch (\Exception $e) {
set_error_handler($prevHandler);
throw new ParserException('Failed To Parse BSON - ' . $e->getMessage());
}
set_error_handler($prevHandler);
return $bson;
}
return [];
}