PHPackages                             sweetrdf/term-templates - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. sweetrdf/term-templates

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sweetrdf/term-templates
=======================

A set of Term templates for the rdfInterface

2.0.5(7mo ago)013.6k↓37.6%3MITPHPPHP &gt;=8.0CI passing

Since Mar 22Pushed 7mo ago2 watchersCompare

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

READMEChangelog (10)Dependencies (5)Versions (21)Used By (3)

termTemplates
=============

[](#termtemplates)

[![Latest Stable Version](https://camo.githubusercontent.com/5d9426a625696ba697e6bb162160a9c2ab989eadaaa94646c6f15248a570ebcb/68747470733a2f2f706f7365722e707567782e6f72672f73776565747264662f7465726d2d74656d706c617465732f762f737461626c65)](https://packagist.org/packages/sweetrdf/term-templates)[![Build status](https://github.com/sweetrdf/termTemplates/workflows/phpunit/badge.svg?branch=master)](https://github.com/sweetrdf/termTemplates/workflows/phpunit/badge.svg?branch=master)[![Coverage Status](https://camo.githubusercontent.com/6e20f26ab7a51a79021cacda558b4d4343e113515be1c5f9ea95157fab4ac999/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73776565747264662f7465726d54656d706c617465732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/sweetrdf/termTemplates?branch=master)[![License](https://camo.githubusercontent.com/5b32f974e6c87603e044565a1de05acae036f8093a53acd3503d115b7ea68435/68747470733a2f2f706f7365722e707567782e6f72672f73776565747264662f7465726d2d74656d706c617465732f6c6963656e7365)](https://packagist.org/packages/sweetrdf/term-templates)

Provides:

- A set of term templates allowing to match desired RDF named nodes, literals and quads. To be used mainly as `rdfInterface\Dataset` methods `$filter` parameter.
- A convenient methods for extracting single terms and/or their values from `rdfInterface\Dataset`.

Quad-matching classes
---------------------

[](#quad-matching-classes)

- `termTemplates\QuadTemplate`
- `termTemplates\PredicateTemplate` - a `termTemplates\QuadTemplate` variant skipping the subject (convenient for filtering the `rdfInterface\DatasetNode`)

Term-matching classes
---------------------

[](#term-matching-classes)

(all classes in the `termTemplate` namespace)

Match bySupported matchModesMatch both named nodes and literalsMatch only named nodesMatch only literalsRemarksterm's string value==, !=, &lt;, &gt;, &lt;=, &gt;=, starts, ends, contains, regex, any`ValueTemplate``NamedNodeTemplate``LiteralTemplate` \[1\]term's numeric value==, !=, &lt;, &gt;, &lt;=, &gt;=, anyNANA`NumericTemplate`\[2\]\[1\] Supports filtering also by literal's lang (using *==* and *any* operators) and datatype (only using *==* operator).

\[2\] Supports both strict and non-strict mode. In the strict mode comparison with literals with non-numeric datatype returns `false` no matter their value.

Other classes
-------------

[](#other-classes)

- `termTemplates\NotTemplate` - negates the result of the `equals()` operation on a given `rdfInterface\TermCompare` object.
- `termTemplates\AnyOfTemplate` - matches terms being equal to any of given list of `rdfInterface\TermCompare` objects. as single `rdfInterface\Term`, single value, array of `rdfInterface\Term` or array of values.

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

[](#installation)

- Obtain the [Composer](https://getcomposer.org)
- Run `composer require sweetrdf/term-templates`

Automatically generated documentation
-------------------------------------

[](#automatically-generated-documentation)

It's very incomplete but better than nothing.
[RdfInterface](https://github.com/sweetrdf/rdfInterface/) documentation is included which explains the most important design decisions.

Usage examples
--------------

[](#usage-examples)

Remarks:

- **The comparison with term templates isn't symmetric!**You should always call `$termTemplate->equals($termToBeCompared)` and never `$termToBeCompared->equals($termTemplate)`. The latter one will always return false.
- See also [here](https://github.com/sweetrdf/rdfInterface/blob/master/EasyRdfReadme.md) for examples of using term templates in conjuction with an RDF datasets.
- To run this examples an RDF terms factory is needed. Here we will use the one from the [quickRdf](https://github.com/sweetrdf/quickRdf) library. You can install it with `composer require sweetrdf/quick-rdf`.
- In the examples below the comparison operators are specified by stating corresponding `termTemplates\ValueTemplate` class constants but you can also use corresponding constants values as listed in the table above (e.g. `>=` or `regex`).
- If you are tired with the long code, define class aliases, e.g. `use termTemplates\QuadTemplate as QT;`, `use termTemplates\ValueTemplate as VT;`, etc.

### String values comparison

[](#string-values-comparison)

```
$df = new quickRdf\DataFactory();

$literals = [
    $df::literal('Lorem ipsum', 'lat'),
    $df::namedNode('http://ipsum.dolor/sit#amet'),
    $df::literal('foo bar'),
];

// find all terms containing 'ipsum'
// true, true, false
$tmplt = new termTemplates\ValueTemplate('ipsum', termTemplates\ValueTemplate::CONTAINS);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find all literals containing 'ipsum'
// true, false, false
$tmplt = new termTemplates\LiteralTemplate('ipsum', termTemplates\ValueTemplate::CONTAINS);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find all named nodes containing 'ipsum'
// false, true, false
$tmplt = new termTemplates\NamedNodeTemplate('ipsum', termTemplates\ValueTemplate::CONTAINS);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find all literals in latin
// true, false, false
$tmplt = new termTemplates\LiteralTemplate(lang: 'lat');
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find all terms with string value lower than 'http'
// true, false, true
$tmplt = new termTemplates\LiteralTemplate('http', termTemplates\ValueTemplate::LOWER);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find all terms matching a 'Lorem|foo' regular expression
// true, false, true
$tmplt = new termTemplates\ValueTemplate('/Lorem|foo/', termTemplates\ValueTemplate::REGEX);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// ValueTemplate, NamedNodeTemplate and LiteralTemplate can be passed multiple values
// In such a case condition needs to be fulfilled on any value
// true, true, false
$tmplt = new termTemplates\ValueTemplate(['Lorem ipsum', 'http://ipsum.dolor/sit#amet']);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));
```

### Numeric values comparison

[](#numeric-values-comparison)

```
$df = new quickRdf\DataFactory();

$literals = [
    $df->literal('2'),
    $df->literal(3, null, 'http://www.w3.org/2001/XMLSchema#int'),
    $df->literal('1foo'),
    $df->literal('2', null, 'http://www.w3.org/2001/XMLSchema#int'),
    $df->namedNode('http://ipsum.dolor/sit#amet'),
];

// find terms with a value of 2
// true, false, false, true, false
$tmplt = new termTemplates\NumericTemplate(2);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find terms with a value of 2 and enforce an RDF numeric type
// false, false, false, true, false
$tmplt = new termTemplates\NumericTemplate(2, strict: true);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find terms with a value greate or equal than 3
// false, true, false, false, false
$tmplt = new termTemplates\NumericTemplate(3, termTemplates\ValueTemplate::GREATER_EQUAL);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find all terms with numeric values
// true, true, false, true, false
$tmplt = new termTemplates\NumericTemplate(matchMode: termTemplates\ValueTemplate::ANY);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find all numeric terms with values not equal 2
// false, true, false, false, false
$tmplt  = new termTemplates\NumericTemplate(2, termTemplates\ValueTemplate::NOT_EQUALS);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));
```

### Negation and aggregation

[](#negation-and-aggregation)

```
$literals = [
    $df->literal('2'),
    $df->literal(3, null, 'http://www.w3.org/2001/XMLSchema#int'),
    $df->literal('1foo'),
    $df->literal('2', null, 'http://www.w3.org/2001/XMLSchema#int'),
    $df->namedNode('http://ipsum.dolor/sit#amet'),
];

// find all terms which value is not equal 2 (also non-numeric and non-literal ones)
// false, true, true, false, true
$tmplt = new termTemplates\NumericTemplate(2);
$tmplt = new termTemplates\NotTemplate($tmplt);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));

// find all terms with numeric value of 2 and all named nodes
// true, false, false, true, true
$tmplt = new termTemplates\AnyOfTemplate([
    new termTemplates\NumericTemplate(2),
    new termTemplates\NamedNodeTemplate()
]);
print_r(array_map(fn($x) => $tmplt->equals($x), $literals));
```

### Quads comparison

[](#quads-comparison)

```
$df = new quickRdf\DataFactory();

// find all quads with a literal value containing 'foo'
$tmplt = new termTemplates\QuadTemplate(object: new termTemplates\LiteralTemplate('foo', termTemplates\ValueTemplate::CONTAINS));

// find all quads with a given subject within a given graph
$tmplt = new termTemplates\QuadTemplate(
    subject: 'http://desired/subject',
    graph: 'http://desired/graph'
);

// find all quads with a given predicate
$tmplt = new termTemplates\QuadTemplate(predicate: 'http://desired/predicate');
//or
$tmplt = new termTemplates\PredicateTemplate('http://desired/predicate');

// both QuadTemplate and PredicateTemplate support negation, e.g.
// find all quads with subject different than 'http://unwanted/subject'
$tmplt = new termTemplates\QuadTemplate('http://desired/predicate', negate: true);
```

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance63

Regular maintenance activity

Popularity26

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 97% 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 ~92 days

Recently: every ~150 days

Total

19

Last Release

227d ago

Major Versions

0.6.1 → 1.0.0-RC12022-11-01

1.2.0 → 2.0.02023-09-01

### 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 (64 commits)")[![k00ni](https://avatars.githubusercontent.com/u/381727?v=4)](https://github.com/k00ni "k00ni (2 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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