Completed
Push — master ( 0456c5...25ee32 )
by Oleg
04:45
created

Parser::extract()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0092
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Stdlib\Request;
5
6
use Psr\Http\Message\ServerRequestInterface;
7
use Zend\Hydrator\HydratorInterface;
8
9
final class Parser
10
{
11 90
    public static function getRequestBody(ServerRequestInterface $request): array
12
    {
13 90
        $data = $request->getParsedBody();
14 90
        if ($data === null) {
15 2
            $data = [];
16
        }
17 90
        if (is_object($data)) {
18 4
            $data = self::extract($data);
19
        }
20
21 90
        return $data;
22
    }
23
24 4
    private static function extract($object): array
25
    {
26 4
        if (is_iterable($object)) {
27 2
            $result = [];
28 2
            foreach ($object as $key => $value) {
29 2
                if (is_object($value)) {
30
                    $result[$key] = self::extract($object);
31
                } else {
32 2
                    $result[$key] = $value;
33
                }
34
            }
35 2
            return $result;
36
        } else {
37 2
            $config = new \GeneratedHydrator\Configuration(get_class($object));
38 2
            $hydratorClass = $config->createFactory()->getHydratorClass();
39
            /** @var HydratorInterface $hydrator */
40 2
            $hydrator = new $hydratorClass();
41 2
            return $hydrator->extract($object);
42
        }
43
    }
44
}
45