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

AffixesRenderer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 82.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 86
ccs 29
cts 35
cp 0.8286
rs 10
c 1
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A factory() 0 8 2
B render() 0 21 7
A getPrefix() 0 3 1
A getCloseQuote() 0 3 1
A isPunctuationInQuote() 0 3 1
A getSuffix() 0 3 1
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        https://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2020 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Styles;
12
13
use Seboettg\CiteProc\Context;
14
15
final class AffixesRenderer implements StylesRendererInterface
16
{
17
18 181
    public static function factory(Context $context, ?string $prefix, ?string $suffix)
19
    {
20
        $piq = $context
21 181
            ->getLocale()
22 181
            ->filter('options', 'punctuation-in-quote');
23 181
        $punctuationInQuote = is_array($piq) ? current($piq) : $piq;
0 ignored issues
show
introduced by
The condition is_array($piq) is always false.
Loading history...
24 181
        $closeQuote = $context->getLocale()->filter("terms", "close-quote")->single;
25 181
        return new self($prefix, $suffix, $punctuationInQuote, $closeQuote);
0 ignored issues
show
Bug introduced by
$punctuationInQuote of type stdClass is incompatible with the type boolean|null expected by parameter $punctuationInQuote of Seboettg\CiteProc\Styles...Renderer::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        return new self($prefix, $suffix, /** @scrutinizer ignore-type */ $punctuationInQuote, $closeQuote);
Loading history...
26
    }
27
28
    /** @var string */
29
    private $prefix = "";
30
31
    /** @var string */
32
    private $suffix = "";
33
34
    /** @var bool */
35
    private $punctuationInQuote = "";
36
37
    /** @var string */
38
    private $closeQuote = "";
39
40 181
    public function __construct(?string $prefix, ?string $suffix, ?bool $punctuationInQuote, ?string $closeQuote)
41
    {
42 181
        $this->prefix = $prefix;
43 181
        $this->suffix = $suffix;
44 181
        $this->punctuationInQuote = $punctuationInQuote;
45 181
        $this->closeQuote = $closeQuote;
46 181
    }
47
48 177
    public function render(string $text): string
49
    {
50 177
        $prefix = $this->prefix;
51 177
        $suffix = $this->suffix;
52 177
        $punctuationInQuote = $this->punctuationInQuote;
53 177
        $closeQuote = $this->closeQuote;
54 177
        if (!empty($suffix)) { // guard against repeated suffixes...
55 90
            $no_tags = strip_tags($text);
56 90
            if (strlen($no_tags) && ($no_tags[(strlen($no_tags) - 1)] == $suffix[0])) {
57 13
                $suffix = substr($suffix, 1);
58
            }
59
60 90
            if ($punctuationInQuote && in_array($suffix, [',', ';', '.'])) {
61 45
                $lastChar = mb_substr($text, -1, 1);
62 45
                if ($closeQuote === $lastChar) { // last char is closing quote?
63 4
                    $text = mb_substr($text, 0, mb_strlen($text) - 1); //set suffix before
64 4
                    return $prefix . $text . $suffix . $lastChar;
65
                }
66
            }
67
        }
68 177
        return $prefix . $text . $suffix;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getPrefix(): ?string
75
    {
76
        return $this->prefix;
77
    }
78
79
    /**
80
     * @return string
81
     */
82 9
    public function getSuffix(): ?string
83
    {
84 9
        return $this->suffix;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function isPunctuationInQuote(): ?bool
91
    {
92
        return $this->punctuationInQuote;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getCloseQuote(): ?string
99
    {
100
        return $this->closeQuote;
101
    }
102
}
103