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

TextCaseRenderer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 76
ccs 38
cts 42
cp 0.9048
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A keepNoCase() 0 6 2
A getTextCase() 0 3 1
A __construct() 0 7 2
A setLanguage() 0 3 1
B render() 0 35 10
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\Util\StringHelper;
14
15
final class TextCaseRenderer implements StylesRendererInterface
16
{
17
    /** @var TextCase */
18
    protected $textCase;
19
20
    /** @var string */
21
    protected $language = 'en';
22
23 139
    public function __construct(?TextCase $textCase = null, string $language = 'en')
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$textCase" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$textCase"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$language" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$language"; expected 0 but found 1
Loading history...
24
    {
25 139
        if (null === $textCase) {
26 137
            $textCase = TextCase::NONE();
27
        }
28 139
        $this->textCase = $textCase;
29 139
        $this->language = $language;
30 139
    }
31
32 123
    public function setLanguage(string $language): void
33
    {
34 123
        $this->language = $language;
35 123
    }
36
37 111
    public function render(string $text): string
38
    {
39 111
        if (null === $this->textCase) {
40
            return $text;
41
        }
42
43 111
        switch ((string)$this->textCase) {
44 111
            case TextCase::UPPERCASE:
45 2
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
46 2
                break;
47 110
            case TextCase::LOWERCASE:
48 1
                $text = $this->keepNoCase(mb_strtolower($text), $text);
49 1
                break;
50 110
            case TextCase::SENTENCE:
51 1
                if (StringHelper::checkUpperCaseString($text)) {
52
                    $text = mb_strtolower($text);
53
                    $text = StringHelper::mb_ucfirst($text);
54
                } else {
55 1
                    $text = StringHelper::mb_ucfirst($text);
56
                }
57 1
                break;
58 110
            case TextCase::CAPITALIZE_ALL:
59 1
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
60 1
                break;
61 110
            case TextCase::TITLE:
62 13
                if ($this->language === "en") {
63 13
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
64
                }
65 13
                break;
66 109
            case TextCase::CAPITALIZE_FIRST:
67 22
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
68 22
                break;
69
        }
70
71 111
        return $text;
72
    }
73
74
75
    /**
76
     * @param  string $render
77
     * @param  string $original
78
     * @return string|string[]|null
79
     */
80 28
    private function keepNoCase(string $render, string $original)
81
    {
82 28
        if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) {
83
            return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render);
84
        }
85 28
        return $render;
86
    }
87
88 2
    public function getTextCase(): ?TextCase
89
    {
90 2
        return $this->textCase;
91
    }
92
}
93