Passed
Push — new-api ( 5677f6...2a03a6 )
by Sebastian
04:12
created

AffixesTrait::addAffixes()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.0877

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 11
nop 1
dl 0
loc 28
ccs 16
cts 18
cp 0.8889
crap 8.0877
rs 8.4444
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 128
    protected function initAffixesAttributes(SimpleXMLElement $node)
42
    {
43 128
        foreach ($node->attributes() as $attribute) {
44 128
            $name = (string) $attribute->getName();
45 128
            $value = (string) $attribute;
46
47
            switch ($name) {
48 128
                case 'prefix':
49 26
                    $this->prefix = $value;
50 26
                    break;
51 128
                case 'suffix':
52 53
                    $this->suffix = $value;
53 53
                    break;
54 128
                case 'quote':
55 128
                    $this->quote = (bool) $attribute;
56
            }
57
        }
58 128
    }
59
60
    /**
61
     * @param $text
62
     * @return string
63
     */
64 119
    protected function addAffixes($text)
65
    {
66 119
        $prefix = $this->prefix;
67 119
        $suffix = $this->suffix;
68
69 119
        if (!empty($suffix)) { // guard against repeated suffixes...
70 22
            $no_tags = strip_tags($text);
71 22
            if (strlen($no_tags) && ($no_tags[(strlen($no_tags) - 1)] == $suffix[0])) {
72 1
                $suffix = substr($suffix, 1);
73
            }
74
75
            // punctuation in quote?
76 22
            $piq = CiteProc::getContext()
77 22
                ->getLocale()
78 22
                ->filter('options', 'punctuation-in-quote');
79 22
            $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 22
            if ($punctuationInQuote && in_array($suffix, [',', ';', '.'])) {
82 6
                $closeQuote = CiteProc::getContext()->getLocale()->filter("terms", "close-quote")->single;
83 6
                $lastChar = mb_substr($text, -1, 1);
84 6
                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 119
        return $prefix . $text . $suffix;
92
    }
93
94
    /**
95
     * @return string
96
     */
97 120
    public function renderPrefix()
98
    {
99 120
        return $this->prefix;
100
    }
101
102
    /**
103
     * @return string
104
     */
105 120
    public function renderSuffix()
106
    {
107 120
        return $this->suffix;
108
    }
109
}
110