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