1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Controllers\Response; |
4
|
|
|
|
5
|
|
|
use Nip\Controllers\Controller; |
6
|
|
|
use Nip\Controllers\Traits\HasResponseTrait; |
7
|
|
|
use Nip\Controllers\Traits\HasViewTrait; |
8
|
|
|
use Nip\Controllers\View\ControllerViewHydrator; |
9
|
|
|
use Nip\Http\Response\Response; |
10
|
|
|
use Nip\Utility\Str; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ResponsePayloadTransformer |
14
|
|
|
* @package Nip\Controllers\Response |
15
|
|
|
*/ |
16
|
|
|
class ResponsePayloadTransformer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Controller|HasViewTrait |
20
|
|
|
*/ |
21
|
|
|
protected $controller; |
22
|
|
|
|
23
|
|
|
protected $payload; |
24
|
|
|
protected $factory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Controller|HasResponseTrait $controller |
28
|
|
|
* @param ResponsePayload $payload |
29
|
|
|
* @return \Nip\Http\Response\Response |
30
|
|
|
*/ |
31
|
1 |
|
public static function make($controller, ResponsePayload $payload) |
32
|
|
|
{ |
33
|
1 |
|
$transformer = new static($controller, $payload); |
34
|
|
|
|
35
|
1 |
|
return $transformer->toResponse(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* ResponsePayloadTransformer constructor. |
40
|
|
|
* @param Controller $controller |
41
|
|
|
* @param ResponsePayload $payload |
42
|
|
|
*/ |
43
|
1 |
|
protected function __construct($controller, ResponsePayload $payload) |
44
|
|
|
{ |
45
|
1 |
|
$this->controller = $controller; |
46
|
1 |
|
$this->payload = $payload; |
47
|
|
|
|
48
|
1 |
|
$this->factory = new ResponseFactory(); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return \Nip\Http\Response\Response |
54
|
|
|
*/ |
55
|
1 |
|
protected function toResponse() |
56
|
|
|
{ |
57
|
1 |
|
$controllerMethod = $this->responseControllerMethod(); |
58
|
1 |
|
if (method_exists($this->controller, $controllerMethod)) { |
59
|
|
|
return call_user_func_array([$this->controller, $controllerMethod], [$this->factory, $this->payload]); |
60
|
|
|
} |
61
|
1 |
|
$format = $this->payload->getFormat(); |
62
|
1 |
|
if (in_array($format, ['view', 'html'])) { |
63
|
|
|
return $this->toViewResponse(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return $this->factory->noContent(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \Nip\Http\Response\Response |
71
|
|
|
*/ |
72
|
|
|
protected function toViewResponse() |
73
|
|
|
{ |
74
|
|
|
$view = $this->controller->getView(); |
|
|
|
|
75
|
|
|
|
76
|
|
|
ControllerViewHydrator::populatePath($view, $this->controller); |
|
|
|
|
77
|
|
|
ControllerViewHydrator::initVars($view, $this->controller); |
|
|
|
|
78
|
|
|
ControllerViewHydrator::initContentBlocks($view, $this->controller); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->factory->setView($view); |
|
|
|
|
81
|
|
|
$this->payload->headers->set('Content-Type', 'text/html'); |
82
|
|
|
|
83
|
|
|
$viewPath = $this->controller->getLayoutPath(); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$response = $this->factory->view( |
86
|
|
|
$viewPath, |
87
|
|
|
$this->payload->data->all(), |
88
|
|
|
Response::HTTP_OK, |
89
|
|
|
$this->payload->headers->all() |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$response->setCharset('utf-8'); |
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
protected function responseControllerMethod(): string |
97
|
|
|
{ |
98
|
1 |
|
return $this->controller->getAction() |
99
|
1 |
|
.Str::studly($this->payload->getFormat()) |
100
|
1 |
|
.'Response'; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|