Parser::parse()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
c 2
b 0
f 0
nc 9
nop 4
dl 0
loc 39
rs 8.9297
1
<?php declare(strict_types = 1);
2
3
namespace JSKOS\RDF;
4
5
use JSKOS\PrettyJsonSerializable;
6
use JSKOS\Set;
7
use ML\JsonLD\JsonLD;
8
use ML\JsonLD\NQuads;
9
use ML\IRI\IRI;
10
use ML\JsonLD\LanguageTaggedString;
11
use EasyRdf_Graph;
12
13
/**
14
 * EasyRdf parser to support JSON-LD to RDF conversion.
15
 *
16
 * Bridges PHP packages EasyRDF and json-ld.
17
 */
18
class Parser extends \EasyRdf_Parser
19
{
20
    protected $context;
21
22
    /**
23
     * Parse JSKOS Resources to RDF.
24
     */
25
    public function parseJSKOS(PrettyJsonSerializable $jskos): EasyRdf_Graph
26
    {
27
        if (!$this->context) {
28
            $this->context = json_decode(file_get_contents(__DIR__.'/jskos-context.json'));
29
        }
30
        
31
        if ($jskos instanceof Set) {
32
            $json = '{"@graph":'.json_encode($jskos->jsonLDSerialize('', true)).'}';
33
        } else {
34
            $json = json_encode($jskos->jsonLDSerialize('', true));
35
        }
36
37
        $rdf = JsonLD::toRdf($json, ['expandContext' => $this->context]);
38
        # TODO: catch (\ML\JsonLD\Exception\JsonLdException $e)
39
40
        $graph = new EasyRdf_Graph();
41
        $this->parse($graph, $rdf);
42
43
        return $graph;
44
    }
45
46
    /**
47
     * Parse Quads as returned by JsonLD.
48
     */
49
    public function parse($graph, $data, $format = 'jsonld', $baseUri = null)
50
    {
51
        parent::checkParseParams($graph, $data, $format, $baseUri);
52
53
        foreach ($data as $quad) {
54
            $subject = (string)$quad->getSubject();
55
56
            if ('_:' === substr($subject, 0, 2)) {
57
                $subject = $this->remapBnode($subject);
58
            }
59
60
            $predicate = (string)$quad->getProperty();
61
62
            if ($quad->getObject() instanceof IRI) {
63
                $object = [
64
                    'type' => 'uri',
65
                    'value' => (string)$quad->getObject()
66
                ];
67
68
                if ('_:' === substr($object['value'], 0, 2)) {
69
                    $object = [
70
                        'type' => 'bnode',
71
                        'value' => $this->remapBnode($object['value'])
72
                    ];
73
                }
74
            } else {
75
                $object = [
76
                    'type' => 'literal',
77
                    'value' => $quad->getObject()->getValue()
78
                ];
79
80
                if ($quad->getObject() instanceof LanguageTaggedString) {
81
                    $object['lang'] = $quad->getObject()->getLanguage();
82
                } else {
83
                    $object['datatype'] = $quad->getObject()->getType();
84
                }
85
            }
86
87
            $this->addTriple($subject, $predicate, $object);
88
        }
89
    }
90
}
91