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

AffixesTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 25.71%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 88
ccs 9
cts 35
cp 0.2571
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 initAffixesAttributes() 0 15 5
A renderPrefix() 0 3 1
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 118
    protected function initAffixesAttributes(SimpleXMLElement $node)
42
    {
43 118
        foreach ($node->attributes() as $attribute) {
44 101
            $name = (string) $attribute->getName();
45 101
            $value = (string) $attribute;
46
47
            switch ($name) {
48 101
                case 'prefix':
49
                    $this->prefix = $value;
50
                    break;
51 101
                case 'suffix':
52
                    $this->suffix = $value;
53
                    break;
54 101
                case 'quote':
55 101
                    $this->quote = (bool) $attribute;
56
            }
57
        }
58 118
    }
59
60
    /**
61
     * @param $text
62
     * @return string
63
     */
64
    protected function addAffixes($text)
65
    {
66
        $prefix = $this->prefix;
67
        $suffix = $this->suffix;
68
69
        if (!empty($suffix)) { // guard against repeated suffixes...
70
            $no_tags = strip_tags($text);
71
            if (strlen($no_tags) && ($no_tags[(strlen($no_tags) - 1)] == $suffix[0])) {
72
                $suffix = substr($suffix, 1);
73
            }
74
75
            // punctuation in quote?
76
            $piq = CiteProc::getContext()
77
                ->getLocale()
78
                ->filter('options', 'punctuation-in-quote');
79
            $punctuationInQuote = is_array($piq) ? current($piq) : $piq;
0 ignored issues
show
introduced by
The condition is_array($piq) is always false.
Loading history...
80
81
            if ($punctuationInQuote && in_array($suffix, [',', ';', '.'])) {
82
                $closeQuote = CiteProc::getContext()->getLocale()->filter("terms", "close-quote")->single;
83
                $lastChar = mb_substr($text, -1, 1);
84
                if ($closeQuote === $lastChar) { // last char is closing quote?
85
                    $text = mb_substr($text, 0, mb_strlen($text) - 1); //set suffix before
86
                    return $prefix . $text . $suffix . $lastChar;
87
                }
88
            }
89
        }
90
91
        return $prefix . $text . $suffix;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function renderPrefix()
98
    {
99
        return $this->prefix;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function renderSuffix()
106
    {
107
        return $this->suffix;
108
    }
109
}
110