Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit 3964dcd

Browse files
Allow JATS keyword groups with only a title (#48)
* Allow JATS keyword groups with only a title * Always array_values()
1 parent 6c03197 commit 3964dcd

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed

vendor-extra/ContentPageBundle/tests/ViewConvertingTestCase.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,24 @@ function () : View {
3232
}
3333
);
3434
}
35+
36+
final protected function createFilteringConverter(ViewConverter $converter, callable $filter) : ViewConverter
37+
{
38+
return new CallbackViewConverter(
39+
function (
40+
NonDocumentTypeChildNode $node,
41+
?string $template = null,
42+
array $context = []
43+
) use (
44+
$converter,
45+
$filter
46+
) : View {
47+
if (false === $filter($node, $template, $context)) {
48+
return new View(null);
49+
}
50+
51+
return $converter->convert($node, $template, $context);
52+
}
53+
);
54+
}
3555
}

vendor-extra/JatsContentBundle/src/ViewConverter/FrontItemTagsVisitor.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Libero\ViewsBundle\Views\View;
1212
use Libero\ViewsBundle\Views\ViewConverter;
1313
use Libero\ViewsBundle\Views\ViewConverterVisitor;
14+
use function array_filter;
1415
use function array_map;
16+
use function array_values;
1517
use function count;
1618

1719
final class FrontItemTagsVisitor implements ViewConverterVisitor
@@ -28,21 +30,28 @@ protected function doVisit(Element $object, View $view) : View
2830
{
2931
/** @var DOMNodeList|Element[] $keywordGroups */
3032
$keywordGroups = $object->ownerDocument->xpath()
31-
->evaluate('jats:article-meta/jats:kwd-group[@kwd-group-type]', $object);
33+
->evaluate('jats:article-meta/jats:kwd-group', $object);
3234

3335
if (0 === count($keywordGroups)) {
3436
return $view;
3537
}
3638

37-
return $view->withArgument(
38-
'groups',
39-
array_map(
40-
function (View $tagList) : array {
41-
return $tagList->getArguments();
42-
},
43-
$this->convertList($keywordGroups, '@LiberoPatterns/tag-list.html.twig', $view->getContext())
39+
$groups = array_values(
40+
array_filter(
41+
array_map(
42+
function (View $tagList) : array {
43+
return $tagList->getArguments();
44+
},
45+
$this->convertList($keywordGroups, '@LiberoPatterns/tag-list.html.twig', $view->getContext())
46+
)
4447
)
4548
);
49+
50+
if (0 === count($groups)) {
51+
return $view;
52+
}
53+
54+
return $view->withArgument('groups', $groups);
4655
}
4756

4857
protected function expectedTemplate() : string

vendor-extra/JatsContentBundle/tests/ViewConverter/FrontItemTagsVisitorTest.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace tests\Libero\JatsContentBundle\ViewConverter;
66

7+
use FluentDOM\DOM\Element;
8+
use FluentDOM\DOM\Node\NonDocumentTypeChildNode;
79
use Libero\JatsContentBundle\ViewConverter\FrontItemTagsVisitor;
810
use Libero\ViewsBundle\Views\View;
911
use PHPUnit\Framework\TestCase;
@@ -111,9 +113,16 @@ public function it_does_nothing_if_there_is_already_groups_set() : void
111113
/**
112114
* @test
113115
*/
114-
public function it_sets_the_groups_argument_with_groups_with_a_type() : void
116+
public function it_sets_the_groups_argument_with_converted_kwd_groups() : void
115117
{
116-
$visitor = new FrontItemTagsVisitor($this->createDumpingConverter());
118+
$visitor = new FrontItemTagsVisitor(
119+
$this->createFilteringConverter(
120+
$this->createDumpingConverter(),
121+
function (NonDocumentTypeChildNode $node, ?string $template = null, array $context = []) : bool {
122+
return $node instanceof Element && $node->hasAttribute('kwd-group-type');
123+
}
124+
)
125+
);
117126

118127
$element = $this->loadElement(
119128
<<<XML
@@ -157,4 +166,45 @@ public function it_sets_the_groups_argument_with_groups_with_a_type() : void
157166
);
158167
$this->assertSame(['qux' => 'quux'], $view->getContext());
159168
}
169+
170+
/**
171+
* @test
172+
*/
173+
public function it_does_not_set_the_groups_argument_if_no_kwd_groups_convert() : void
174+
{
175+
$visitor = new FrontItemTagsVisitor(
176+
$this->createFilteringConverter(
177+
$this->createDumpingConverter(),
178+
function (NonDocumentTypeChildNode $node, ?string $template = null, array $context = []) : bool {
179+
return false;
180+
}
181+
)
182+
);
183+
184+
$element = $this->loadElement(
185+
<<<XML
186+
<?xml version="1.0" encoding="UTF-8"?>
187+
<jats:front xmlns:jats="http://jats.nlm.nih.gov">
188+
<jats:article-meta>
189+
<jats:kwd-group kwd-group-type="foo">
190+
<jats:kwd>foo</jats:kwd>
191+
</jats:kwd-group>
192+
<jats:kwd-group>
193+
<jats:kwd>bar</jats:kwd>
194+
</jats:kwd-group>
195+
<jats:kwd-group kwd-group-type="baz">
196+
<jats:kwd>baz</jats:kwd>
197+
</jats:kwd-group>
198+
</jats:article-meta>
199+
</jats:front>
200+
XML
201+
);
202+
203+
$context = ['qux' => 'quux'];
204+
$view = $visitor->visit($element, new View('@LiberoPatterns/item-tags.html.twig', [], $context));
205+
206+
$this->assertSame('@LiberoPatterns/item-tags.html.twig', $view->getTemplate());
207+
$this->assertEmpty($view->getArguments());
208+
$this->assertSame(['qux' => 'quux'], $view->getContext());
209+
}
160210
}

0 commit comments

Comments
 (0)