|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RichToms\LaravelJsend; |
|
4
|
|
|
|
|
5
|
|
|
class JSend |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @param mixed $data |
|
9
|
|
|
* @param int $statusCode |
|
10
|
|
|
* @return $this |
|
11
|
|
|
*/ |
|
12
|
|
|
public function build($data, $statusCode = 200) |
|
13
|
|
|
{ |
|
14
|
|
|
return $this->getResponseHandler($statusCode) |
|
15
|
|
|
->withData($data) |
|
16
|
|
|
->withStatusCode($statusCode); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $message |
|
21
|
|
|
* @param int $statusCode |
|
22
|
|
|
* @return $this |
|
23
|
|
|
*/ |
|
24
|
|
|
public function error($message, $statusCode = 500, $code = null, $data = null) |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
return $this->getResponseHandler($statusCode) |
|
27
|
|
|
->withMessage($message) |
|
28
|
|
|
->withStatusCode($statusCode); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $data |
|
33
|
|
|
* @param int $statusCode |
|
34
|
|
|
* @return $this |
|
35
|
|
|
*/ |
|
36
|
|
|
public function fail($data, $statusCode = 400) |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->getResponseHandler($statusCode) |
|
39
|
|
|
->withData($data) |
|
40
|
|
|
->withStatusCode($statusCode); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param int $statusCode |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getResponseHandler($statusCode) |
|
48
|
|
|
{ |
|
49
|
|
|
if ($statusCode >= 500) { |
|
50
|
|
|
return $this->getErrorResponseHandler(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($statusCode >= 400) { |
|
54
|
|
|
return $this->getFailResponseHandler(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->getSuccessResponseHandler(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return \RichToms\LaravelJSend\Responses\Response |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function getErrorResponseHandler() |
|
64
|
|
|
{ |
|
65
|
|
|
return new Responses\ErrorResponse; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return \RichToms\LaravelJSend\Responses\Response |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getFailResponseHandler() |
|
72
|
|
|
{ |
|
73
|
|
|
return new Responses\FailResponse; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return \RichToms\LaravelJSend\Responses\Response |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function getSuccessResponseHandler() |
|
80
|
|
|
{ |
|
81
|
|
|
return new Responses\SuccessResponse; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.