Passed
Push — new-api ( 4bfe18...7ec1cc )
by Sebastian
05:06
created

FormattingTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 71.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 84
ccs 23
cts 32
cp 0.7188
rs 10
c 1
b 0
f 0
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initFormattingAttributes() 0 14 3
C format() 0 32 13
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Styles;
11
12
use Seboettg\Collection\ArrayList;
13
use SimpleXMLElement;
14
15
/**
16
 * Trait FormattingTrait
17
 * @package Seboettg\CiteProc\Styles
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
trait FormattingTrait
21
{
22
23
    /**
24
     * @var array
25
     */
26
    private static $formattingAttributes = [
27
        'font-style',
28
        'font-family',
29
        'font-weight',
30
        'font-variant',
31
        'text-decoration',
32
        'vertical-align'
33
    ];
34
35
    /**
36
     * @var ArrayList
37
     */
38
    private $formattingOptions;
39
40
    /**
41
     * @var bool
42
     */
43
    private $stripPeriods = false;
44
45
    /**
46
     * @var string
47
     */
48
    private $format;
49
50
    /**
51
     * @param SimpleXMLElement $node
52
     */
53 118
    protected function initFormattingAttributes(SimpleXMLElement $node)
54
    {
55 118
        $this->formattingOptions = new ArrayList();
56
57
        /** @var SimpleXMLElement $attribute */
58 118
        foreach ($node->attributes() as $attribute) {
59
60
            /** @var string $name */
61 101
            $name = (string) $attribute->getName();
62 101
            $value = (string) $attribute;
63
64 101
            if (in_array($name, self::$formattingAttributes)) {
65 3
                $this->formattingOptions->add($name, $value);
66 101
                continue;
67
            }
68
        }
69 118
    }
70
71
72 105
    protected function format($text)
73
    {
74 105
        if (empty($text)) {
75
            return $text;
76
        }
77
78 105
        if (!empty($this->formattingOptions)) {
79 105
            $format = "";
80 105
            foreach ($this->formattingOptions as $option => $optionValue) {
81 2
                if ($optionValue === "italic") {
82
                    $text = "<i>$text</i>";
83 2
                } elseif ($optionValue === "bold") {
84
                    $text = "<b>$text</b>";
85 2
                } elseif ($optionValue === "normal") {
86
                    $text = "$text";
87 2
                } elseif ($option === "vertical-align") {
88
                    if ($optionValue === "sub") {
89
                        $text = "<sub>$text</sub>";
90
                    } elseif ($optionValue === "sup") {
91
                        $text = "<sup>$text</sup>";
92
                    }
93 2
                } elseif ($option === "text-decoration" && $optionValue === "none") {
94
                    $format .= "";
95
                } else {
96 2
                    $format .= "$option: $optionValue;";
97
                }
98
            }
99 105
            if (!empty($format)) {
0 ignored issues
show
introduced by
The condition empty($format) is always true.
Loading history...
100 2
                $text = '<span style="' . $format . '">' . $text . '</span>';
101
            }
102
        }
103 105
        return $text;
104
    }
105
}
106