2025-06-28 23:22:37 +03:00

67 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace goodboyalex\php_components_pack\exceptions;
use Exception;
use goodboyalex\php_components_pack\enums\JsonErrorCode;
/**
* Ошибка работы с JSON.
*
* @author Александр Бабаев
* @package php_components_pack
* @version 1.0
* @since 1.1.0
*/
final class JsonException extends Exception
{
/**
* @var string|null $JsonString Строка JSON.
*/
public ?string $JsonString;
/**
* @var JsonErrorCode $ErrorCode Код ошибки JSON.
*/
public JsonErrorCode $ErrorCode;
/**
* @link https://www.php.net/manual/ru/function.json-last-error-msg.php
* @var string|null $ErrorMessage Сообщение об ошибке JSON.
*
* Внимание! В отличие от функции json_last_error_msg(), данная переменная при отсутствии ошибок выводит null, а не
* "No error".
*/
public ?string $ErrorMessage;
/**
* Конструктор.
*
* @param string|null $json Строка JSON.
* @param JsonErrorCode $errorCode Код ошибки JSON.
* @param string|null $errorMessage Сообщение об ошибке JSON.
*/
public function __construct (?string $json = null, JsonErrorCode $errorCode = JsonErrorCode::Unknown,
?string $errorMessage = null)
{
// Если код ошибки JSON равен 0
if ($errorMessage === "No error")
// - то присваиваем ему null для совместимости
$errorMessage = null;
// Сохраняем сообщение об ошибке
$this->ErrorMessage = $errorMessage;
// Если сообщение пусто, то присваиваем ему "" для совместимости
$errorMessage = $errorMessage ?? "";
// Запускаем базовый конструктор
parent::__construct($errorMessage);
// Присваиваем имя файла
$this->JsonString = $json;
// Присваиваем код ошибки
$this->ErrorCode = $errorCode;
}
}