Passed
Push — new-api ( 42b595...e6ef7d )
by Sebastian
05:57
created

TextCaseRenderer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 58.97%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 70
ccs 23
cts 39
cp 0.5897
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A keepNoCase() 0 6 2
A __construct() 0 4 1
B render() 0 37 10
A setLanguage() 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\Util\StringHelper;
14
15
class TextCaseRenderer implements StyleRendererInterface
16
{
17
    /** @var TextCase */
18
    protected $textCase;
19
20
    /** @var string */
21
    protected $language = 'en';
22
23 60
    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 60
        $this->textCase = $textCase;
26 60
        $this->language = $language;
27 60
    }
28
29 45
    public function setLanguage(string $language): void
30
    {
31 45
        $this->language = $language;
32 45
    }
33
34 18
    public function render(string $text): string
35
    {
36 18
        if (null === $this->textCase) {
37 17
            return $text;
38
        }
39
40 2
        switch ((string)$this->textCase) {
41 2
            case TextCase::UPPERCASE:
42
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
43
                break;
44 2
            case TextCase::LOWERCASE:
45
                $text = $this->keepNoCase(mb_strtolower($text), $text);
46
                break;
47 2
            case TextCase::SENTENCE:
48
                if (StringHelper::checkUpperCaseString($text)) {
49
                    $text = mb_strtolower($text);
50
                    $text = StringHelper::mb_ucfirst($text);
51
                } else {
52
                    $text = StringHelper::mb_ucfirst($text);
53
                }
54
                break;
55 2
            case TextCase::CAPITALIZE_ALL:
56
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
57
                break;
58 2
            case TextCase::TITLE:
59
                if ($this->language === "en") {
60
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
61
                }
62
                break;
63 2
            case TextCase::CAPITALIZE_FIRST:
64 2
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
65 2
                break;
66
            default:
67
                break;
68
        }
69
70 2
        return $text;
71
    }
72
73
74
    /**
75
     * @param  string $render
76
     * @param  string $original
77
     * @return string|string[]|null
78
     */
79 2
    private function keepNoCase(string $render, string $original)
80
    {
81 2
        if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) {
82
            return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render);
83
        }
84 2
        return $render;
85
    }
86
}
87