Passed
Push — dev ( be6874...dc909a )
by Jakob
02:38
created

Parser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parseJSKOS() 0 26 3
B parse() 0 39 6
A jsonLDSerialize() 0 9 4
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
    # TODO: move to jskos-php package
23
    private function jsonLDSerialize($jskos, $context, int $typed=0) {
24
        $data = $jskos->jsonLDSerialize($context);
25
        if ($typed && !count($data['type'] ?? [])) {
26
            $class = get_class($jskos);
27
            if (defined("$class::TYPES")) {
28
                $data['type'] = [$class::TYPES[0]];
29
            }
30
        }
31
        return $data;
32
    }
33
34
    /**
35
     * Parse JSKOS Resources to RDF.
36
     */
37
    public function parseJSKOS(PrettyJsonSerializable $jskos): EasyRdf_Graph
38
    {
39
        if (!$this->context) {
40
            $this->context = json_decode(file_get_contents(__DIR__.'/jskos-context.json'));
41
        }
42
        
43
        # TODO: put into JSKOS core package?
44
        if ($jskos instanceof Set) {
45
            $data = $jskos->map(
46
                function ($m) {
47
                    return json_encode($this->jsonLDSerialize($m,'', 1));
48
                }
49
            );
50
            $json = '{"@graph":['. implode(',', $data)."]}";
51
        } else { # TODO: throw on unparsable JSKOS
52
            $data = $this->jsonLDSerialize($jskos, '', 1);
53
            $json = json_encode($data);
54
        }
55
56
        $rdf = JsonLD::toRdf($json, ['expandContext' => $this->context]);
57
        # TODO: catch (\ML\JsonLD\Exception\JsonLdException $e) 
58
59
        $graph = new EasyRdf_Graph();
60
        $this->parse($graph, $rdf);
61
62
        return $graph;
63
    }
64
65
    /**
66
     * Parse Quads as returned by JsonLD.
67
     */
68
    public function parse($graph, $data, $format = 'jsonld', $baseUri = NULL) 
69
    {
70
        parent::checkParseParams($graph, $data, $format, $baseUri);
71
72
        foreach ($data as $quad) {
73
            $subject = (string)$quad->getSubject();
74
75
            if ('_:' === substr($subject, 0, 2)) {
76
                $subject = $this->remapBnode($subject);
77
            }
78
79
            $predicate = (string)$quad->getProperty();
80
81
            if ($quad->getObject() instanceof IRI) {
82
                $object = [
83
                    'type' => 'uri',
84
                    'value' => (string)$quad->getObject()
85
                ];
86
87
                if ('_:' === substr($object['value'], 0, 2)) {
88
                    $object = [
89
                        'type' => 'bnode',
90
                        'value' => $this->remapBnode($object['value'])
91
                    ];
92
                }
93
            } else {
94
                $object = [
95
                    'type' => 'literal',
96
                    'value' => $quad->getObject()->getValue()
97
                ];
98
99
                if ($quad->getObject() instanceof LanguageTaggedString) {
100
                    $object['lang'] = $quad->getObject()->getLanguage();
101
                } else {
102
                    $object['datatype'] = $quad->getObject()->getType();
103
                }
104
            }
105
106
            $this->addTriple($subject, $predicate, $object);
107
        }
108
    }
109
}
110