Passed
Push — master ( f36a08...6258cd )
by Sebastian
12:50
created

AffixesTrait::renderPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\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 164
    protected function initAffixesAttributes(SimpleXMLElement $node)
42
    {
43
        /** @var SimpleXMLElement $attribute */
44 164
        foreach ($node->attributes() as $attribute) {
45
            /** @var string $name */
46 164
            $name = (string) $attribute->getName();
47 164
            $value = (string) $attribute;
48
49 164
            switch ($name) {
50 164
                case 'prefix':
51 84
                    $this->prefix = $value;
52 84
                    break;
53 164
                case 'suffix':
54 92
                    $this->suffix = $value;
55 92
                    break;
56 164
                case 'quote':
57
                    $this->quote = (bool) $attribute;
58
            }
59
        }
60 164
    }
61
62
    /**
63
     * @param $text
64
     * @return string
65
     */
66 160
    protected function addAffixes($text)
67
    {
68 160
        $prefix = $this->prefix;
69 160
        $suffix = $this->suffix;
70
71 160
        if (!empty($suffix)) { // guard against repeated suffixes...
72 81
            $no_tags = strip_tags($text);
73 81
            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 81
            $piq = CiteProc::getContext()
79 81
                ->getLocale()
80 81
                ->filter('options', 'punctuation-in-quote');
81 81
            $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 81
            if ($punctuationInQuote && in_array($suffix, [',', ';', '.'])) {
84 34
                $closeQuote = CiteProc::getContext()->getLocale()->filter("terms", "close-quote")->single;
85 34
                $lastChar = mb_substr($text, -1, 1);
86 34
                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 160
        return $prefix . $text . $suffix;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 147
    public function renderPrefix()
100
    {
101 147
        return $this->prefix;
102
    }
103
104
    /**
105
     * @return string
106
     */
107 147
    public function renderSuffix()
108
    {
109 147
        return $this->suffix;
110
    }
111
}
112