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\CiteProc; |
13
|
|
|
use SimpleXMLElement; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait AffixesTrait |
17
|
|
|
* @package Seboettg\CiteProc\Styles |
18
|
|
|
* @author Sebastian Böttger <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
trait AffixesTrait |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $prefix = ""; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $suffix = ""; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $quote = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param SimpleXMLElement $node |
40
|
|
|
*/ |
41
|
164 |
|
protected function initAffixesAttributes(SimpleXMLElement $node) |
42
|
|
|
{ |
43
|
|
|
/** @var SimpleXMLElement $attribute */ |
44
|
164 |
|
foreach ($node->attributes() as $attribute) { |
45
|
|
|
/** @var string $name */ |
46
|
164 |
|
$name = (string) $attribute->getName(); |
47
|
164 |
|
$value = (string) $attribute; |
48
|
|
|
|
49
|
164 |
|
switch ($name) { |
50
|
164 |
|
case 'prefix': |
51
|
84 |
|
$this->prefix = $value; |
52
|
84 |
|
break; |
53
|
164 |
|
case 'suffix': |
54
|
92 |
|
$this->suffix = $value; |
55
|
92 |
|
break; |
56
|
164 |
|
case 'quote': |
57
|
|
|
$this->quote = (bool) $attribute; |
58
|
|
|
} |
59
|
|
|
} |
60
|
164 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $text |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
160 |
|
protected function addAffixes($text) |
67
|
|
|
{ |
68
|
160 |
|
$prefix = $this->prefix; |
69
|
160 |
|
$suffix = $this->suffix; |
70
|
|
|
|
71
|
160 |
|
if (!empty($suffix)) { // guard against repeated suffixes... |
72
|
81 |
|
$no_tags = strip_tags($text); |
73
|
81 |
|
if (strlen($no_tags) && ($no_tags[(strlen($no_tags) - 1)] == $suffix[0])) { |
74
|
9 |
|
$suffix = substr($suffix, 1); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// punctuation in quote? |
78
|
81 |
|
$piq = CiteProc::getContext() |
79
|
81 |
|
->getLocale() |
80
|
81 |
|
->filter('options', 'punctuation-in-quote'); |
81
|
81 |
|
$punctuationInQuote = is_array($piq) ? current($piq) : $piq; |
|
|
|
|
82
|
|
|
|
83
|
81 |
|
if ($punctuationInQuote && in_array($suffix, [',', ';', '.'])) { |
84
|
34 |
|
$closeQuote = CiteProc::getContext()->getLocale()->filter("terms", "close-quote")->single; |
85
|
34 |
|
$lastChar = mb_substr($text, -1, 1); |
86
|
34 |
|
if ($closeQuote === $lastChar) { // last char is closing quote? |
87
|
2 |
|
$text = mb_substr($text, 0, mb_strlen($text) - 1); //set suffix before |
88
|
2 |
|
return $text . $suffix . $lastChar; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
160 |
|
return $prefix . $text . $suffix; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
147 |
|
public function renderPrefix() |
100
|
|
|
{ |
101
|
147 |
|
return $this->prefix; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
147 |
|
public function renderSuffix() |
108
|
|
|
{ |
109
|
147 |
|
return $this->suffix; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|