Passed
Push — new-api ( 34a0a9...30b18d )
by Sebastian
04:06
created

QuotesTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 38
c 0
b 0
f 0
dl 0
loc 71
ccs 0
cts 51
cp 0
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceOuterQuotes() 0 25 1
A initQuotesAttributes() 0 4 3
A addSurroundingQuotes() 0 19 6
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2017 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 Seboettg\CiteProc\Util\StringHelper;
14
use SimpleXMLElement;
15
16
/**
17
 * Trait QuotesTrait
18
 *
19
 * The quotes attribute may set on cs:text. When set to “true” (“false” is default), the rendered text is wrapped in
20
 * quotes (the quotation marks used are terms). The localized punctuation-in-quote option controls whether an adjoining
21
 * comma or period appears outside (default) or inside the closing quotation mark.
22
 *
23
 * @package Seboettg\CiteProc\Styles
24
 * @author Sebastian Böttger <[email protected]>
25
 */
26
trait QuotesTrait
27
{
28
29
    /**
30
     * @var bool
31
     */
32
    private $quotes = false;
33
34
    public function initQuotesAttributes(SimpleXMLElement $node)
35
    {
36
        if (isset($node['quotes']) && "true" === (string) $node['quotes']) {
37
            $this->quotes = true;
38
        }
39
    }
40
41
    /**
42
     * @param string $text
43
     * @return string
44
     */
45
    public function addSurroundingQuotes($text)
46
    {
47
        if ($this->quotes) {
48
            $openQuote = CiteProc::getContext()->getLocale()->filter("terms", "open-quote")->single;
49
            $closeQuote = CiteProc::getContext()->getLocale()->filter("terms", "close-quote")->single;
50
            $punctuationInQuotes = CiteProc::getContext()->getLocale()->filter("options", "punctuation-in-quote");
51
            $text = $this->replaceOuterQuotes($text, $openQuote, $closeQuote);
52
            if (null !== $punctuationInQuotes || $punctuationInQuotes === false) {
0 ignored issues
show
introduced by
The condition $punctuationInQuotes === false is always true.
Loading history...
53
                if (preg_match("/([^\.,;]+)([\.,;]{1,})$/", $text, $match)) {
54
                    $punctuation = substr($match[2], -1);
55
                    if ($this->suffix !== $punctuation) {
56
                        $text = $match[1] . substr($match[2], 0, strlen($match[2]) - 1);
57
                        return $openQuote . $text . $closeQuote . $punctuation;
58
                    }
59
                }
60
            }
61
            return $openQuote . $text . $closeQuote;
62
        }
63
        return $text;
64
    }
65
66
    /**
67
     * @param $text
68
     * @param $outerOpenQuote
69
     * @param $outerCloseQuote
70
     * @return string
71
     */
72
    private function replaceOuterQuotes($text, $outerOpenQuote, $outerCloseQuote)
73
    {
74
        $innerOpenQuote = CiteProc::getContext()
75
            ->getLocale()
76
            ->filter("terms", "open-inner-quote")
77
            ->single;
78
        $innerCloseQuote = CiteProc::getContext()
79
            ->getLocale()
80
            ->filter("terms", "close-inner-quote")
81
            ->single;
82
        $text = StringHelper::replaceOuterQuotes(
83
            $text,
84
            "\"",
85
            "\"",
86
            $innerOpenQuote,
87
            $innerCloseQuote
88
        );
89
        $text = StringHelper::replaceOuterQuotes(
90
            $text,
91
            $outerOpenQuote,
92
            $outerCloseQuote,
93
            $innerOpenQuote,
94
            $innerCloseQuote
95
        );
96
        return $text;
97
    }
98
}
99