Completed
Pull Request — master (#135)
by Kristof
05:16
created

IriOfferIdentifierJSONDeserializer::deserialize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Offer;
4
5
use CultuurNet\Deserializer\DeserializerInterface;
6
use CultuurNet\Deserializer\MissingValueException;
7
use CultuurNet\Deserializer\NotWellFormedException;
8
use ValueObjects\String\String;
9
10
class IriOfferIdentifierJSONDeserializer implements DeserializerInterface
11
{
12
    /**
13
     * @param String $data
14
     * @return IriOfferIdentifier
15
     */
16
    public function deserialize(String $data)
17
    {
18
        $data = json_decode($data->toNative(), true);
19
20
        if (null === $data) {
21
            throw new NotWellFormedException('Invalid JSON');
22
        }
23
24
        if (!isset($data['@id'])) {
25
            throw new MissingValueException('Missing property "@id".');
26
        }
27
        if (!isset($data['@type'])) {
28
            throw new MissingValueException('Missing property "@type".');
29
        }
30
31
        return new IriOfferIdentifier(
32
            $data['@id'],
33
            OfferType::fromNative($data['@type'])
34
        );
35
    }
36
}
37