Skip to content

Commit bd6d03f

Browse files
committed
make the n3 parser aware of the order of the instructions
1 parent 5122af1 commit bd6d03f

1 file changed

Lines changed: 57 additions & 57 deletions

File tree

src/Server.php

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -397,44 +397,38 @@ private function normalizeN3($contents) {
397397

398398
private function n3Convert($contents) {
399399
$parsedGraph = $this->normalizeN3($contents);
400-
$inserts = [];
400+
$result = array();
401401
foreach ($parsedGraph[':root'] as $subject) {
402-
if (
403-
isset($subject['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']) &&
404-
(in_array('http://www.w3.org/ns/solid/terms#InsertDeletePatch', $subject['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'])) &&
405-
isset($subject['http://www.w3.org/ns/solid/terms#inserts'])
406-
) {
407-
foreach ($subject['http://www.w3.org/ns/solid/terms#inserts'] as $target) {
408-
$inserts[] = $parsedGraph[$target];
402+
if (in_array('http://www.w3.org/ns/solid/terms#InsertDeletePatch', $subject['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'])) {
403+
foreach ($subject as $predicate => $value) {
404+
switch ($predicate) {
405+
case 'http://www.w3.org/ns/solid/terms#inserts':
406+
foreach ($value as $target) {
407+
if (!isset($result['insert'])) {
408+
$result['insert'] = array();
409+
}
410+
$result['insert'][] = $parsedGraph[$target];
411+
}
412+
break;
413+
case 'http://www.w3.org/ns/solid/terms#deletes':
414+
foreach ($value as $target) {
415+
if (!isset($result['delete'])) {
416+
$result['delete'] = array();
417+
}
418+
$result['delete'][] = $parsedGraph[$target];
419+
}
420+
break;
421+
}
409422
}
410423
}
411424
}
412425

413-
$deletes = [];
414-
foreach ($parsedGraph[':root'] as $subject) {
415-
if (
416-
isset($subject['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']) &&
417-
(in_array('http://www.w3.org/ns/solid/terms#InsertDeletePatch', $subject['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'])) &&
418-
isset($subject['http://www.w3.org/ns/solid/terms#deletes'])
419-
) {
420-
foreach ($subject['http://www.w3.org/ns/solid/terms#deletes'] as $target) {
421-
$deletes[] = $parsedGraph[$target];
422-
}
423-
}
426+
foreach ($result as $key => $value) {
427+
$writer = new TriGWriter(["format" => "turtle"]);
428+
$writer->addTriples($value);
429+
$result[$key] = $writer->end();
424430
}
425-
426-
$writer = new TriGWriter(["format" => "turtle"]);
427-
$writer->addTriples($inserts);
428-
$insertTurtle = $writer->end();
429-
430-
$writer = new TriGWriter(["format" => "turtle"]);
431-
$writer->addTriples($deletes);
432-
$deleteTurtle = $writer->end();
433-
434-
return array(
435-
"insert" => $insertTurtle ?? "",
436-
"delete" => $deleteTurtle ?? ""
437-
);
431+
return $result;
438432
}
439433

440434
private function handleN3Update(Response $response, string $path, $contents): Response
@@ -457,34 +451,40 @@ private function handleN3Update(Response $response, string $path, $contents): Re
457451
// FIXME: Adding this base will allow us to parse <> entries; , $this->baseUrl . $this->basePath . $path), but that breaks the build.
458452
// FIXME: Use enums from namespace Pdsinterop\Rdf\Enum\Format instead of 'turtle'?
459453
$instructions = $this->n3Convert($contents);
460-
461-
// error_log("INSERT");
462-
// error_log($instructions['insert']);
463-
$graph->parse($instructions['insert']);
464-
465-
$deleteGraph = $this->getGraph();
466-
// error_log("DELETE");
467-
// error_log($instructions['delete']);
468-
469-
// @CHECKME: Does the Graph Parse here also need an URI?
470-
$deleteGraph->parse($instructions['delete'], "turtle");
471-
$resources = $deleteGraph->resources();
472-
foreach ($resources as $resource) {
473-
$properties = $resource->propertyUris();
474-
foreach ($properties as $property) {
475-
$values = $resource->all($property);
476-
if (!count($values)) {
477-
$graph->delete($resource, $property);
478-
} else {
479-
foreach ($values as $value) {
480-
$count = $graph->delete($resource, $property, $value);
481-
if ($count === 0) {
482-
throw new Exception("Could not delete a value", 500);
454+
foreach ($instructions as $key => $value) {
455+
switch ($key) {
456+
case "insert":
457+
// error_log("INSERT");
458+
// error_log($instructions['insert']);
459+
$graph->parse($instructions['insert'], "turtle");
460+
break;
461+
case "delete":
462+
$deleteGraph = $this->getGraph();
463+
// error_log("DELETE");
464+
// error_log($instructions['delete']);
465+
466+
// @CHECKME: Does the Graph Parse here also need an URI?
467+
$deleteGraph->parse($instructions['delete'], "turtle");
468+
$resources = $deleteGraph->resources();
469+
foreach ($resources as $resource) {
470+
$properties = $resource->propertyUris();
471+
foreach ($properties as $property) {
472+
$values = $resource->all($property);
473+
if (!count($values)) {
474+
$graph->delete($resource, $property);
475+
} else {
476+
foreach ($values as $value) {
477+
$count = $graph->delete($resource, $property, $value);
478+
if ($count === 0) {
479+
throw new Exception("Could not delete a value", 500);
480+
}
481+
}
482+
}
483483
}
484+
// FIXME: Is there a 'patches'? What does it look like and how do we handle it?
484485
}
485-
}
486+
break;
486487
}
487-
// FIXME: Is there a 'patches'? What does it look like and how do we handle it?
488488
}
489489

490490
// Assuming this is in our native format, turtle

0 commit comments

Comments
 (0)