Passed
Push — new-api ( e6ef7d...34a0a9 )
by Sebastian
04:34
created

FormattingRenderer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 97.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 76
ccs 38
cts 39
cp 0.9744
rs 10
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 19 3
A __construct() 0 3 1
A addFormattingOption() 0 3 1
C render() 0 37 13
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 StyleRendererInterface
17
{
18
19 78
    public static function factory(SimpleXMLElement $node)
20
    {
21
        $formattingAttributes = [
22 78
            'font-style',
23
            'font-family',
24
            'font-weight',
25
            'font-variant',
26
            'text-decoration',
27
            'vertical-align'
28
        ];
29 78
        $instance = new self();
30 78
        foreach ($node->attributes() as $attribute) {
31 77
            $name = (string) $attribute->getName();
32 77
            $value = (string) $attribute;
33 77
            if (in_array($name, $formattingAttributes)) {
34 77
                $instance->addFormattingOption($name, $value);
35
            }
36
        }
37 78
        return $instance;
38
    }
39
40
    /**
41
     * @var ArrayList
42
     */
43
    private $formattingOptions;
44
45 78
    public function __construct()
46
    {
47 78
        $this->formattingOptions = new ArrayList();
48 78
    }
49
50 10
    public function addFormattingOption($option, $optionValue)
51
    {
52 10
        $this->formattingOptions->add($option, $optionValue);
53 10
    }
54
55 36
    public function render(string $text): string
56
    {
57 36
        if (empty($text)) {
58
            return $text;
59
        }
60
61 36
        if (!empty($this->formattingOptions)) {
62 36
            $format = [];
63 36
            foreach ($this->formattingOptions as $option => $optionValue) {
64
                switch ($optionValue) {
65 7
                    case "italic":
66 2
                        $text = sprintf("<i>%s</i>", $text);
67 2
                        break;
68 6
                    case "bold":
69 1
                        $text = sprintf("<b>%s</b>", $text);
70 1
                        break;
71 6
                    case "normal":
72 3
                        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 7
                                $format[] = "$option: $optionValue";
83
                            }
84
                        }
85
                }
86
            }
87 36
            if (!empty($format)) {
88 5
                $text = sprintf('<span style="%s">%s</span>', implode(";", $format), $text);
89
            }
90
        }
91 36
        return $text;
92
    }
93
}
94