|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fermi; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates\Engine; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
8
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
9
|
|
|
use Zend\Diactoros\Response\TextResponse; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
|
|
12
|
|
|
class Response |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Returns a response with a particular view loaded. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $view name of the view to load |
|
18
|
|
|
* @param array $data data to expose to the view |
|
19
|
|
|
* @param Engine $engine alternative rendering engine |
|
20
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
21
|
|
|
*/ |
|
22
|
1 |
|
public static function view(string $view, array $data, Engine $engine = null): ResponseInterface |
|
23
|
|
|
{ |
|
24
|
1 |
|
return new HtmlResponse(Framework::render($view, $data, $engine)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Returns a response with a particular view loaded. |
|
29
|
|
|
* |
|
30
|
|
|
* @param mixed $json JSON serializable data |
|
|
|
|
|
|
31
|
|
|
* @param int $flags JSON flags |
|
32
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public static function json($data, int $flags = 79): ResponseInterface |
|
35
|
|
|
{ |
|
36
|
3 |
|
return new JsonResponse($data, 200, [], $flags); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new basic string response. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $string string to respond |
|
43
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public static function string(string $string): ResponseInterface |
|
46
|
|
|
{ |
|
47
|
4 |
|
return new TextResponse($string); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Respond with a 400 bad request error json message. |
|
52
|
|
|
* |
|
53
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public static function error400(ServerRequestInterface $request): ResponseInterface |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
1 |
|
return static::json(["message" => "Bad Request"])->withStatus(400); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Respond with a 403 error message. |
|
62
|
|
|
* |
|
63
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public static function error403(ServerRequestInterface $request): ResponseInterface |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
1 |
|
return static::string("Not Authorized")->withStatus(403); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Respond with a 404 error message. |
|
72
|
|
|
* |
|
73
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public static function error404(ServerRequestInterface $request): ResponseInterface |
|
76
|
|
|
{ |
|
77
|
1 |
|
return static::string($request->getUri() . " was not found.")->withStatus(404); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Respond with a 405 error message. |
|
82
|
|
|
* |
|
83
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public static function error405(ServerRequestInterface $request): ResponseInterface |
|
86
|
|
|
{ |
|
87
|
1 |
|
return static::string("Method " . $request->getMethod() . " not allowed")->withStatus(405); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Respond with a 429 error message. |
|
92
|
|
|
* |
|
93
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public static function error429(ServerRequestInterface $request): ResponseInterface |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
1 |
|
return static::json(["message" => "Too Many Requests"])->withStatus(429); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.