Bicycle/System/Classes/HTTP/Client/Request.php
Egor Isaev b516ca07dc Initial commit: Bicycle PHP MVC micro-framework
Core MVC, HTTP client, Session, Cookie, Config, HTTPException — 111 PHPUnit tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 10:04:19 +03:00

67 lines
1.6 KiB
PHP

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description HTTP/Client/Request.php
* @copyright (c) 04/06/2026
*/
namespace System\Classes\HTTP\Client;
class Request
{
private string $method = 'GET';
private string $url;
private array $headers = [];
private ?string $body = null;
private int $timeout = 30;
public function __construct(string $url)
{
$this->url = $url;
}
public static function factory(string $url): static
{
return new static($url);
}
public function method(string $method): static
{
$this->method = strtoupper($method);
return $this;
}
public function header(string $name, string $value): static
{
$this->headers[$name] = $value;
return $this;
}
public function body(string $body): static
{
$this->body = $body;
return $this;
}
public function json(array $data): static
{
$this->header('Content-Type', 'application/json');
$this->header('Accept', 'application/json');
$this->body = json_encode($data, JSON_UNESCAPED_UNICODE);
return $this;
}
public function timeout(int $seconds): static
{
$this->timeout = $seconds;
return $this;
}
public function getMethod(): string { return $this->method; }
public function getUrl(): string { return $this->url; }
public function getHeaders(): array { return $this->headers; }
public function getBody(): ?string { return $this->body; }
public function getTimeout(): int { return $this->timeout; }
}