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

TextCaseTrait::applyTextCase()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 14.184

Importance

Changes 0
Metric Value
cc 9
eloc 27
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 34
ccs 15
cts 25
cp 0.6
crap 14.184
rs 8.0555
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\Util\StringHelper;
13
use SimpleXMLElement;
14
15
/**
16
 * Trait TextCase
17
 *
18
 * @package Seboettg\CiteProc\Styles
19
 *
20
 * @author Sebastian Böttger <[email protected]>
21
 */
22
trait TextCaseTrait
23
{
24
25
    private $textCase;
26
27 132
    protected function initTextCaseAttributes(SimpleXMLElement $node)
28
    {
29 132
        foreach ($node->attributes() as $attribute) {
30
            /**
31
             * @var string $name
32
             */
33 132
            $name = $attribute->getName();
34 132
            $value = (string) $attribute;
35
36
            switch ($name) {
37 132
                case 'text-case':
38 41
                    $this->textCase = $value;
39 132
                    return;
40
            }
41
        }
42 132
    }
43
44
    /**
45
     * @param  string $text
46
     * @param  string $lang
47
     * @return string
48
     */
49 108
    public function applyTextCase($text, $lang = "en")
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$lang" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$lang"; expected 0 but found 1
Loading history...
50
    {
51
52 108
        switch ($this->textCase) {
53 108
            case 'uppercase':
54
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
55
                break;
56 108
            case 'lowercase':
57
                $text = $this->keepNoCase(mb_strtolower($text), $text);
58
                break;
59 108
            case 'sentence':
60
                if (StringHelper::checkUpperCaseString($text)) {
61
                    $text = mb_strtolower($text);
62
                    return StringHelper::mb_ucfirst($text);
63
                } else {
64
                    return StringHelper::mb_ucfirst($text);
65
                }
66
                break;
67 108
            case 'capitalize-all':
68
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
69
                break;
70 108
            case 'title':
71 13
                if ($lang === "en") {
72 13
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
73
                }
74 13
                break;
75 107
            case 'capitalize-first':
76 18
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
77 18
                break;
78
            default:
79 107
                break;
80
        }
81
82 108
        return $text;
83
    }
84
85
86
    /**
87
     * @param  string $render
88
     * @param  string $original
89
     * @return string|string[]|null
90
     */
91 23
    private function keepNoCase($render, $original)
92
    {
93 23
        if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) {
94
            return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render);
95
        }
96 23
        return $render;
97
    }
98
}
99