Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Nov 4, 2024
1 parent 6727c13 commit f018a89
Show file tree
Hide file tree
Showing 3 changed files with 322 additions and 50 deletions.
103 changes: 83 additions & 20 deletions Classes/Processing/BibEntryConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ class BibEntryConfig
];
const BODY_FIELDS = [
[
'field' => 'title'
'field' => 'title',
'postfix' => ' '
],
[
'field' => 'shortTitle'
'field' => 'shortTitle',
'prefix' => '(',
'postfix' => ')'
],
];
const FOOTER_FIELDS = [
Expand All @@ -64,53 +67,56 @@ class BibEntryConfig
],
'field' => 'creators',
'separator' => ', ',
'postfix' => ':'
'postfix' => ': '
]],
[
'field' => 'publicationTitle',
'conditionField' => 'publicationTitle',
'conditionValue' => '',
'conditionRelation' => 'neq',
'postfix' => ','
'postfix' => ', '
],
[
'field' => 'bookTitle',
'conditionField' => 'bookTitle',
'conditionValue' => '',
'conditionRelation' => 'neq',
'postfix' => ','
'postfix' => ', '
],
[
'field' => 'university',
'conditionField' => 'university',
'conditionValue' => '',
'conditionRelation' => 'neq'
'conditionRelation' => 'neq',
'postfix' => ' '
],
[
'field' => 'volume',
'conditionField' => 'volume',
'conditionValue' => '',
'conditionRelation' => 'neq'
'conditionRelation' => 'neq',
'postfix' => ' '
],
[
'field' => 'issue',
'conditionField' => 'issue',
'conditionValue' => '',
'conditionRelation' => 'neq',
'prefix' => '(',
'postfix' => ')'
'postfix' => ') '
],
[
'field' => 'place',
'conditionField' => 'place',
'conditionValue' => '',
'conditionRelation' => 'neq'
'conditionRelation' => 'neq',
'postfix' => ' '
],
[
'field' => 'date',
'conditionField' => 'date',
'conditionValue' => '',
'conditionRelation' => 'neq'
'conditionRelation' => 'neq',
],
[
'field' => 'pages',
Expand All @@ -121,17 +127,74 @@ class BibEntryConfig
]
];
const SEARCHABLE_FIELDS = [
'author',
'title',
'university',
'bookTitle',
'series',
'publicationTitle',
'place',
'date',
'shortTitle'
[
'compound' => [
'fields' => [
[
'field' => 'firstName',
'conditionField' => 'creatorType',
'conditionValue' => 'author',
'conditionRelation' => 'eq'
],
[
'field' => 'lastName',
'conditionField' => 'creatorType',
'conditionValue' => 'author',
'conditionRelation' => 'eq'
]
],
'field' => 'creators',
'separator' => ' ',
'postfix' => ' '
]
],
[
'field' => 'title',
'postfix' => ' '
],
[
'field' => 'university',
'postfix' => ' '
],
[
'field' => 'bookTitle',
'postfix' => ' '
],
[
'field' => 'series',
'postfix' => ' '
],
[
'field' => 'publicationTitle',
'postfix' => ' '
],
[
'field' => 'place',
'postfix' => ' '
],
[
'field' => 'date',
'postfix' => ' '
]
];
const BOOSTED_FIELDS = [
'title'
[
'compound' => [
'fields' => [
[
'field' => 'lastName',
'conditionField' => 'creatorType',
'conditionValue' => 'author',
'conditionRelation' => 'eq'
]
],
'field' => 'creators',
'separator' => ' ',
'reverseFirst' => true,
'postfix' => ' '
]
],
[ 'field' => 'title' ],
[ 'field' => 'date' ]
];
}
85 changes: 55 additions & 30 deletions Classes/Processing/BibEntryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ public static function process(
$bibliographyItem['tx_lisztcommon_header'] = self::buildListingField($bibliographyItem, BibEntryConfig::HEADER_FIELDS);
$bibliographyItem['tx_lisztcommon_body'] = self::buildListingField($bibliographyItem, BibEntryConfig::BODY_FIELDS);
$bibliographyItem['tx_lisztcommon_footer'] = self::buildListingField($bibliographyItem, BibEntryConfig::FOOTER_FIELDS);
$bibliographyItem['tx_lisztcommon_searchable'] = Collection::wrap($bibliographyItem)->
filter( function ($val, $key) { return $val && in_array($key, BibEntryConfig::SEARCHABLE_FIELDS); } )->
join(' ')->
trim();
$bibliographyItem['tx_lisztcommon_boosted'] = Collection::wrap($bibliographyItem)->
filter( function ($val, $key) { return $val && in_array($key, BibEntryConfig::BOOSTED_FIELDS); } )->
join(' ')->
trim();
$bibliographyItem['tx_lisztcommon_searchable'] = self::buildListingField($bibliographyItem, BibEntryConfig::SEARCHABLE_FIELDS);
$bibliographyItem['tx_lisztcommon_boosted'] = self::buildListingField($bibliographyItem, BibEntryConfig::BOOSTED_FIELDS);

return $bibliographyItem;
}
Expand All @@ -54,12 +48,11 @@ public static function buildListingField(
{
return Collection::wrap($fieldConfig)->
map( function($field) use ($bibliographyItem) { return self::buildListingEntry($field, $bibliographyItem); })->
filter()->
join(' ')->
join('')->
trim();
}

private static function buildListingEntry(array $field, array $bibliographyItem): Stringable
private static function buildListingEntry(array $field, array $bibliographyItem): ?Stringable
{
// return empty string if field does not exist
if (
Expand All @@ -68,7 +61,7 @@ private static function buildListingEntry(array $field, array $bibliographyItem)
isset($field['compound']['field']) &&
!isset($bibliographyItem[$field['compound']['field']])
) {
return Str::of('');
return null;
}
// return empty string if conditions are not met
if (
Expand All @@ -81,54 +74,86 @@ private static function buildListingEntry(array $field, array $bibliographyItem)
$field['conditionRelation'] == 'eq' &&
$bibliographyItem[$field['conditionField']] != $field['conditionValue']
) {
return Str::of('');
return null;
}
// return empty if inequality condition is not met
if (
$field['conditionRelation'] == 'neq' &&
$bibliographyItem[$field['conditionField']] == $field['conditionValue']
) {
return Str::of('');
return null;
}
}

$fieldString = Str::of('');
if (isset($field['prefix'])) {
$fieldString = $fieldString->append($field['prefix']);
}

// build compound fields
if (isset($field['compound'])) {
// build compound fields
$compoundString = Collection::wrap($bibliographyItem[$field['compound']['field']])->
map(function ($bibliographyCell) use ($field) { return self::processCompound($field['compound'], $bibliographyCell); })->
when( isset($field['separator']), function($compoundFields) {
return $compoundFields->join($field['separator']);
// get selected strings
map(function ($bibliographyCell) use ($field) {
return self::processCompound($field['compound'], $bibliographyCell);
})->
// filter out non fitting fields
filter()->
// join fields
when( isset($field['compound']['separator']), function($compoundFields) use ($field) {
return $compoundFields->join($field['compound']['separator']);
}, function($compoundFields) {
return $compoundFields->join('');
});
$fieldString = $fieldString->append($compoundString);
$bodyString = Str::of($compoundString);
} else {
$fieldString = $fieldString->append($bibliographyItem[$field['field']]);
$bodyString = Str::of($bibliographyItem[$field['field']]);
}

if ($bodyString->isEmpty()) {
return null;
}

// prefix handling
$fieldString = Str::of('');
if (isset($field['prefix'])) {
$fieldString = $fieldString->append($field['prefix']);
}
if (isset($field['compound']['prefix'])) {
$fieldString = $fieldString->append($field['compound']['prefix']);
}

// body handling
$fieldString = $fieldString->append($bodyString);

// postfix handling
if (isset($field['postfix'])) {
$fieldString = $fieldString->append($field['postfix']);
}
if (isset($field['compound']['postfix'])) {
$fieldString = $fieldString->append($field['compound']['postfix']);
}

return $fieldString;
}

private static function processCompound(array $field, array $bibliographyCell): Stringable
private static function processCompound(array $field, array $bibliographyCell): ?Stringable
{
return Collection::wrap($field['fields'])->
$compoundString = Collection::wrap($field['fields'])->
// get selected strings
map( function ($field) use ($bibliographyCell) { return self::buildListingEntry($field, $bibliographyCell); })->
// filter out empty fields
filter()->
// conditionally reverse and join fields
when(
isset($field['reverseFirst']) &&
$field['reverseFirst'] == true,
function($compoundFields) {
return $compoundFields->reverse();
return $compoundFields->reverse()->join(', ');;
},
function($compoundFields) {
return $compoundFields->join(' ');
}
)->
join(' ');
);

if ($compoundString->isEmpty()) {
return null;
}
return $compoundString;
}
}
Loading

0 comments on commit f018a89

Please sign in to comment.