Completed
Push — master ( b2e200...1332b8 )
by Oscar
04:48
created

BodyParser::urlencode()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 1
nop 1
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
0 ignored issues
show
Bug introduced by
There is no parameter named $response. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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
0 ignored issues
show
Bug introduced by
There is no parameter named $response. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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
0 ignored issues
show
Bug introduced by
There is no parameter named $response. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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