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

TextCaseTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 70.27%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 75
ccs 26
cts 37
cp 0.7027
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initTextCaseAttributes() 0 13 3
A keepNoCase() 0 6 2
B applyTextCase() 0 34 9
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