Bicycle/System/Classes/HTTP/Client/Response.php
Egor Isaev b65603b4d0 dev
2026-06-23 10:44:18 +03:00

65 lines
1.4 KiB
PHP

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description HTTP/Client/Response.php
* @copyright (c) 04/06/2026
*/
namespace System\Classes\HTTP\Client;
use JsonException;
/**
* Ответ внешнего HTTP-запроса.
*/
class Response
{
/**
* @param int $status HTTP-статус ответа
* @param string $body Тело ответа
*/
public function __construct(
private readonly int $status,
private readonly string $body
) {}
/**
* @return int HTTP-статус
*/
public function status(): int
{
return $this->status;
}
/**
* @return string Тело ответа
*/
public function body(): string
{
return $this->body;
}
/**
* @return bool true, если статус в диапазоне 2xx
*/
public function isSuccess(): bool
{
return $this->status >= 200 && $this->status < 300;
}
/**
* Декодирует тело ответа как JSON.
*
* @return array Пустой массив, если тело пустое
* @throws JsonException При некорректном JSON
*/
public function json(): array
{
if ($this->body === '') {
return [];
}
return json_decode($this->body, true, 512, JSON_THROW_ON_ERROR);
}
}