Skip to content

Commit

Permalink
TemplateMetadata: add support for notMatch attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Sep 3, 2024
1 parent 8c78c09 commit d7469da
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.phpunit.cache
/config
/log
/harvestoaipmh.php
13 changes: 13 additions & 0 deletions doc/TemplateMetadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ For the complete reference of the replace expression syntax please look [here](h
The `match` annotation is passed as the `$pattern`, the `replace` annotation as the `$replacement`
and the RDF property value as the `$subject`.

### notMatch, notMatch0, notMatch1, ...

`regular expression`

Skips values which match a given regular expression,
e.g. `^foo` skips all values which begin with `foo`.

The regular expression is evaluted with the `umsD` [modifiers](https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php).

For the complete reference of the regular expressions syntax supported please look [here](https://www.php.net/manual/en/reference.pcre.pattern.syntax.php).

Can be combined with `match`. In such a case both conditions must be fulfilled.

### format, format0, format1, ...

`[DUbcdeEfFgGhHosuxX]:{format-specific parameters}`
Expand Down
8 changes: 8 additions & 0 deletions src/acdhOeaw/arche/oaipmh/metadata/util/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ static public function fromDomElement(DOMElement $el, string $suffix = ''): self
$x->match = '`' . $el->getAttribute('match' . $suffix) . '`umsD';
$el->removeAttribute('match' . $suffix);
}
if ($el->hasAttribute('notMatch' . $suffix)) {
$x->notMatch = '`' . $el->getAttribute('notMatch' . $suffix) . '`umsD';
$el->removeAttribute('notMatch' . $suffix);
}
$x->replace = $el->getAttribute('replace' . $suffix);
$el->removeAttribute('replace' . $suffix);
if ($el->hasAttribute('format' . $suffix)) {
Expand Down Expand Up @@ -137,6 +141,7 @@ static public function fromDomElement(DOMElement $el, string $suffix = ''): self

public string $path;
public string $match;
public string $notMatch;
public string $replace;
public string $format;
public string $map;
Expand Down Expand Up @@ -167,6 +172,9 @@ public function setValues(array $values, ValueMapper $mapper): void {
$valueLangs = array_map(fn($x) => $x instanceof LiteralInterface ? $x->getLang() : '', $values);

$values = array_map(fn($x) => (string) $x, $values);
if (!empty($this->notMatch)) {
$values = array_filter($values, fn($x) => !preg_match($this->notMatch, $x));
}
if (!empty($this->match)) {
$values = array_filter($values, fn($x) => preg_match($this->match, $x));
if (!empty($this->replace)) {
Expand Down
1 change: 1 addition & 0 deletions tests/TemplateMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public function testValTransform(): void {
<n xml:lang="de">Rede</n>
<o>3</o>
<p xml:lang="en">foo</p>
<q>https://baz</q>
</root>
OUT;
$this->assertEquals($this->std($expected), $xml);
Expand Down
1 change: 1 addition & 0 deletions tests/data/valTransform.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<n val1="/base:category" map1="/skos:prefLabel" lang1="overwrite"/>
<o val="/base:number" aggregate="min"/>
<p val="/base:label" aggregate="min,en" lang="overwrite"/>
<q val="/base:id" notMatch="foo" match="(.)ar" replace="\1az"/>
</root>

0 comments on commit d7469da

Please sign in to comment.