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