| Current Path : /var/www/clients/client3/web2/web/vendor/m2epro/magento2-extension.disabled/Helper/ |
| Current File : /var/www/clients/client3/web2/web/vendor/m2epro/magento2-extension.disabled/Helper/Json.php |
<?php
/**
* @author M2E Pro Developers Team
* @copyright M2E LTD
* @license Commercial use is forbidden
*/
namespace Ess\M2ePro\Helper;
class Json
{
/**
* @param mixed $data
* @param bool $throwError
*
* @return string|null
* @throws \Ess\M2ePro\Model\Exception\Logic
*/
public static function encode($data, $throwError = true): ?string
{
if ($data === false) {
return 'false';
}
$encoded = json_encode($data);
if ($encoded !== false) {
return $encoded;
}
$encoded = json_encode(\Ess\M2ePro\Helper\Data::normalizeToUtf($data));
if ($encoded !== false) {
return $encoded;
}
if (!$throwError) {
return null;
}
throw new \Ess\M2ePro\Model\Exception\Logic(
'Unable to encode to JSON.',
['error' => json_last_error_msg()]
);
}
/**
* @param mixed $json
* @param bool $throwError
*
* @return array|bool|null
* @throws \Ess\M2ePro\Model\Exception\Logic
*/
public static function decode($json, $throwError = false)
{
if ($json === null || $json === '' || strtolower($json) === 'null') {
return null;
}
$decoded = json_decode($json, true);
if ($decoded !== null) {
return $decoded;
}
if ($throwError) {
throw new \Ess\M2ePro\Model\Exception\Logic(
'Unable to decode JSON.',
['source' => $json]
);
}
return null;
}
}