|
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\Context; |
|
14
|
|
|
|
|
15
|
|
|
final class AffixesRenderer implements StyleRendererInterface |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
60 |
|
public static function factory(Context $context, ?string $prefix, ?string $suffix) |
|
19
|
|
|
{ |
|
20
|
|
|
$piq = $context |
|
21
|
60 |
|
->getLocale() |
|
22
|
60 |
|
->filter('options', 'punctuation-in-quote'); |
|
23
|
60 |
|
$punctuationInQuote = is_array($piq) ? current($piq) : $piq; |
|
|
|
|
|
|
24
|
60 |
|
$closeQuote = $context->getLocale()->filter("terms", "close-quote")->single; |
|
25
|
60 |
|
return new self($prefix, $suffix, $punctuationInQuote, $closeQuote); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $prefix = ""; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $suffix = ""; |
|
33
|
|
|
|
|
34
|
|
|
/** @var bool */ |
|
35
|
|
|
private $punctuationInQuote = ""; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
private $closeQuote = ""; |
|
39
|
|
|
|
|
40
|
60 |
|
public function __construct(?string $prefix, ?string $suffix, ?bool $punctuationInQuote, ?string $closeQuote) |
|
41
|
|
|
{ |
|
42
|
60 |
|
$this->prefix = $prefix; |
|
43
|
60 |
|
$this->suffix = $suffix; |
|
44
|
60 |
|
$this->punctuationInQuote = $punctuationInQuote; |
|
45
|
60 |
|
$this->closeQuote = $closeQuote; |
|
46
|
60 |
|
} |
|
47
|
|
|
|
|
48
|
18 |
|
public function render(string $text): string |
|
49
|
|
|
{ |
|
50
|
18 |
|
$prefix = $this->prefix; |
|
51
|
18 |
|
$suffix = $this->suffix; |
|
52
|
18 |
|
$punctuationInQuote = $this->punctuationInQuote; |
|
53
|
18 |
|
$closeQuote = $this->closeQuote; |
|
54
|
18 |
|
if (!empty($suffix)) { // guard against repeated suffixes... |
|
55
|
10 |
|
$no_tags = strip_tags($text); |
|
56
|
10 |
|
if (strlen($no_tags) && ($no_tags[(strlen($no_tags) - 1)] == $suffix[0])) { |
|
57
|
3 |
|
$suffix = substr($suffix, 1); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
10 |
|
if ($punctuationInQuote && in_array($suffix, [',', ';', '.'])) { |
|
61
|
|
|
$lastChar = mb_substr($text, -1, 1); |
|
62
|
|
|
if ($closeQuote === $lastChar) { // last char is closing quote? |
|
63
|
|
|
$text = mb_substr($text, 0, mb_strlen($text) - 1); //set suffix before |
|
64
|
|
|
return $text . $suffix . $lastChar; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
18 |
|
return $prefix . $text . $suffix; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|