Passed
Push — new-api ( 5677f6...2a03a6 )
by Sebastian
04:12
created

StylesRenderer::getAffixes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        https://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2020 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Styles;
12
13
use Seboettg\CiteProc\CiteProc;
14
use SimpleXMLElement;
15
16
class StylesRenderer
17
{
18
19 181
    public static function factory(SimpleXMLElement $node)
20
    {
21
        $formattingAttributes = [
22 181
            'font-style',
23
            'font-family',
24
            'font-weight',
25
            'font-variant',
26
            'text-decoration',
27
            'vertical-align'
28
        ];
29
30 181
        $prefix = $suffix = $textCase = $display = null;
31 181
        $quotes = false;
32 181
        $formatting = new FormattingRenderer();
33
34 181
        foreach ($node->attributes() as $attribute) {
35 156
            $name = $attribute->getName();
36
            switch ($name) {
37 156
                case 'prefix':
38 90
                    $prefix = (string) $attribute;
39 90
                    break;
40 156
                case 'suffix':
41 99
                    $suffix = (string) $attribute;
42 99
                    break;
43 155
                case 'text-case':
44 53
                    $textCase = new TextCase((string) $attribute);
45 53
                    break;
46 155
                case 'display':
47 4
                    $display = new Display((string )$attribute);
48 4
                    break;
49 155
                case 'quotes':
50 37
                    $quotes = "true" === (string) $attribute;
51 37
                    break;
52
                default:
53 155
                    if (in_array($attribute->getName(), $formattingAttributes)) {
54 49
                        $value = (string) $attribute;
55 156
                        $formatting->addFormattingOption($attribute->getName(), $value);
56
                    }
57
            }
58
        }
59 181
        $context = CiteProc::getContext();
60 181
        $locale = $context->getLocale();
61 181
        $affixes = AffixesRenderer::factory($context, $prefix, $suffix);
62 181
        $textCase = new TextCaseRenderer($textCase);
63 181
        $display = new DisplayRenderer($display);
64 181
        $quotes = new QuotesRenderer($quotes, $locale, $suffix);
65 181
        return new self(
66 181
            $affixes,
67 181
            $textCase,
68 181
            $display,
69 181
            $formatting,
70 181
            $quotes
71
        );
72
    }
73
74
    /** @var AffixesRenderer */
75
    private $affixes;
76
77
    /** @var TextCaseRenderer */
78
    private $textCase;
79
80
    /** @var DisplayRenderer */
81
    private $display;
82
83
    /** @var FormattingRenderer */
84
    private $formatting;
85
86
    /** @var QuotesRenderer */
87
    private $quotes;
88
89 181
    public function __construct(
90
        ?AffixesRenderer $affixes,
91
        ?TextCaseRenderer $textCase,
92
        ?DisplayRenderer $display,
93
        ?FormattingRenderer $formatting,
94
        ?QuotesRenderer $quotes
95
    ) {
96 181
        $this->affixes = $affixes;
97 181
        $this->textCase = $textCase;
98 181
        $this->display = $display;
99 181
        $this->formatting = $formatting;
100 181
        $this->quotes = $quotes;
101 181
    }
102
103 177
    public function renderAffixes(string $text): string
104
    {
105 177
        return $this->affixes->render($text);
106
    }
107
108 131
    public function renderTextCase(string $text): string
109
    {
110 131
        return $this->textCase->render($text);
111
    }
112
113 130
    public function renderDisplay(string $text): string
114
    {
115 130
        return $this->display->render($text);
116
    }
117
118 177
    public function renderFormatting(string $text): string
119
    {
120 177
        return $this->formatting->render($text);
121
    }
122
123 104
    public function renderQuotes(string $text): string
124
    {
125 104
        return $this->quotes->render($text);
126
    }
127
128 126
    public function getTextCase(): TextCaseRenderer
129
    {
130 126
        return $this->textCase;
131
    }
132
133 9
    public function getAffixes(): AffixesRenderer
134
    {
135 9
        return $this->affixes;
136
    }
137
}
138