1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* citeproc-php |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
6
|
|
|
* @copyright Copyright (c) 2017 Sebastian Böttger. |
7
|
|
|
* @license https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Seboettg\CiteProc\Styles; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait ConsecutivePunctuationCharacterTrait |
14
|
|
|
* @package Seboettg\CiteProc\Styles |
15
|
|
|
* @author Sebastian Böttger <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
trait ConsecutivePunctuationCharacterTrait |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $childrenPrefixes = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $childrenSuffixes = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $childrenDelimiter = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $punctuationSign |
37
|
|
|
* @param $subject |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
147 |
|
public function removeConsecutivePunctuation($punctuationSign, $subject): string |
41
|
|
|
{ |
42
|
147 |
|
if (empty($punctuationSign) || preg_match("/^\s+$/", $punctuationSign)) { |
43
|
55 |
|
return $subject; |
44
|
|
|
} |
45
|
125 |
|
$pattern = '/'.preg_quote(trim($punctuationSign), '/').'{2,}/'; |
46
|
125 |
|
if (preg_match($pattern, $subject)) { |
47
|
1 |
|
return preg_replace($pattern, $punctuationSign, $subject); |
48
|
|
|
} |
49
|
125 |
|
return $subject; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $child |
54
|
|
|
*/ |
55
|
170 |
|
protected function getChildrenAffixesAndDelimiter($child) |
56
|
|
|
{ |
57
|
170 |
|
if (method_exists($child, "renderPrefix")) { |
58
|
166 |
|
if (!empty($child->renderPrefix()) && !in_array($child->renderPrefix(), $this->childrenPrefixes)) { |
59
|
48 |
|
$this->childrenPrefixes[] = $child->renderPrefix(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
170 |
|
if (method_exists($child, "renderSuffix")) { |
63
|
166 |
|
if (!empty($child->renderSuffix()) && !in_array($child->renderSuffix(), $this->childrenSuffixes)) { |
64
|
46 |
|
$this->childrenSuffixes[] = $child->renderSuffix(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
170 |
|
if (method_exists($child, "getDelimiter")) { |
68
|
136 |
|
if (!empty($child->getDelimiter()) && !in_array($child->getDelimiter(), $this->childrenDelimiter)) { |
69
|
130 |
|
$this->childrenDelimiter[] = $child->getDelimiter(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
170 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $string |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
175 |
|
protected function removeConsecutiveChars($string) |
79
|
|
|
{ |
80
|
175 |
|
foreach ($this->childrenPrefixes as $prefix) { |
81
|
48 |
|
$string = $this->removeConsecutivePunctuation($prefix, $string); |
82
|
|
|
} |
83
|
175 |
|
foreach ($this->childrenSuffixes as $suffix) { |
84
|
46 |
|
$string = $this->removeConsecutivePunctuation($suffix, $string); |
85
|
|
|
} |
86
|
175 |
|
foreach ($this->childrenDelimiter as $delimiter) { |
87
|
126 |
|
$string = $this->removeConsecutivePunctuation($delimiter, $string); |
88
|
|
|
} |
89
|
|
|
|
90
|
175 |
|
$string = preg_replace("/\s{2,}/", " ", $string); |
91
|
|
|
|
92
|
175 |
|
return $string; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|