PHP code example of sweetrdf / rdfinterface2easyrdf
1. Go to this page and download the library: Download sweetrdf/rdfinterface2easyrdf library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$graph = new EasyRdf\Graph();
$blank = $graph->resource('_:blank');
$res1 = $graph->resource('http://foo');
$res2 = $graph->resource('http://baz');
$res1->add('http://resource', $res2);
$lit1 = new EasyRdf\Literal('literal', 'en');
$lit2 = new EasyRdf\Literal(1, null, 'http://www.w3.org/2001/XMLSchema#integer');
$res1->addLiteral('http://langLiteral', $lit1);
$res1->addLiteral('http://intLiteral', $lit2);
$res3 = $graph->resource('http://marry');
$res3->addLiteral('http://langLiteral', $lit1);
$df = new quickRdf\DataFactory();
print_r(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($blank, $df));
# as $res2 contains no properties, it's converted to a named node
print_r(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($res2, $df));
print_r(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($lit1, $df));
print_r(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($lit2, $df));
# as $res1 contains properties, it's converted to a quad iterator
print_r(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($res1, $df));
foreach (rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($res1, $df) as $i) {
print_r($i);
}
# EasyRdf\Graph is also converted to a quad iterator
print_r(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($graph, $df));
foreach (rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($graph, $df) as $i) {
print_r($i);
}
echo $graph->dump('text');
# using quad iterator returned by the asRdfInterface()
$dataset = new quickRdf\Dataset();
$dataset->add(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($graph, $df));
echo $dataset;
# using add()/addDataset() method
# (addDataset() works the same, just has strictly defined return type)
$dataset = rdfInterface2easyRdf\AsRdfInterface::add($graph, $df, new quickRdf\Dataset());
echo $dataset;
# similarly for an EasyRdf\Resource
# (just only given resource triples are converted)
$dataset = new quickRdf\Dataset();
$dataset->add(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($res1, $df));
echo $dataset;
$dataset = rdfInterface2easyRdf\AsRdfInterface::add($res1, $df, new quickRdf\Dataset());
echo $dataset;
# using add()/addDataset() we can also enforce
# a whole graph to be converted based on an EasyRdf\Resource
$dataset = rdfInterface2easyRdf\AsRdfInterface::add($res1, $df, new quickRdf\Dataset(), true);
echo $dataset;
echo $graph->dump('text');
$emptyDatasetNode = new rdfHelpers\DatasetNode(new quickRdf\Dataset(), $df::blankNode());
$datasetNode = rdfInterface2easyRdf\AsRdfInterface::add($res1, $df, $emptyDatasetNode);
print_r($datasetNode->getNode());
# the dataset attached to the dataset node contains all triples
echo $datasetNode->getDataset();
# but the dataset node itself returns only triples of the converted EasyRdf\Resource
foreach($datasetNode as $i) {
echo "$i\n";
}
# conversion could be limited to EasyRdf\Resource triples only using the $wholeGraph parameter
$emptyDatasetNode = new rdfHelpers\DatasetNode(new quickRdf\Dataset(), $df::blankNode());
$datasetNode = rdfInterface2easyRdf\AsRdfInterface::add($res1, $df, $emptyDatasetNode, false);
print_r($datasetNode->getNode());
# the dataset attached to the dataset node contains all triples
echo $datasetNode->getDataset();
# addDatasetNode() works in the same way, just has narrower return type
$emptyDatasetNode = new rdfHelpers\DatasetNode(new quickRdf\Dataset(), $df::blankNode());
$datasetNode = rdfInterface2easyRdf\AsRdfInterface::addDatasetNode($res1, $df, $emptyDatasetNode);
print_r($datasetNode->getNode());
echo $datasetNode->getDataset();
$dataset = rdfInterface2easyRdf\AsRdfInterface::addDataset($res1, $df, fn() => new quickRdf\Dataset());
$datasetNode = rdfInterface2easyRdf\AsRdfInterface::addDatasetNode(
$res1,
$df,
fn($x) => new rdfHelpers\DatasetNode(new quickRdf\Dataset(), $x)
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.