-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBody.php
More file actions
39 lines (29 loc) · 868 Bytes
/
Body.php
File metadata and controls
39 lines (29 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
declare(strict_types=1);
namespace TinyBlocks\Http\Internal\Request;
use Psr\Http\Message\ServerRequestInterface;
use TinyBlocks\Http\Internal\Stream\StreamFactory;
final readonly class Body
{
private function __construct(private array $data)
{
}
public static function from(ServerRequestInterface $request): Body
{
$body = $request->getBody();
$streamFactory = StreamFactory::fromStream(stream: $body);
if ($streamFactory->isEmptyContent()) {
return new Body(data: []);
}
return new Body(data: json_decode($streamFactory->content(), true));
}
public function get(string $key): Attribute
{
$value = ($this->data[$key] ?? null);
return Attribute::from(value: $value);
}
public function toArray(): array
{
return $this->data;
}
}