Passed
Push — master ( e13826...b82956 )
by Sebastian
16:47 queued 09:52
created

FormattingTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initFormattingAttributes() 0 17 3
C format() 0 23 7
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
13
use Seboettg\Collection\ArrayList;
14
15
trait FormattingTrait
16
{
17
18
    /**
19
     * @var array
20
     */
21
    static $formattingAttributes = ['font-style', 'font-family', 'font-weight', 'font-variant', 'text-decoration', 'vertical-align'];
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $formattingAttributes.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
22
23
    /**
24
     * @var ArrayList
25
     */
26
    private $formattingOptions;
27
28
    /**
29
     * @var bool
30
     */
31
    private $stripPeriods = false;
32
33
    /**
34
     * @var string
35
     */
36
    private $format;
37
38
    protected function initFormattingAttributes(\SimpleXMLElement $node)
39
    {
40
        $this->formattingOptions = new ArrayList();
41
42
        /** @var \SimpleXMLElement $attribute */
43
        foreach ($node->attributes() as $attribute) {
44
45
            /** @var string $name */
46
            $name = (string) $attribute->getName();
47
            $value = (string) $attribute;
48
49
            if (in_array($name, self::$formattingAttributes)) {
50
                $this->formattingOptions->add($name, $value);
51
                continue;
52
            }
53
        }
54
    }
55
56
57
    protected function format($text)
58
    {
59
        if (empty($text)) {
60
            return $text;
61
        }
62
63
        if (!empty($this->formattingOptions)) {
64
            $format = "";
65
            foreach ($this->formattingOptions as $option => $optionValue) {
66
                if ($optionValue === "italic") {
67
                    $text = "<i>$text</i>";
68
                } else if ($optionValue === "bold") {
69
                    $text = "<b>$text</b>";
70
                } else {
71
                    $format .= "$option:$optionValue;";
72
                }
73
            }
74
            if (!empty($format)) {
75
                $text = '<span style="' . $format . '">' . $text . '</span>';
76
            }
77
        }
78
        return $text;
79
    }
80
}