Browse Source

allow nullable response init

main
yggverse 8 months ago
parent
commit
c38cdc841f
  1. 55
      src/Client/Response.php

55
src/Client/Response.php

@ -10,40 +10,43 @@ class Response
private ?string $_meta = null; private ?string $_meta = null;
private ?string $_body = null; private ?string $_body = null;
public function __construct(string $data) public function __construct(?string $data = null)
{ {
$match = []; if ($data)
{
$match = [];
preg_match( preg_match(
'/(?<status>\d{2})\s(?<meta>.*)\r\n(?<body>.*)/su', '/(?<status>\d{2})\s(?<meta>.*)\r\n(?<body>.*)/su',
$data, $data,
$match $match
); );
if (isset($match['code'])) if (isset($match['code']))
{ {
$code = (int) $match['code']; $code = (int) $match['code'];
if ($code >= 10 && $code <= 69) if ($code >= 10 && $code <= 69)
{
$this->setCode(
$code
);
}
}
if (isset($match['meta']) && mb_strlen($match['meta']) <= 1024)
{ {
$this->setCode( $this->setMeta(
$code (string) $match['meta']
); );
} }
}
if (isset($match['meta']) && mb_strlen($match['meta']) <= 1024)
{
$this->setMeta(
(string) $match['meta']
);
}
if (isset($match['body'])) if (isset($match['body']))
{ {
$this->setBody( $this->setBody(
(string) (string) $match['body'] (string) (string) $match['body']
); );
}
} }
} }

Loading…
Cancel
Save