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

FormattingTrait::initFormattingAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 171
    protected function initFormattingAttributes(SimpleXMLElement $node)
54
    {
55 171
        $this->formattingOptions = new ArrayList();
56
57
        /** @var SimpleXMLElement $attribute */
58 171
        foreach ($node->attributes() as $attribute) {
59
60
            /** @var string $name */
61 170
            $name = (string) $attribute->getName();
62 170
            $value = (string) $attribute;
63
64 170
            if (in_array($name, self::$formattingAttributes)) {
65 44
                $this->formattingOptions->add($name, $value);
66 170
                continue;
67
            }
68
        }
69 171
    }
70
71
72 167
    protected function format($text)
73
    {
74 167
        if (empty($text)) {
75 2
            return $text;
76
        }
77
78 167
        if (!empty($this->formattingOptions)) {
79 167
            $format = "";
80 167
            foreach ($this->formattingOptions as $option => $optionValue) {
81 34
                if ($optionValue === "italic") {
82 32
                    $text = "<i>$text</i>";
83 7
                } elseif ($optionValue === "bold") {
84 5
                    $text = "<b>$text</b>";
85 3
                } elseif ($optionValue === "normal") {
86 1
                    $text = "$text";
87 3
                } elseif ($option === "vertical-align") {
88 1
                    if ($optionValue === "sub") {
89
                        $text = "<sub>$text</sub>";
90 1
                    } elseif ($optionValue === "sup") {
91 1
                        $text = "<sup>$text</sup>";
92
                    }
93 2
                } elseif ($option === "text-decoration" && $optionValue === "none") {
94
                    $format .= "";
95
                } else {
96 34
                    $format .= "$option: $optionValue;";
97
                }
98
            }
99 167
            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 167
        return $text;
104
    }
105
}
106