1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Transformers; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Generic resolver to parse the body content. |
9
|
|
|
*/ |
10
|
|
|
class BodyParser implements ResolverInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var bool Whether convert the object into associative arrays |
14
|
|
|
*/ |
15
|
|
|
private $associative = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array List of all parseable content types |
19
|
|
|
*/ |
20
|
|
|
protected $contentTypes = [ |
21
|
|
|
'json' => 'application/json', |
22
|
|
|
'urlencode' => 'application/x-www-form-urlencoded', |
23
|
|
|
'csv' => 'text/csv', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $id |
28
|
|
|
* |
29
|
|
|
* @return callable|null |
30
|
|
|
*/ |
31
|
|
|
public function resolve($id) |
32
|
|
|
{ |
33
|
|
|
foreach ($this->contentTypes as $method => $contentType) { |
34
|
|
|
if (stripos($id, $contentType) === 0) { |
35
|
|
|
return [$this, $method]; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* JSON parser. |
42
|
|
|
* |
43
|
|
|
* @param ServerRequestInterface $response |
|
|
|
|
44
|
|
|
* |
45
|
|
|
* @return ServerRequestInterface |
46
|
|
|
*/ |
47
|
|
|
public function json(ServerRequestInterface $request) |
48
|
|
|
{ |
49
|
|
|
$data = json_decode((string) $request->getBody(), $this->associative); |
50
|
|
|
|
51
|
|
|
return $request->withParsedBody($data); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Parses url-encoded strings. |
56
|
|
|
* |
57
|
|
|
* @param ServerRequestInterface $response |
|
|
|
|
58
|
|
|
* |
59
|
|
|
* @return ServerRequestInterface |
60
|
|
|
*/ |
61
|
|
|
public function urlencode(ServerRequestInterface $request) |
62
|
|
|
{ |
63
|
|
|
parse_str((string) $request->getBody(), $data); |
64
|
|
|
|
65
|
|
|
return $request->withParsedBody($data ?: []); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Parses csv strings. |
70
|
|
|
* |
71
|
|
|
* @param ServerRequestInterface $response |
|
|
|
|
72
|
|
|
* |
73
|
|
|
* @return ServerRequestInterface |
74
|
|
|
*/ |
75
|
|
|
public function csv(ServerRequestInterface $request) |
76
|
|
|
{ |
77
|
|
|
$body = $request->getBody(); |
78
|
|
|
|
79
|
|
|
if ($body->isSeekable()) { |
80
|
|
|
$body->rewind(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$stream = $body->detach(); |
84
|
|
|
$data = []; |
85
|
|
|
|
86
|
|
|
while (($row = fgetcsv($stream)) !== false) { |
87
|
|
|
$data[] = $row; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
fclose($stream); |
91
|
|
|
|
92
|
|
|
return $request->withParsedBody($data); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.