1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrightComponents\Responder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use DevMarketer\LaraFlash\LaraFlash; |
7
|
|
|
use Illuminate\Contracts\Support\Responsable; |
8
|
|
|
use BrightComponents\Common\Payloads\AbstractPayload; |
9
|
|
|
|
10
|
|
|
abstract class Responder implements Responsable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The response payload. |
14
|
|
|
* |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
protected $payload; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Laraflash for flashing to session. |
21
|
|
|
* |
22
|
|
|
* @var \DevMarketer\LaraFlash\LaraFlash |
23
|
|
|
*/ |
24
|
|
|
protected $flash; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Construct a new base Responder. |
28
|
|
|
* |
29
|
|
|
* @param \Illuminate\Http\Request $request |
30
|
|
|
* @param \DevMarketer\LaraFlash\LaraFlash $flash |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Request $request, LaraFlash $flash) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this->request = $request; |
|
|
|
|
35
|
|
|
$this->flash = $flash; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create an HTTP response that represents the object. |
40
|
|
|
* |
41
|
|
|
* @param \Illuminate\Http\Request $request |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Http\Response |
44
|
|
|
*/ |
45
|
|
|
public function toResponse($request) |
46
|
|
|
{ |
47
|
|
|
return $this->respond(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Send a response. |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
abstract public function respond(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add the payload to the response. |
59
|
|
|
* |
60
|
|
|
* @param mixed $payload |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function withPayload($payload) |
65
|
|
|
{ |
66
|
|
|
$this->payload = $payload instanceof AbstractPayload ? $payload->getData() : $payload; |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add the request to the response. |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Http\Request $request |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function withRequest(Request $request) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->request = $request; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Flash data to the session. |
87
|
|
|
* |
88
|
|
|
* @param string $key |
|
|
|
|
89
|
|
|
* @param mixed $value |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
protected function flash($message, array $options = []) |
92
|
|
|
{ |
93
|
|
|
return $this->flash->add($message, $options); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|