gemini-php/src/Client/Response.php
2024-06-24 23:27:15 +03:00

84 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Yggverse\Gemini\Client;
class Response
{
private ?int $_code = null;
private ?string $_meta = null;
private ?string $_body = null;
public function __construct(?string $data = null)
{
if ($data)
{
$match = [];
preg_match(
'/^(?<code>\d{2})(?<meta>.*)$/m',
$data,
$match
);
if (isset($match['code']))
{
$code = (int) $match['code'];
if ($code >= 10 && $code <= 69)
{
$this->setCode(
$code
);
}
}
if (isset($match['meta']) && mb_strlen($match['meta']) <= 1024)
{
$this->setMeta(
trim(
(string) $match['meta']
)
);
}
if ($body = substr($data, strpos($data, chr(10)) + 1))
{
$this->setBody(
(string) $body
);
}
}
}
public function setCode(?int $value): void
{
$this->_code = $value;
}
public function getCode(): ?int
{
return $this->_code;
}
public function setMeta(?string $value): void
{
$this->_meta = $value;
}
public function getMeta(): ?string
{
return $this->_meta;
}
public function setBody(?string $value): void
{
$this->_body = $value;
}
public function getBody(): ?string
{
return $this->_body;
}
}