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
|
|
|
use Seboettg\CiteProc\Util\StringHelper; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait TextCase |
16
|
|
|
* @package Seboettg\CiteProc\Styles |
17
|
|
|
* |
18
|
|
|
* @author Sebastian Böttger <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
trait TextCaseTrait |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
private $textCase; |
24
|
|
|
|
25
|
|
View Code Duplication |
protected function initTextCaseAttributes(\SimpleXMLElement $node) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
foreach ($node->attributes() as $attribute) { |
28
|
|
|
/** @var string $name */ |
29
|
|
|
$name = $attribute->getName(); |
30
|
|
|
$value = (string) $attribute; |
31
|
|
|
|
32
|
|
|
switch ($name) { |
33
|
|
|
case 'text-case': |
34
|
|
|
$this->textCase = $value; |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $text |
42
|
|
|
* @param string $lang |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function applyTextCase($text, $lang = "en") |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
switch ($this->textCase) { |
49
|
|
|
case 'uppercase': |
50
|
|
|
$text = $this->keepNoCase(mb_strtoupper($text), $text); |
51
|
|
|
break; |
52
|
|
|
case 'lowercase': |
53
|
|
|
$text = $this->keepNoCase(mb_strtolower($text), $text); |
54
|
|
|
break; |
55
|
|
|
case 'sentence': |
56
|
|
|
if (StringHelper::checkUpperCaseString($text)) { |
57
|
|
|
$text = mb_strtolower($text); |
58
|
|
|
return StringHelper::mb_ucfirst($text); |
59
|
|
|
} else { |
60
|
|
|
return StringHelper::mb_ucfirst($text); |
61
|
|
|
} |
62
|
|
|
break; |
|
|
|
|
63
|
|
|
case 'capitalize-all': |
64
|
|
|
$text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text); |
65
|
|
|
break; |
66
|
|
|
case 'title': |
67
|
|
|
if ($lang === "en") { |
68
|
|
|
$text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text); |
69
|
|
|
} |
70
|
|
|
break; |
71
|
|
|
case 'capitalize-first': |
72
|
|
|
$text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text); |
73
|
|
|
break; |
74
|
|
|
default: |
|
|
|
|
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $text; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
private function keepNoCase($render, $original) |
83
|
|
|
{ |
84
|
|
|
if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) { |
85
|
|
|
return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render); |
86
|
|
|
} |
87
|
|
|
return $render; |
88
|
|
|
} |
89
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.