60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
<?php
|
|
/**
|
|
* @package Bicycle
|
|
* @author Egor Isaev
|
|
* @description Response.php
|
|
* @copyright (c) 03/06/2026
|
|
*/
|
|
|
|
namespace System\Classes;
|
|
|
|
/**
|
|
* HTTP-ответ приложения: содержит тело и статус-код.
|
|
*/
|
|
class Response
|
|
{
|
|
/** @var int HTTP-статус ответа */
|
|
private int $_status = 200;
|
|
/** @var string Тело ответа */
|
|
private string $_body = '';
|
|
|
|
/**
|
|
* @param int $status Начальный HTTP-статус
|
|
*/
|
|
public function __construct(int $status = 200)
|
|
{
|
|
$this->_status = $status;
|
|
}
|
|
|
|
/**
|
|
* Геттер/сеттер тела ответа.
|
|
*
|
|
* @param string|null $content Новое тело или null для чтения
|
|
* @return string|static Тело при чтении, $this при записи
|
|
*/
|
|
public function body(?string $content = null): string|static
|
|
{
|
|
if ($content === null) {
|
|
return $this->_body;
|
|
}
|
|
$this->_body = $content;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Геттер/сеттер статус-кода. При установке вызывает http_response_code().
|
|
*
|
|
* @param int|null $code Новый код или null для чтения
|
|
* @return int|static Код при чтении, $this при записи
|
|
*/
|
|
public function status(?int $code = null): int|static
|
|
{
|
|
if ($code === null) {
|
|
return $this->_status;
|
|
}
|
|
$this->_status = $code;
|
|
http_response_code($code);
|
|
return $this;
|
|
}
|
|
}
|