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

Commit 877a356

Browse files
Combine simplified visitors (#58)
* Combine simplified visitors * Rename the method now * Try and make clearer
1 parent 5d8b471 commit 877a356

24 files changed

+226
-215
lines changed

composer.lock

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,39 @@
66

77
use FluentDOM\DOM\Element;
88
use Libero\ViewsBundle\Views\ConvertsChildren;
9-
use Libero\ViewsBundle\Views\SimplifiedChildVisitor;
9+
use Libero\ViewsBundle\Views\OptionalTemplateVisitor;
1010
use Libero\ViewsBundle\Views\View;
1111
use Libero\ViewsBundle\Views\ViewConverter;
1212
use Libero\ViewsBundle\Views\ViewConverterVisitor;
13+
use function Libero\ViewsBundle\array_has_key;
1314

1415
final class BoldVisitor implements ViewConverterVisitor
1516
{
1617
use ConvertsChildren;
17-
use SimplifiedChildVisitor;
18+
use OptionalTemplateVisitor;
1819

1920
public function __construct(ViewConverter $converter)
2021
{
2122
$this->converter = $converter;
2223
}
2324

24-
protected function doVisit(Element $object, View $view) : View
25+
protected function handle(Element $object, View $view) : View
2526
{
2627
return $view->withArgument('text', $this->convertChildren($object, $view->getContext()));
2728
}
2829

29-
protected function possibleTemplate() : string
30+
protected function template() : string
3031
{
3132
return '@LiberoPatterns/bold.html.twig';
3233
}
3334

34-
protected function expectedElement() : string
35+
protected function canHandleElement(string $element) : bool
3536
{
36-
return '{http://jats.nlm.nih.gov}bold';
37+
return '{http://jats.nlm.nih.gov}bold' === $element;
3738
}
3839

39-
protected function unexpectedArguments() : array
40+
protected function canHandleArguments(array $arguments) : bool
4041
{
41-
return ['text'];
42+
return !array_has_key($arguments, 'text');
4243
}
4344
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Libero\ViewsBundle\Views\View;
1010
use Libero\ViewsBundle\Views\ViewConverter;
1111
use Libero\ViewsBundle\Views\ViewConverterVisitor;
12+
use function Libero\ViewsBundle\array_has_key;
1213

1314
final class FrontArticleTitleContentHeaderVisitor implements ViewConverterVisitor
1415
{
@@ -21,7 +22,7 @@ public function __construct(ViewConverter $converter)
2122
$this->converter = $converter;
2223
}
2324

24-
protected function doVisit(Element $object, View $view) : View
25+
protected function handle(Element $object, View $view) : View
2526
{
2627
$title = $object->ownerDocument->xpath()
2728
->firstOf('jats:article-meta/jats:title-group/jats:article-title', $object);
@@ -36,18 +37,18 @@ protected function doVisit(Element $object, View $view) : View
3637
);
3738
}
3839

39-
protected function expectedTemplate() : string
40+
protected function canHandleTemplate(?string $template) : bool
4041
{
41-
return '@LiberoPatterns/content-header.html.twig';
42+
return '@LiberoPatterns/content-header.html.twig' === $template;
4243
}
4344

44-
protected function expectedElement() : array
45+
protected function canHandleElement(string $element) : bool
4546
{
46-
return ['{http://jats.nlm.nih.gov}front'];
47+
return '{http://jats.nlm.nih.gov}front' === $element;
4748
}
4849

49-
protected function unexpectedArguments() : array
50+
protected function canHandleArguments(array $arguments) : bool
5051
{
51-
return ['contentTitle'];
52+
return !array_has_key($arguments, 'contentTitle');
5253
}
5354
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function array_map;
1616
use function array_values;
1717
use function count;
18+
use function Libero\ViewsBundle\array_has_key;
1819

1920
final class FrontItemTagsVisitor implements ViewConverterVisitor
2021
{
@@ -26,7 +27,7 @@ public function __construct(ViewConverter $converter)
2627
$this->converter = $converter;
2728
}
2829

29-
protected function doVisit(Element $object, View $view) : View
30+
protected function handle(Element $object, View $view) : View
3031
{
3132
/** @var DOMNodeList|Element[] $keywordGroups */
3233
$keywordGroups = $object->ownerDocument->xpath()
@@ -54,18 +55,18 @@ function (View $tagList) : array {
5455
return $view->withArgument('groups', $groups);
5556
}
5657

57-
protected function expectedTemplate() : string
58+
protected function canHandleTemplate(?string $template) : bool
5859
{
59-
return '@LiberoPatterns/item-tags.html.twig';
60+
return '@LiberoPatterns/item-tags.html.twig' === $template;
6061
}
6162

62-
protected function expectedElement() : array
63+
protected function canHandleElement(string $element) : bool
6364
{
64-
return ['{http://jats.nlm.nih.gov}front'];
65+
return '{http://jats.nlm.nih.gov}front' === $element;
6566
}
6667

67-
protected function unexpectedArguments() : array
68+
protected function canHandleArguments(array $arguments) : bool
6869
{
69-
return ['groups'];
70+
return !array_has_key($arguments, 'groups');
7071
}
7172
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Contracts\Translation\TranslatorInterface;
1616
use function array_map;
1717
use function count;
18+
use function Libero\ViewsBundle\array_has_key;
1819

1920
final class FrontSubjectGroupContentHeaderVisitor implements ViewConverterVisitor
2021
{
@@ -31,7 +32,7 @@ public function __construct(ViewConverter $converter, TranslatorInterface $trans
3132
$this->translator = $translator;
3233
}
3334

34-
protected function doVisit(Element $object, View $view) : View
35+
protected function handle(Element $object, View $view) : View
3536
{
3637
/** @var DOMNodeList|Element[] $subjects */
3738
$subjects = $object->ownerDocument->xpath()->evaluate(
@@ -62,18 +63,18 @@ function (View $link) : array {
6263
);
6364
}
6465

65-
protected function expectedTemplate() : string
66+
protected function canHandleTemplate(?string $template) : bool
6667
{
67-
return '@LiberoPatterns/content-header.html.twig';
68+
return '@LiberoPatterns/content-header.html.twig' === $template;
6869
}
6970

70-
protected function expectedElement() : array
71+
protected function canHandleElement(string $element) : bool
7172
{
72-
return ['{http://jats.nlm.nih.gov}front'];
73+
return '{http://jats.nlm.nih.gov}front' === $element;
7374
}
7475

75-
protected function unexpectedArguments() : array
76+
protected function canHandleArguments(array $arguments) : bool
7677
{
77-
return ['categories'];
78+
return !array_has_key($arguments, 'categories');
7879
}
7980
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Libero\ViewsBundle\Views\View;
1111
use Libero\ViewsBundle\Views\ViewConverter;
1212
use Libero\ViewsBundle\Views\ViewConverterVisitor;
13+
use function Libero\ViewsBundle\array_has_key;
14+
use function Libero\ViewsBundle\string_is;
1315

1416
final class HeadingVisitor implements ViewConverterVisitor
1517
{
@@ -21,7 +23,7 @@ public function __construct(ViewConverter $converter)
2123
$this->converter = $converter;
2224
}
2325

24-
protected function doVisit(Element $object, View $view) : View
26+
protected function handle(Element $object, View $view) : View
2527
{
2628
if ($view->hasContext('level')) {
2729
$view = $view->withArgument('level', $view->getContext('level'));
@@ -30,21 +32,18 @@ protected function doVisit(Element $object, View $view) : View
3032
return $view->withArgument('text', $this->convertChildren($object, $view->getContext()));
3133
}
3234

33-
protected function expectedTemplate() : string
35+
protected function canHandleTemplate(?string $template) : bool
3436
{
35-
return '@LiberoPatterns/heading.html.twig';
37+
return '@LiberoPatterns/heading.html.twig' === $template;
3638
}
3739

38-
protected function expectedElement() : array
40+
protected function canHandleElement(string $element) : bool
3941
{
40-
return [
41-
'{http://jats.nlm.nih.gov}article-title',
42-
'{http://jats.nlm.nih.gov}title',
43-
];
42+
return string_is($element, '{http://jats.nlm.nih.gov}article-title', '{http://jats.nlm.nih.gov}title');
4443
}
4544

46-
protected function unexpectedArguments() : array
45+
protected function canHandleArguments(array $arguments) : bool
4746
{
48-
return ['text'];
47+
return !array_has_key($arguments, 'text');
4948
}
5049
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,39 @@
66

77
use FluentDOM\DOM\Element;
88
use Libero\ViewsBundle\Views\ConvertsChildren;
9-
use Libero\ViewsBundle\Views\SimplifiedChildVisitor;
9+
use Libero\ViewsBundle\Views\OptionalTemplateVisitor;
1010
use Libero\ViewsBundle\Views\View;
1111
use Libero\ViewsBundle\Views\ViewConverter;
1212
use Libero\ViewsBundle\Views\ViewConverterVisitor;
13+
use function Libero\ViewsBundle\array_has_key;
1314

1415
final class ItalicVisitor implements ViewConverterVisitor
1516
{
1617
use ConvertsChildren;
17-
use SimplifiedChildVisitor;
18+
use OptionalTemplateVisitor;
1819

1920
public function __construct(ViewConverter $converter)
2021
{
2122
$this->converter = $converter;
2223
}
2324

24-
protected function doVisit(Element $object, View $view) : View
25+
protected function handle(Element $object, View $view) : View
2526
{
2627
return $view->withArgument('text', $this->convertChildren($object, $view->getContext()));
2728
}
2829

29-
protected function possibleTemplate() : string
30+
protected function template() : string
3031
{
3132
return '@LiberoPatterns/italic.html.twig';
3233
}
3334

34-
protected function expectedElement() : string
35+
protected function canHandleElement(string $element) : bool
3536
{
36-
return '{http://jats.nlm.nih.gov}italic';
37+
return '{http://jats.nlm.nih.gov}italic' === $element;
3738
}
3839

39-
protected function unexpectedArguments() : array
40+
protected function canHandleArguments(array $arguments) : bool
4041
{
41-
return ['text'];
42+
return !array_has_key($arguments, 'text');
4243
}
4344
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Contracts\Translation\TranslatorInterface;
1616
use function array_map;
1717
use function count;
18+
use function Libero\ViewsBundle\array_has_key;
1819

1920
final class KeywordGroupTagListVisitor implements ViewConverterVisitor
2021
{
@@ -31,7 +32,7 @@ public function __construct(ViewConverter $converter, TranslatorInterface $trans
3132
$this->translationKeys = $translationKeys;
3233
}
3334

34-
protected function doVisit(Element $object, View $view) : View
35+
protected function handle(Element $object, View $view) : View
3536
{
3637
$title = $object->ownerDocument->xpath()
3738
->firstOf('jats:title', $object);
@@ -69,18 +70,18 @@ function (View $link) : array {
6970
);
7071
}
7172

72-
protected function expectedTemplate() : string
73+
protected function canHandleTemplate(?string $template) : bool
7374
{
74-
return '@LiberoPatterns/tag-list.html.twig';
75+
return '@LiberoPatterns/tag-list.html.twig' === $template;
7576
}
7677

77-
protected function expectedElement() : array
78+
protected function canHandleElement(string $element) : bool
7879
{
79-
return ['{http://jats.nlm.nih.gov}kwd-group'];
80+
return '{http://jats.nlm.nih.gov}kwd-group' === $element;
8081
}
8182

82-
protected function unexpectedArguments() : array
83+
protected function canHandleArguments(array $arguments) : bool
8384
{
84-
return ['list'];
85+
return !array_has_key($arguments, 'list');
8586
}
8687
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Libero\ViewsBundle\Views\View;
1111
use Libero\ViewsBundle\Views\ViewConverter;
1212
use Libero\ViewsBundle\Views\ViewConverterVisitor;
13+
use function Libero\ViewsBundle\array_has_key;
14+
use function Libero\ViewsBundle\string_is;
1315

1416
final class LinkVisitor implements ViewConverterVisitor
1517
{
@@ -23,26 +25,23 @@ public function __construct(ViewConverter $converter)
2325
$this->converter = $converter;
2426
}
2527

26-
protected function doVisit(Element $object, View $view) : View
28+
protected function handle(Element $object, View $view) : View
2729
{
2830
return $view->withArgument('text', $this->convertChildren($object, $view->getContext()));
2931
}
3032

31-
protected function expectedTemplate() : string
33+
protected function canHandleTemplate(?string $template) : bool
3234
{
33-
return '@LiberoPatterns/link.html.twig';
35+
return '@LiberoPatterns/link.html.twig' === $template;
3436
}
3537

36-
protected function expectedElement() : array
38+
protected function canHandleElement(string $element) : bool
3739
{
38-
return [
39-
'{http://jats.nlm.nih.gov}kwd',
40-
'{http://jats.nlm.nih.gov}subject',
41-
];
40+
return string_is($element, '{http://jats.nlm.nih.gov}kwd', '{http://jats.nlm.nih.gov}subject');
4241
}
4342

44-
protected function unexpectedArguments() : array
43+
protected function canHandleArguments(array $arguments) : bool
4544
{
46-
return ['text'];
45+
return !array_has_key($arguments, 'text');
4746
}
4847
}

0 commit comments

Comments
 (0)