BodyParser::parsedBody()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 4
nop 1
dl 0
loc 16
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Server request body parser
4
 * User: moyo
5
 * Date: 2018/6/11
6
 * Time: 11:41 AM
7
 */
8
9
namespace Carno\Web\Chips\Server;
10
11
use Psr\Http\Message\ServerRequestInterface;
12
13
trait BodyParser
14
{
15
    /**
16
     * @param ServerRequestInterface $srq
17
     */
18
    protected function parsedBody(ServerRequestInterface $srq) : void
19
    {
20
        $srm = $srq->getMethod();
21
22
        if ($srm === 'GET' || $srm === 'HEAD' || $srm === 'OPTIONS') {
23
            return;
24
        }
25
26
        switch ($srq->getHeaderLine('Content-Type')) {
27
            case 'application/json':
28
                $srq->withParsedBody(json_decode((string)$srq->getBody(), true));
29
                break;
30
            case 'application/x-www-form-urlencoded':
31
                parse_str((string)$srq->getBody(), $posted);
32
                $srq->withParsedBody($posted);
33
                break;
34
        }
35
    }
36
}
37