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 StyleRendererInterface |
16
|
|
|
{ |
17
|
|
|
/** @var TextCase */ |
18
|
|
|
protected $textCase; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $language = 'en'; |
22
|
|
|
|
23
|
78 |
|
public function __construct(?TextCase $textCase = null, string $language = 'en') |
|
|
|
|
24
|
|
|
{ |
25
|
78 |
|
if (null === $textCase) { |
26
|
73 |
|
$textCase = TextCase::NONE(); |
27
|
|
|
} |
28
|
78 |
|
$this->textCase = $textCase; |
29
|
78 |
|
$this->language = $language; |
30
|
78 |
|
} |
31
|
|
|
|
32
|
63 |
|
public function setLanguage(string $language): void |
33
|
|
|
{ |
34
|
63 |
|
$this->language = $language; |
35
|
63 |
|
} |
36
|
|
|
|
37
|
36 |
|
public function render(string $text): string |
38
|
|
|
{ |
39
|
36 |
|
if (null === $this->textCase) { |
40
|
|
|
return $text; |
41
|
|
|
} |
42
|
|
|
|
43
|
36 |
|
switch ((string)$this->textCase) { |
44
|
36 |
|
case TextCase::UPPERCASE: |
45
|
2 |
|
$text = $this->keepNoCase(mb_strtoupper($text), $text); |
46
|
2 |
|
break; |
47
|
35 |
|
case TextCase::LOWERCASE: |
48
|
1 |
|
$text = $this->keepNoCase(mb_strtolower($text), $text); |
49
|
1 |
|
break; |
50
|
35 |
|
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
|
35 |
|
case TextCase::CAPITALIZE_ALL: |
59
|
1 |
|
$text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text); |
60
|
1 |
|
break; |
61
|
35 |
|
case TextCase::TITLE: |
62
|
|
|
if ($this->language === "en") { |
63
|
|
|
$text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text); |
64
|
|
|
} |
65
|
|
|
break; |
66
|
35 |
|
case TextCase::CAPITALIZE_FIRST: |
67
|
3 |
|
$text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text); |
68
|
3 |
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
36 |
|
return $text; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $render |
77
|
|
|
* @param string $original |
78
|
|
|
* @return string|string[]|null |
79
|
|
|
*/ |
80
|
4 |
|
private function keepNoCase(string $render, string $original) |
81
|
|
|
{ |
82
|
4 |
|
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
|
4 |
|
return $render; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
public function getTextCase(): ?TextCase |
89
|
|
|
{ |
90
|
2 |
|
return $this->textCase; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|