Completed
Push — master ( 52b2ea...d5cfd4 )
by Jasper
12s queued 10s
created

JsonapiParser::buildMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Swis\JsonApi\Client\Parsers;
4
5
use Swis\JsonApi\Client\Exceptions\ValidationException;
6
use Swis\JsonApi\Client\Jsonapi;
7
8
/**
9
 * @internal
10
 */
11
class JsonapiParser
12
{
13
    /**
14
     * @var \Swis\JsonApi\Client\Parsers\MetaParser
15
     */
16
    private $metaParser;
17
18
    /**
19
     * @param \Swis\JsonApi\Client\Parsers\MetaParser $metaParser
20
     */
21 234
    public function __construct(MetaParser $metaParser)
22
    {
23 234
        $this->metaParser = $metaParser;
24 234
    }
25
26
    /**
27
     * @param mixed $data
28
     *
29
     * @return \Swis\JsonApi\Client\Jsonapi
30
     */
31 84
    public function parse($data): Jsonapi
32
    {
33 84
        if (!is_object($data)) {
34 36
            throw new ValidationException(sprintf('Jsonapi has to be an object, "%s" given.', gettype($data)));
35
        }
36 48
        if (property_exists($data, 'version') && !is_string($data->version)) {
37 36
            throw new ValidationException(sprintf('Jsonapi property "version" has to be a string, "%s" given.', gettype($data->version)));
38
        }
39
40 12
        return new Jsonapi(
41 12
            property_exists($data, 'version') ? $data->version : null,
42 12
            property_exists($data, 'meta') ? $this->metaParser->parse($data->meta) : null
43
        );
44
    }
45
}
46