-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewResponse.php
58 lines (45 loc) · 1.16 KB
/
ViewResponse.php
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
<?php
declare(strict_types=1);
namespace Snicco\Component\HttpRouting\Http\Response;
use Psr\Http\Message\ResponseInterface;
use Snicco\Component\HttpRouting\Http\Psr7\Response;
use function array_replace;
final class ViewResponse extends Response
{
private string $view;
/**
* @var array<string,mixed>
*/
private array $view_data = [];
public function __construct(string $view, ResponseInterface $response)
{
$this->view = $view;
parent::__construct($response->withHeader('content-type', 'text/html; charset=UTF-8'));
}
public function view(): string
{
return $this->view;
}
public function withView(string $view): ViewResponse
{
$new = clone $this;
$new->view = $view;
return $new;
}
/**
* @return array<string,mixed>
*/
public function viewData(): array
{
return $this->view_data;
}
/**
* @param array<string,mixed> $data
*/
public function withViewData(array $data): ViewResponse
{
$new = clone $this;
$new->view_data = array_replace($this->view_data, $data);
return $new;
}
}