Passed
Push — new-api ( 2a03a6...15a925 )
by Sebastian
04:45
created

FormattingRenderer::factory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 3
rs 9.7998
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\Collection\ArrayList;
14
use SimpleXMLElement;
15
16
final class FormattingRenderer implements StylesRendererInterface
17
{
18
19 14
    public static function factory(SimpleXMLElement $node)
20
    {
21
        $formattingAttributes = [
22 14
            'font-style',
23
            'font-family',
24
            'font-weight',
25
            'font-variant',
26
            'text-decoration',
27
            'vertical-align'
28
        ];
29 14
        $instance = new self();
30 14
        foreach ($node->attributes() as $attribute) {
31 14
            $name = (string) $attribute->getName();
32 14
            $value = (string) $attribute;
33 14
            if (in_array($name, $formattingAttributes)) {
34 14
                $instance->addFormattingOption($name, $value);
35
            }
36
        }
37 14
        return $instance;
38
    }
39
40
    /**
41
     * @var ArrayList
42
     */
43
    private $formattingOptions;
44
45 182
    public function __construct()
46
    {
47 182
        $this->formattingOptions = new ArrayList();
48 182
    }
49
50 51
    public function addFormattingOption($option, $optionValue)
51
    {
52 51
        $this->formattingOptions->add($option, $optionValue);
53 51
    }
54
55 178
    public function render(?string $text): string
56
    {
57 178
        if (empty($text)) {
58 1
            return $text;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $text returns the type null which is incompatible with the type-hinted return string.
Loading history...
59
        }
60
61 178
        if (!empty($this->formattingOptions)) {
62 178
            $format = [];
63 178
            foreach ($this->formattingOptions as $option => $optionValue) {
64
                switch ($optionValue) {
65 41
                    case "italic":
66 36
                        $text = sprintf("<i>%s</i>", $text);
67 36
                        break;
68 11
                    case "bold":
69 6
                        $text = sprintf("<b>%s</b>", $text);
70 6
                        break;
71 7
                    case "normal":
72 4
                        break;
73
                    default:
74 6
                        if ($option === "vertical-align") {
75 1
                            if ($optionValue === "sub") {
76 1
                                $text = sprintf("<sub>%s</sub>", $text);
77 1
                            } elseif ($optionValue === "sup") {
78 1
                                $text = sprintf("<sup>%s</sup>", $text);
79
                            }
80
                        } else {
81 5
                            if ($option !== "text-decoration" || $optionValue !== "none") {
82 41
                                $format[] = "$option: $optionValue";
83
                            }
84
                        }
85
                }
86
            }
87 178
            if (!empty($format)) {
88 5
                $text = sprintf('<span style="%s">%s</span>', implode(";", $format), $text);
89
            }
90
        }
91 178
        return $text;
92
    }
93
}
94