TextCaseTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 72
rs 10
c 1
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B applyTextCase() 0 34 9
A initTextCaseAttributes() 0 10 3
A keepNoCase() 0 6 2
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
    protected function initTextCaseAttributes(SimpleXMLElement $node)
28
    {
29
        foreach ($node->attributes() as $attribute) {
30
            $name = $attribute->getName();
31
            $value = (string) $attribute;
32
33
            switch ($name) {
34
                case 'text-case':
35
                    $this->textCase = $value;
36
                    return;
37
            }
38
        }
39
    }
40
41
    /**
42
     * @param  string $text
43
     * @param  string $lang
44
     * @return string
45
     */
46
    public function applyTextCase($text, $lang = "en")
47
    {
48
49
        switch ($this->textCase) {
50
            case 'uppercase':
51
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
52
                break;
53
            case 'lowercase':
54
                $text = $this->keepNoCase(mb_strtolower($text), $text);
55
                break;
56
            case 'sentence':
57
                if (StringHelper::checkUpperCaseString($text)) {
58
                    $text = mb_strtolower($text);
59
                    return StringHelper::mb_ucfirst($text);
60
                } else {
61
                    return StringHelper::mb_ucfirst($text);
62
                }
63
                break;
64
            case 'capitalize-all':
65
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
66
                break;
67
            case 'title':
68
                if ($lang === "en") {
69
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
70
                }
71
                break;
72
            case 'capitalize-first':
73
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
74
                break;
75
            default:
76
                break;
77
        }
78
79
        return $text;
80
    }
81
82
83
    /**
84
     * @param  string $render
85
     * @param  string $original
86
     * @return string|string[]|null
87
     */
88
    private function keepNoCase($render, $original)
89
    {
90
        if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) {
91
            return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render);
92
        }
93
        return $render;
94
    }
95
}
96