JsonapiParser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 10
c 0
b 0
f 0
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parse() 0 12 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\JsonApi\Client\Parsers;
6
7
use Swis\JsonApi\Client\Exceptions\ValidationException;
8
use Swis\JsonApi\Client\Jsonapi;
9
10
/**
11
 * @internal
12
 */
13
class JsonapiParser
14
{
15
    private MetaParser $metaParser;
16
17 204
    public function __construct(MetaParser $metaParser)
18
    {
19 204
        $this->metaParser = $metaParser;
20 102
    }
21
22
    /**
23
     * @param  mixed  $data
24
     */
25 56
    public function parse($data): Jsonapi
26
    {
27 56
        if (! is_object($data)) {
28 24
            throw new ValidationException(sprintf('Jsonapi MUST be an object, "%s" given.', gettype($data)));
29
        }
30 32
        if (property_exists($data, 'version') && ! is_string($data->version)) {
31 24
            throw new ValidationException(sprintf('Jsonapi property "version" MUST be a string, "%s" given.', gettype($data->version)));
32
        }
33
34 8
        return new Jsonapi(
35 8
            property_exists($data, 'version') ? $data->version : null,
36 8
            property_exists($data, 'meta') ? $this->metaParser->parse($data->meta) : null
37 4
        );
38
    }
39
}
40