Completed
Push — version2.0 ( af5a57...2f739c )
by Sebastian
06:52
created

FormattingTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initFormattingAttributes() 0 17 3
C format() 0 24 7
1
<?php
2
3
namespace Seboettg\CiteProc\Styles;
4
5
6
use Seboettg\Collection\ArrayList;
7
8
trait FormattingTrait
9
{
10
11
    /**
12
     * @var array
13
     */
14
    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...
15
16
    /**
17
     * @var ArrayList
18
     */
19
    private $formattingOptions;
20
21
    /**
22
     * @var bool
23
     */
24
    private $stripPeriods = false;
25
26
    /**
27
     * @var string
28
     */
29
    private $format;
30
31
    protected function initFormattingAttributes(\SimpleXMLElement $node)
32
    {
33
        $this->formattingOptions = new ArrayList();
34
35
        /** @var \SimpleXMLElement $attribute */
36
        foreach ($node->attributes() as $attribute) {
37
38
            /** @var string $name */
39
            $name = (string)$attribute->getName();
40
            $value = (string)$attribute;
41
42
            if (in_array($name, self::$formattingAttributes)) {
43
                $this->formattingOptions->add($name, $value);
44
                continue;
45
            }
46
        }
47
    }
48
49
50
    protected function format($text)
51
    {
52
        if (empty($text)) {
53
            return $text;
54
        }
55
56
        if (!empty($this->formattingOptions)) {
57
            $format = "";
58
            foreach ($this->formattingOptions as $option => $optionValue) {
59
                if ($optionValue === "italic") {
60
                    $text = "<i>$text</i>";
61
                }
62
                else if ($optionValue === "bold") {
63
                    $text = "<b>$text</b>";
64
                } else {
65
                    $format .= "$option:$optionValue;";
66
                }
67
            }
68
            if (!empty($format)) {
69
                $text = '<span style="' . $format . '">'.$text.'</span>';
70
            }
71
        }
72
        return $text;
73
    }
74
}