Passed
Push — master ( 9180f1...3c41f4 )
by Sebastian
07:32 queued 03:42
created

AffixesTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 90
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
B addAffixes() 0 28 8
A renderSuffix() 0 3 1
A renderPrefix() 0 3 1
A initAffixesAttributes() 0 17 5
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\CiteProc\CiteProc;
13
use SimpleXMLElement;
14
15
/**
16
 * Trait AffixesTrait
17
 * @package Seboettg\CiteProc\Styles
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
trait AffixesTrait
21
{
22
23
    /**
24
     * @var string
25
     */
26
    private $prefix = "";
27
28
    /**
29
     * @var string
30
     */
31
    private $suffix = "";
32
33
    /**
34
     * @var bool
35
     */
36
    private $quote = false;
37
38
    /**
39
     * @param SimpleXMLElement $node
40
     */
41 170
    protected function initAffixesAttributes(SimpleXMLElement $node)
42
    {
43
        /** @var SimpleXMLElement $attribute */
44 170
        foreach ($node->attributes() as $attribute) {
45
            /** @var string $name */
46 170
            $name = (string) $attribute->getName();
47 170
            $value = (string) $attribute;
48
49
            switch ($name) {
50 170
                case 'prefix':
51 87
                    $this->prefix = $value;
52 87
                    break;
53 170
                case 'suffix':
54 96
                    $this->suffix = $value;
55 96
                    break;
56 170
                case 'quote':
57 170
                    $this->quote = (bool) $attribute;
58
            }
59
        }
60 170
    }
61
62
    /**
63
     * @param $text
64
     * @return string
65
     */
66 166
    protected function addAffixes($text)
67
    {
68 166
        $prefix = $this->prefix;
69 166
        $suffix = $this->suffix;
70
71 166
        if (!empty($suffix)) { // guard against repeated suffixes...
72 85
            $no_tags = strip_tags($text);
73 85
            if (strlen($no_tags) && ($no_tags[(strlen($no_tags) - 1)] == $suffix[0])) {
74 9
                $suffix = substr($suffix, 1);
75
            }
76
77
            // punctuation in quote?
78 85
            $piq = CiteProc::getContext()
79 85
                ->getLocale()
80 85
                ->filter('options', 'punctuation-in-quote');
81 85
            $punctuationInQuote = is_array($piq) ? current($piq) : $piq;
0 ignored issues
show
introduced by
The condition is_array($piq) is always false.
Loading history...
82
83 85
            if ($punctuationInQuote && in_array($suffix, [',', ';', '.'])) {
84 35
                $closeQuote = CiteProc::getContext()->getLocale()->filter("terms", "close-quote")->single;
85 35
                $lastChar = mb_substr($text, -1, 1);
86 35
                if ($closeQuote === $lastChar) { // last char is closing quote?
87 2
                    $text = mb_substr($text, 0, mb_strlen($text) - 1); //set suffix before
88 2
                    return $text . $suffix . $lastChar;
89
                }
90
            }
91
        }
92
93 166
        return $prefix . $text . $suffix;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 152
    public function renderPrefix()
100
    {
101 152
        return $this->prefix;
102
    }
103
104
    /**
105
     * @return string
106
     */
107 152
    public function renderSuffix()
108
    {
109 152
        return $this->suffix;
110
    }
111
}
112