Passed
Push — master ( dcb940...49cd7f )
by Sebastian
14:22 queued 12:58
created

AffixesTrait::initAffixesAttributes()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

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