-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResponseData.php
More file actions
89 lines (80 loc) · 2.25 KB
/
ResponseData.php
File metadata and controls
89 lines (80 loc) · 2.25 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace DemonDextralHorn\Data;
use Illuminate\Support\Arr;
use Spatie\LaravelData\Data;
use Symfony\Component\HttpFoundation\Response;
/**
* DTO representing HTTP response data.
*
* @class ResponseData
*/
final class ResponseData extends Data
{
/**
* Create a new ResponseData object.
*
* @param int $status
* @param HeadersData|null $headers
* @param string|bool|null $content
*/
public function __construct(
public int $status,
public ?HeadersData $headers = null,
public string|bool|null $content = null,
) {}
/**
* Create a new instance from a ResponseData object.
*
* @param Response $response
*
* @return static
*/
public static function fromResponse(Response $response): static
{
return new self(
status: $response->getStatusCode(),
headers: HeadersData::fromHeaders(
$response->headers->all(),
$response->headers->getCookies()
),
content: $response->getContent(),
);
}
/**
* Serialize the current object to an array.
*
* @return array
*/
public function __serialize(): array
{
return [
'status' => $this->status,
'headers' => $this->headers ? $this->headers->__serialize() : null,
'content' => $this->content,
];
}
/**
* Unserialize the data into the current object.
*
* @param array $data
*
* @return void
*/
public function __unserialize(array $data): void
{
$this->status = Arr::get($data, 'status');
// Unserialize HeadersData
$headersArray = Arr::get($data, 'headers');
$this->headers = $headersArray
? new HeadersData(
authorization: Arr::get($headersArray, 'authorization'),
accept: Arr::get($headersArray, 'accept'),
acceptLanguage: Arr::get($headersArray, 'acceptLanguage'),
prefetchHeader: Arr::get($headersArray, 'prefetchHeader'),
setCookie: Arr::get($headersArray, 'setCookie'),
)
: null;
$this->content = Arr::get($data, 'content');
}
}