|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/* |
|
4
|
|
|
* citeproc-php |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/seboettg/citeproc-php for the source repository |
|
7
|
|
|
* @copyright Copyright (c) 2020 Sebastian Böttger. |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Seboettg\CiteProc\Styles; |
|
12
|
|
|
|
|
13
|
|
|
use Seboettg\CiteProc\Util\StringHelper; |
|
14
|
|
|
|
|
15
|
|
|
final class TextCaseRenderer implements StylesRendererInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var TextCase */ |
|
18
|
|
|
protected $textCase; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $language = 'en'; |
|
22
|
|
|
|
|
23
|
139 |
|
public function __construct(?TextCase $textCase = null, string $language = 'en') |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
139 |
|
if (null === $textCase) { |
|
26
|
137 |
|
$textCase = TextCase::NONE(); |
|
27
|
|
|
} |
|
28
|
139 |
|
$this->textCase = $textCase; |
|
29
|
139 |
|
$this->language = $language; |
|
30
|
139 |
|
} |
|
31
|
|
|
|
|
32
|
123 |
|
public function setLanguage(string $language): void |
|
33
|
|
|
{ |
|
34
|
123 |
|
$this->language = $language; |
|
35
|
123 |
|
} |
|
36
|
|
|
|
|
37
|
111 |
|
public function render(string $text): string |
|
38
|
|
|
{ |
|
39
|
111 |
|
if (null === $this->textCase) { |
|
40
|
|
|
return $text; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
111 |
|
switch ((string)$this->textCase) { |
|
44
|
111 |
|
case TextCase::UPPERCASE: |
|
45
|
2 |
|
$text = $this->keepNoCase(mb_strtoupper($text), $text); |
|
46
|
2 |
|
break; |
|
47
|
110 |
|
case TextCase::LOWERCASE: |
|
48
|
1 |
|
$text = $this->keepNoCase(mb_strtolower($text), $text); |
|
49
|
1 |
|
break; |
|
50
|
110 |
|
case TextCase::SENTENCE: |
|
51
|
1 |
|
if (StringHelper::checkUpperCaseString($text)) { |
|
52
|
|
|
$text = mb_strtolower($text); |
|
53
|
|
|
$text = StringHelper::mb_ucfirst($text); |
|
54
|
|
|
} else { |
|
55
|
1 |
|
$text = StringHelper::mb_ucfirst($text); |
|
56
|
|
|
} |
|
57
|
1 |
|
break; |
|
58
|
110 |
|
case TextCase::CAPITALIZE_ALL: |
|
59
|
1 |
|
$text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text); |
|
60
|
1 |
|
break; |
|
61
|
110 |
|
case TextCase::TITLE: |
|
62
|
13 |
|
if ($this->language === "en") { |
|
63
|
13 |
|
$text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text); |
|
64
|
|
|
} |
|
65
|
13 |
|
break; |
|
66
|
109 |
|
case TextCase::CAPITALIZE_FIRST: |
|
67
|
22 |
|
$text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text); |
|
68
|
22 |
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
111 |
|
return $text; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $render |
|
77
|
|
|
* @param string $original |
|
78
|
|
|
* @return string|string[]|null |
|
79
|
|
|
*/ |
|
80
|
28 |
|
private function keepNoCase(string $render, string $original) |
|
81
|
|
|
{ |
|
82
|
28 |
|
if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) { |
|
83
|
|
|
return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render); |
|
84
|
|
|
} |
|
85
|
28 |
|
return $render; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
public function getTextCase(): ?TextCase |
|
89
|
|
|
{ |
|
90
|
2 |
|
return $this->textCase; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|