AffixesTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

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