|
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
|
|
|
class TextCaseRenderer implements StyleRendererInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var TextCase */ |
|
18
|
|
|
protected $textCase; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $language = 'en'; |
|
22
|
|
|
|
|
23
|
60 |
|
public function __construct(?TextCase $textCase = null, string $language = 'en') |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
60 |
|
$this->textCase = $textCase; |
|
26
|
60 |
|
$this->language = $language; |
|
27
|
60 |
|
} |
|
28
|
|
|
|
|
29
|
45 |
|
public function setLanguage(string $language): void |
|
30
|
|
|
{ |
|
31
|
45 |
|
$this->language = $language; |
|
32
|
45 |
|
} |
|
33
|
|
|
|
|
34
|
18 |
|
public function render(string $text): string |
|
35
|
|
|
{ |
|
36
|
18 |
|
if (null === $this->textCase) { |
|
37
|
17 |
|
return $text; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
switch ((string)$this->textCase) { |
|
41
|
2 |
|
case TextCase::UPPERCASE: |
|
42
|
|
|
$text = $this->keepNoCase(mb_strtoupper($text), $text); |
|
43
|
|
|
break; |
|
44
|
2 |
|
case TextCase::LOWERCASE: |
|
45
|
|
|
$text = $this->keepNoCase(mb_strtolower($text), $text); |
|
46
|
|
|
break; |
|
47
|
2 |
|
case TextCase::SENTENCE: |
|
48
|
|
|
if (StringHelper::checkUpperCaseString($text)) { |
|
49
|
|
|
$text = mb_strtolower($text); |
|
50
|
|
|
$text = StringHelper::mb_ucfirst($text); |
|
51
|
|
|
} else { |
|
52
|
|
|
$text = StringHelper::mb_ucfirst($text); |
|
53
|
|
|
} |
|
54
|
|
|
break; |
|
55
|
2 |
|
case TextCase::CAPITALIZE_ALL: |
|
56
|
|
|
$text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text); |
|
57
|
|
|
break; |
|
58
|
2 |
|
case TextCase::TITLE: |
|
59
|
|
|
if ($this->language === "en") { |
|
60
|
|
|
$text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text); |
|
61
|
|
|
} |
|
62
|
|
|
break; |
|
63
|
2 |
|
case TextCase::CAPITALIZE_FIRST: |
|
64
|
2 |
|
$text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text); |
|
65
|
2 |
|
break; |
|
66
|
|
|
default: |
|
67
|
|
|
break; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
return $text; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $render |
|
76
|
|
|
* @param string $original |
|
77
|
|
|
* @return string|string[]|null |
|
78
|
|
|
*/ |
|
79
|
2 |
|
private function keepNoCase(string $render, string $original) |
|
80
|
|
|
{ |
|
81
|
2 |
|
if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) { |
|
82
|
|
|
return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render); |
|
83
|
|
|
} |
|
84
|
2 |
|
return $render; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|