|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jenky\Hermes\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
trait InteractsWithMessage |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Retrieve a header from the request. |
|
9
|
|
|
* |
|
10
|
|
|
* @param string|null $key |
|
11
|
|
|
* @param string|array|null $default |
|
12
|
|
|
* @return string|array|null |
|
13
|
|
|
*/ |
|
14
|
|
|
public function header($header = null, $default = null) |
|
15
|
|
|
{ |
|
16
|
|
|
if ($header) { |
|
17
|
|
|
return $this->getHeader($header)[0] ?? $default; |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
return array_map(function ($values) { |
|
21
|
|
|
return $values[0] ?? null; |
|
22
|
|
|
}, $this->getHeaders()); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get or check the status code. |
|
27
|
|
|
* |
|
28
|
|
|
* @param int|null $code |
|
29
|
|
|
* @return int|bool |
|
30
|
|
|
*/ |
|
31
|
|
|
public function status($code = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$statusCode = $this->getStatusCode(); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
return $code ? $statusCode == $code : $statusCode; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the response body as string. |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function body() |
|
44
|
|
|
{ |
|
45
|
|
|
return (string) $this->getBody(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Determine that response status code is ok. |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function ok(): bool |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->status(200); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Determine that response status code is created. |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function created(): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->status(201); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Determine that response status code is bad request. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function badRequest(): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->status(400); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Determine that response status code is unauthorized. |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
public function unauthorized(): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->status(401); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Determine that response status code is forbidden. |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public function forbidden(): bool |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->status(403); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Determine that response status code is not found. |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function notFound(): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->status(404); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Determine that response status code is unprocessable. |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function unprocessable(): bool |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->status(422); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Determine that response status code is internal server error. |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function serverError(): bool |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->status(500); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|