1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Savannabits\JetstreamInertiaGenerator\Helpers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Symfony\Component\HttpFoundation\HeaderBag; |
10
|
|
|
|
11
|
|
|
class ApiResponse |
12
|
|
|
{ |
13
|
|
|
private $success=true, $message="Request Successful.", $payload=[], $code, $headers=[]; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The operation was successful. |
17
|
|
|
* @return ApiResponse |
18
|
|
|
*/ |
19
|
|
|
public function success() { |
20
|
|
|
$this->success = true; |
21
|
|
|
if (!$this->code) { |
22
|
|
|
$this->code = 200; |
23
|
|
|
} |
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The operation was not successful, we are returning an error |
29
|
|
|
* @return ApiResponse |
30
|
|
|
*/ |
31
|
|
|
public function failed() { |
32
|
|
|
$this->success = false; |
33
|
|
|
if (!$this->code) { |
34
|
|
|
$this->code = 500; |
35
|
|
|
} |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* set the message to respond with |
41
|
|
|
* @param string $message |
42
|
|
|
* @return ApiResponse |
43
|
|
|
*/ |
44
|
|
|
public function message($message="") { |
45
|
|
|
$this->message = $message; |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set the payload to the response |
51
|
|
|
* @param $payload | The data to send to the client |
52
|
|
|
* @return ApiResponse |
53
|
|
|
*/ |
54
|
|
|
public function payload($payload) { |
55
|
|
|
$this->payload = $payload; |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set the response code according to the status of the response |
61
|
|
|
* @param int $httpCode |
62
|
|
|
* @return ApiResponse |
63
|
|
|
*/ |
64
|
|
|
public function code($httpCode=200) { |
65
|
|
|
$this->code = $httpCode; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set custom response headers if necessary |
71
|
|
|
* @param array|HeaderBag $headers |
72
|
|
|
*/ |
73
|
|
|
public function headers($headers) { |
74
|
|
|
$this->headers = $headers; |
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
public function send() { |
78
|
|
|
return response()->json([ |
|
|
|
|
79
|
|
|
"success" => $this->success, |
80
|
|
|
"message" => $this->message, |
81
|
|
|
"payload" => $this->payload, |
82
|
|
|
],$this->code)->withHeaders($this->headers); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.