BodyParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A parsedBody() 0 16 6
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