Skip to content

Commit a35ec65

Browse files
committed
added support for baseUri usage; refined README.md
create the Parser instance using: > new Parser(new DataFactory(), $baseUri); to tell it to use $baseUri instead of a random string
1 parent c7fb517 commit a35ec65

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ foreach ($quadsIterator as $quad ) {
7171

7272
When using this class keep the following in mind:
7373

74-
* Even though output buffering is used, `exec` keeps leaking output in the terminal (maybe in the browser too?). It is shown at least in PHPUnit tests.
7574
* The `Parser` class is basically a wrapper around the `rapper` command, which means input must be prepared for further processing (may impact performance). A readable local file as source is needed as well as a local file for generated N-Quads output, which is used for the internal NQuads parser later on.
7675

7776
## License

src/Parser.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*/
3131
class Parser implements ParserInterface
3232
{
33+
private string|null $baseUri = null;
34+
3335
private DataFactoryInterface $dataFactory;
3436

3537
/**
@@ -51,8 +53,9 @@ class Parser implements ParserInterface
5153
* @throws \Error
5254
* @throws \Exception
5355
*/
54-
public function __construct(DataFactoryInterface $dataFactory)
56+
public function __construct(DataFactoryInterface $dataFactory, string|null $baseUri = null)
5557
{
58+
$this->baseUri = $baseUri;
5659
$this->dataFactory = $dataFactory;
5760

5861
$tempDir = sys_get_temp_dir();
@@ -78,6 +81,8 @@ public function __destruct()
7881
}
7982

8083
/**
84+
* Tells the parser to not guess the format, but to use the given one.
85+
*
8186
* @throws \ValueError
8287
*/
8388
public function setFormat(string|null $format): void
@@ -192,7 +197,8 @@ public function parse($input): QuadIteratorInterface
192197
RapperCommand::parseSourceFileAndPutGeneratedRdfIntoTargetFile(
193198
$sourceFilePath,
194199
$targetFilepath,
195-
$this->format
200+
$this->format,
201+
$this->baseUri
196202
);
197203

198204
// create NQuads parser to parse generated target file

src/RapperCommand.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,24 @@ public static function rapperCommandIsAvailable(): bool
130130
public static function parseSourceFileAndPutGeneratedRdfIntoTargetFile(
131131
string $sourceFilepath,
132132
string $targetFilepath,
133-
string|null $sourceFileFormat = null
133+
string|null $sourceFileFormat = null,
134+
string|null $baseUri = null
134135
): void {
135136
$normalizedFormat = self::getNormalizedFormat($sourceFileFormat);
136137

137138
if (file_exists($sourceFilepath) && file_exists($targetFilepath)) {
138-
$command = 'rapper --quiet '.$normalizedFormat.' -o ntriples '.$sourceFilepath.' > '.$targetFilepath;
139+
// build command string
140+
$command = 'rapper --quiet --ignore-errors ';
141+
142+
if (is_string($baseUri) && 0 < strlen($baseUri)) {
143+
$command .= ' --input-uri '.$baseUri.' ';
144+
}
145+
146+
$command .= $normalizedFormat.' -o ntriples '.$sourceFilepath.' > '.$targetFilepath;
147+
148+
// note: using escapeshellcmd here would escape the > in the command which breaks the whole thing
149+
150+
// run the command
139151
exec($command);
140152
} else {
141153
throw new RdfIoException('Either source or target filepath points to a non existing file');

tests/ParserTest.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function setUp(): void
3232
}
3333
}
3434

35-
private function getSubjectUnderTest(): Parser
35+
private function getSubjectUnderTest(string|null $baseUri): Parser
3636
{
37-
$subjectUnderTest = new Parser(new DataFactory());
37+
$subjectUnderTest = new Parser(new DataFactory(), $baseUri);
3838
$subjectUnderTest->setDirPathForTemporaryFiles(__DIR__.'/../.cache');
3939
return $subjectUnderTest;
4040
}
@@ -55,22 +55,41 @@ private function generateTripleStringArray(QuadIteratorInterface $iterator): arr
5555

5656
public function testParseString(): void
5757
{
58-
$iterator = $this->getSubjectUnderTest()->parse($this->testRdfString);
58+
$iterator = $this->getSubjectUnderTest(null)->parse($this->testRdfString);
5959

6060
$this->assertEquals(
6161
['http://bar http://baz 1', 'http://bar http://baz 2'],
6262
$this->generateTripleStringArray($iterator)
6363
);
6464
}
6565

66+
public function testParseStringWithBaseUri(): void
67+
{
68+
$str = '@prefix : <http://example.org/prefix/> .
69+
@prefix rdf: <http://example.org/rdf/> .
70+
71+
<name> rdf:type rdf:Property .
72+
:phone rdf:type rdf:Property .';
73+
74+
$iterator = $this->getSubjectUnderTest('http://test/base/')->parse($str);
75+
76+
$this->assertEquals(
77+
[
78+
'http://test/base/name http://example.org/rdf/type http://example.org/rdf/Property',
79+
'http://example.org/prefix/phone http://example.org/rdf/type http://example.org/rdf/Property',
80+
],
81+
$this->generateTripleStringArray($iterator)
82+
);
83+
}
84+
6685
public function testParseResource(): void
6786
{
6887
// put test RDF into a temp. file
6988
$filepath = tempnam(sys_get_temp_dir(), 'phpunit_parsertest_');
7089
file_put_contents($filepath, $this->testRdfString);
7190
$resource = fopen($filepath, 'r');
7291

73-
$iterator = $this->getSubjectUnderTest()->parse($resource);
92+
$iterator = $this->getSubjectUnderTest(null)->parse($resource);
7493

7594
$this->assertEquals(
7695
['http://bar http://baz 1', 'http://bar http://baz 2'],
@@ -87,7 +106,7 @@ public function testParseResponseInterface(): void
87106
$response = $this->createMock(ResponseInterface::class);
88107
$response->method('getBody')->willReturn($stream);
89108

90-
$iterator = $this->getSubjectUnderTest()->parse($response);
109+
$iterator = $this->getSubjectUnderTest(null)->parse($response);
91110

92111
$this->assertEquals(
93112
['http://bar http://baz 1', 'http://bar http://baz 2'],
@@ -100,7 +119,7 @@ public function testParseStreamInterface(): void
100119
$stream = $this->createMock(StreamInterface::class);
101120
$stream->method('__toString')->willReturn($this->testRdfString);
102121

103-
$iterator = $this->getSubjectUnderTest()->parse($stream);
122+
$iterator = $this->getSubjectUnderTest(null)->parse($stream);
104123

105124
$this->assertEquals(
106125
['http://bar http://baz 1', 'http://bar http://baz 2'],
@@ -110,7 +129,7 @@ public function testParseStreamInterface(): void
110129

111130
public function testParseWithCustomFormat(): void
112131
{
113-
$subjectUnderTest = $this->getSubjectUnderTest();
132+
$subjectUnderTest = $this->getSubjectUnderTest(null);
114133
$subjectUnderTest->setFormat('ntriples');
115134
$iterator = $subjectUnderTest->parse($this->testRdfString);
116135

@@ -127,7 +146,7 @@ public function testParseWithInvalidFormat(): void
127146
$msg .= 'ntriples, n-triples, rdfa, rdfxml, rdfxml-abbrev, rdfxml-xmp, rss-1.0, trig, turtle, text/turtle, ttl, xml';
128147
$this->expectExceptionMessage($msg);
129148

130-
$subjectUnderTest = $this->getSubjectUnderTest();
149+
$subjectUnderTest = $this->getSubjectUnderTest(null);
131150
$subjectUnderTest->setFormat('invalid');
132151
$subjectUnderTest->parse($this->testRdfString);
133152
}
@@ -139,7 +158,7 @@ public function testParseStreamResource(): void
139158
file_put_contents($filepath, $this->testRdfString);
140159
$resource = fopen($filepath, 'r');
141160

142-
$iterator = $this->getSubjectUnderTest()->parseStream($resource);
161+
$iterator = $this->getSubjectUnderTest(null)->parseStream($resource);
143162

144163
$this->assertEquals(
145164
['http://bar http://baz 1', 'http://bar http://baz 2'],
@@ -152,7 +171,7 @@ public function testParseStreamStreamInterface(): void
152171
$stream = $this->createMock(StreamInterface::class);
153172
$stream->method('__toString')->willReturn($this->testRdfString);
154173

155-
$iterator = $this->getSubjectUnderTest()->parseStream($stream);
174+
$iterator = $this->getSubjectUnderTest(null)->parseStream($stream);
156175

157176
$this->assertEquals(
158177
['http://bar http://baz 1', 'http://bar http://baz 2'],

0 commit comments

Comments
 (0)