• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sweetrdf / quickRdfIo / #110

24 Apr 2025 02:36PM UTC coverage: 90.805%. First build
#110

push

php-coveralls

zozlak
Implement rdf-interface 3.1.0

* Implement $baseUri parameter handling in parse() and parseStream() of
  all parsers.
* Util::parse(): pass document base URI to the parser.

(closes #11)

33 of 34 new or added lines in 7 files covered. (97.06%)

869 of 957 relevant lines covered (90.8%)

6.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.47
/src/quickRdfIo/JsonLdParser.php
1
<?php
2

3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2022 zozlak.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26

27
namespace quickRdfIo;
28

29
use Psr\Http\Message\StreamInterface;
30
use ML\IRI\IRI;
31
use ML\JsonLD\JsonLD;
32
use ML\JsonLD\Quad as JsonLdQuad;
33
use ML\JsonLD\LanguageTaggedString;
34
use ML\JsonLD\TypedValue;
35
use rdfInterface\QuadIteratorInterface as iQuadIterator;
36
use rdfInterface\ParserInterface as iParser;
37
use rdfInterface\QuadInterface as iQuad;
38
use rdfInterface\DataFactoryInterface as iDataFactory;
39

40
/**
41
 * Thin wrapper providing RdfInterface\Parser API for JSON-LD the parser
42
 * provided by the ml/json-ld library.
43
 *
44
 * Doesn't provide stream parsing because the ml/json-ld library doesn't so
45
 * be carefull when parsing large inputs.
46
 * 
47
 * @author zozlak
48
 */
49
class JsonLdParser implements iParser, iQuadIterator {
50

51
    use CreateBlankNodeTrait;
52

53
    private iDataFactory $dataFactory;
54
    private ?iQuad $curQuad = null;
55

56
    /**
57
     * 
58
     * @var array<int, JsonLdQuad>
59
     */
60
    private array $quads;
61

62
    public function __construct(iDataFactory $dataFactory,
63
                                ?string $baseUri = null) {
64
        $this->dataFactory = $dataFactory;
6✔
65
        $this->baseUri     = $baseUri ?? '';
6✔
66
    }
67

68
    public function setBaseUri(?string $baseUri): void {
NEW
69
        $this->baseUri = $baseUri ?? '';
×
70
    }
71

72
    public function current(): iQuad | null {
73
        $df   = $this->dataFactory;
6✔
74
        $quad = current($this->quads);
6✔
75
        if ($quad === false) {
6✔
76
            return null;
×
77
        }
78
        if ($this->curQuad === null) {
6✔
79
            /* @var $quad JsonLdQuad */
80
            $sbj = $quad->getSubject();
6✔
81
            $sbj = $sbj->getScheme() === '_' ? $this->createBlankNode((string) $sbj) : $df->namedNode((string) $sbj);
6✔
82

83
            $pred = $df->namedNode((string) $quad->getProperty());
6✔
84

85
            $obj = $quad->getObject();
6✔
86
            if ($obj instanceof LanguageTaggedString) {
6✔
87
                $obj = $df->literal($obj->getValue(), $obj->getLanguage());
4✔
88
            } elseif ($obj instanceof TypedValue) {
6✔
89
                $obj = $df->literal($obj->getValue(), null, $obj->getType());
5✔
90
            } elseif ($obj instanceof IRI) {
5✔
91
                $obj = $obj->getScheme() === '_' ? $df->blankNode((string) $obj) : $df->namedNode((string) $obj);
5✔
92
            } else {
93
                throw new RdfIoException("Unsupported object class " . $obj::class);
×
94
            }
95

96
            $graph = $quad->getGraph();
6✔
97
            if (!empty((string) $graph)) {
6✔
98
                $graph = $graph->getScheme() === '_' ? $df->blankNode((string) $graph) : $df->namedNode($graph);
3✔
99
            } else {
100
                $graph = $df->DefaultGraph();
3✔
101
            }
102

103
            $this->curQuad = $df->quad($sbj, $pred, $obj, $graph);
6✔
104
        }
105
        return $this->curQuad;
6✔
106
    }
107

108
    public function key(): int | null {
109
        return key($this->quads);
×
110
    }
111

112
    public function next(): void {
113
        next($this->quads);
6✔
114
        $this->curQuad = null;
6✔
115
    }
116

117
    public function parse(string $input, string $baseUri = ''): iQuadIterator {
118
        if (substr($input, 0, 3) === "\xEF\xBB\xBF") {
6✔
119
            $input = substr($input, 3);
1✔
120
        }
121
        if (!empty($baseUri)) {
6✔
122
            $this->baseUri = $baseUri;
1✔
123
        }
124
        $this->quads = JsonLD::toRdf($input, ['base' => empty($this->baseUri) ? null : $this->baseUri]);
6✔
125
        return $this;
6✔
126
    }
127

128
    /**
129
     * 
130
     * @param resource | StreamInterface $input
131
     * @return iQuadIterator
132
     */
133
    public function parseStream($input, string $baseUri = ''): iQuadIterator {
134
        $input         = is_resource($input) ? stream_get_contents($input) : $input->getContents();
3✔
135
        return $this->parse($input ?: '', $baseUri);
3✔
136
    }
137

138
    public function rewind(): void {
139
        reset($this->quads);
6✔
140
    }
141

142
    public function valid(): bool {
143
        return key($this->quads) !== null;
6✔
144
    }
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc