PHPackages                             sweetrdf/rdfinterface2easyrdf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. sweetrdf/rdfinterface2easyrdf

ActiveLibrary

sweetrdf/rdfinterface2easyrdf
=============================

Provides methods to convert between EasyRdf and rdfInterface objects.

0.3.2(6mo ago)0101MITPHPPHP &gt;=8.0CI passing

Since Jan 4Pushed 6mo ago2 watchersCompare

[ Source](https://github.com/sweetrdf/rdfInterface2easyRdf)[ Packagist](https://packagist.org/packages/sweetrdf/rdfinterface2easyrdf)[ Docs](https://github.com/sweetrdf/rdfInterface2easyRdf)[ RSS](/packages/sweetrdf-rdfinterface2easyrdf/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (7)Versions (6)Used By (0)

rdfInterface2easyRdf
====================

[](#rdfinterface2easyrdf)

[![Latest Stable Version](https://camo.githubusercontent.com/63fcadc9167de2b3882265b5b8afb0c93c6b183b761e6eb87c0829a67382fa01/68747470733a2f2f706f7365722e707567782e6f72672f73776565747264662f726466496e7465726661636532656173795264662f762f737461626c65)](https://packagist.org/packages/sweetrdf/rdfInterface2easyRdf)[![Build status](https://github.com/sweetrdf/rdfInterface2easyRdf/workflows/phpunit/badge.svg?branch=master)](https://github.com/sweetrdf/rdfInterface2easyRdf/workflows/phpunit/badge.svg?branch=master)[![Coverage Status](https://camo.githubusercontent.com/43cde12aa7e09f08001fe69d0c9447bfd0bd5ac8acaff01883808827795b0b4f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73776565747264662f726466496e7465726661636532656173795264662f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/sweetrdf/rdfInterface2easyRdf?branch=master)[![License](https://camo.githubusercontent.com/79127cd4f69cc688a671357454ce1bafdf2a54c5f416435b37519f6f39aecffa/68747470733a2f2f706f7365722e707567782e6f72672f73776565747264662f726466496e7465726661636532656173795264662f6c6963656e7365)](https://packagist.org/packages/sweetrdf/rdfInterface2easyRdf)

A library providing methods for converting between EasyRdf ([original library](https://github.com/easyrdf/easyrdf), [still maintained fork](https://github.com/sweetrdf/easyrdf)) and [rdfInterface](https://github.com/sweetrdf/rdfInterface) objects (in both directions).

Helpful especially when you have too much EasyRdf code to port it but you would like to develop new code using the rdfInterface ecosystem.

Installation
------------

[](#installation)

- Obtain the [Composer](https://getcomposer.org)
- Run `composer require sweetrdf/rdfInterface2easyRdf`
- Install EasyRdf implementation of your choice, e.g. `composer require sweetrdf/easyrdf`.
- Install rdfInterface implementation of your choice, e.g. `composer require sweetrdf/quick-rdf`.

Usage
-----

[](#usage)

### rdfInterface to EasyRdf

[](#rdfinterface-to-easyrdf)

Conversion in this direction is straightforward:

- If you don't care about strong result type checks, just use the `rdfInterface2easyRdf\AsEasyRdf::asEasyRdf()` method, e.g.: ```
    # let's prepare all kind of rdfInterface objects
    $blank = quickRdf\DataFactory::blankNode();
    $named = quickRdf\DataFactory::namedNode('http://foo');
    $literal = quickRdf\DataFactory::literal('bar', 'en');
    $quad = quickRdf\DataFactory::quad($blank, $named, $literal);
    $dataset = new quickRdf\Dataset();
    $dataset->add($quad);
    $node = $dataset->withTerm($named);

    print_r(rdfInterface2easyRdf\AsEasyRdf::AsEasyRdf($blank));
    print_r(rdfInterface2easyRdf\AsEasyRdf::AsEasyRdf($named));
    print_r(rdfInterface2easyRdf\AsEasyRdf::AsEasyRdf($literal));
    echo rdfInterface2easyRdf\AsEasyRdf::AsEasyRdf($quad)->getGraph()->dump('text');
    echo rdfInterface2easyRdf\AsEasyRdf::AsEasyRdf($node)->getGraph()->dump('text');
    echo rdfInterface2easyRdf\AsEasyRdf::AsEasyRdf($dataset)->dump('text');
    ```

    - If you want converted data to be appended to an already existing graph, pass it as a second parameter, e.g. (continuing the code from the previous example): ```
        $graph = new EasyRdf\Graph();
        $graph->resource('http://baz')->addLiteral('https://foo', 'other value');
        rdfInterface2easyRdf\AsEasyRdf::AsEasyRdf($quad, $graph);
        echo $graph->dump('text');
        ```
- If you care about stictly defined return data types, use `rdfInterface2easyRdf\AsEasyRdf::asLiteral()`, `rdfInterface2easyRdf\AsEasyRdf::asResource()` and `rdfInterface2easyRdf\AsEasyRdf::asGraph()`.
    - Each of them accepts only compatible input types, e.g. `rdfInterface2easyRdf\AsEasyRdf::asLiteral()` accepts only `rdfInterface\LiteralInterface`
    - `rdfInterface\NodeInterface` is accepted both by `rdfInterface2easyRdf\AsEasyRdf::asResource()` and `rdfInterface2easyRdf\AsEasyRdf::asGraph()`
    - An `EasyRdf\Graph` can be passed as an optional second parameter just as with `rdfInterface2easyRdf\AsEasyRdf::AsEasyRdf()`(see the example above)

### EasyRdf to rdfInterface

[](#easyrdf-to-rdfinterface)

Conversion in this direction might get tricky. Important remarks:

- As the rdfInterface defines only an interface but no actual implementation, you must always pass an RDF terms factory object (the `$dataFactory` parameter).
- As the rdfInterface doesn't define a standardized way to create datasets (`rdfInterface\DatasetInterface`) and dataset nodes (`rdfInterface\DatasetNodeInterface`) the `asRdfInterface()` method returns an `rdfInterface\QuadIteratorInterface`which is a triples iterator. **The only way to convert an `EasyRdf\Resource` or `EasyRdf\Graph` to a dataset or dataset node is to add triples from the EasyRdf object to an existing `rdfInterface\DatasetInterface` or `rdfInterface\DatasetNodeInterface`.**The `add()`, `addDataset()` and `addDatasetNode()` methods can be used for that (see examples below).
- There's an ambiguity around the `EasyRdf\Resource` conversion. You may want convert it to an RDF term (`rdfInterface\BlankNode` or `rdfInterface\NamedNode`) or you may want to convert it to a set of triples (quad iterator, dataset or dataset node).
    - The `asRdfInterface()` method converts to an RDF term if the `EasyRdf\Resource`object has no properties (no triples) and to a `rdfInterface\QuadIteratorInterface`otherwise.
    - Use `asTerm()`, `asQuadIterator()`, `add()`, `addDataset()` or `addDatasetNode()`to enforce a more specific behavior.

A sample EasyRdf graph and terms factory used in examples below:

```
$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();
```

- Use `asRdfInterface()` to guess the output type based on the input. ```
    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);
    }
    ```
- Use `asTerm()` to enforce conversion of an `EasyRdf\Resource` to a term: ```
    print_r(rdfInterface2easyRdf\AsRdfInterface::asRdfInterface($res1, $df));
    print_r(rdfInterface2easyRdf\AsRdfInterface::asTerm($res1, $df));
    ```
- There are two ways of converting an `EasyRdf\Graph` and `EasyRdf\Resource` to a dataset: ```
    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;
    ```
- Conversion of an `EasyRdf\Resource` to a dataset node is relatively most complex: ```
    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();
    ```
- In case of `add()`, `addDataset()` and `addDatasetNode()` the parameter used to pass a dataset/dataset node accepts also a callable, e.g. ```
    $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)
    );
    ```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance67

Regular maintenance activity

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 78.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~257 days

Total

5

Last Release

193d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1f662e3acb95efd9208cdcd2c97437db366044aea705985251760efcbd23070?d=identicon)[zozlak](/maintainers/zozlak)

---

Top Contributors

[![zozlak](https://avatars.githubusercontent.com/u/6503177?v=4)](https://github.com/zozlak "zozlak (15 commits)")[![k00ni](https://avatars.githubusercontent.com/u/381727?v=4)](https://github.com/k00ni "k00ni (4 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sweetrdf-rdfinterface2easyrdf/health.svg)

```
[![Health](https://phpackages.com/badges/sweetrdf-rdfinterface2easyrdf/health.svg)](https://phpackages.com/packages/sweetrdf-rdfinterface2easyrdf)
```

###  Alternatives

[sweetrdf/easyrdf

EasyRdf is a PHP library designed to make it easy to consume and produce RDF.

261.3M11](/packages/sweetrdf-easyrdf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
