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") |
|
|
|
|
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
|
|
|
|