|
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
|
92 |
|
public function removeConsecutivePunctuation($punctuationSign, $subject) |
|
41
|
|
|
{ |
|
42
|
92 |
|
if (empty($punctuationSign) || preg_match("/^\s+$/", $punctuationSign)) { |
|
43
|
14 |
|
return $subject; |
|
44
|
|
|
} |
|
45
|
91 |
|
$pattern = '/' . preg_quote(trim($punctuationSign)) . '{2,}/'; |
|
46
|
91 |
|
if (preg_match($pattern, $subject)) { |
|
47
|
4 |
|
$res = preg_replace($pattern, $punctuationSign, $subject); |
|
48
|
4 |
|
return $res; |
|
49
|
|
|
} |
|
50
|
89 |
|
return $subject; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
|
|
|
|
|
54
|
|
|
* @param $child |
|
|
|
|
|
|
55
|
|
|
*/ |
|
|
|
|
|
|
56
|
132 |
|
protected function getChildsAffixesAndDelimiter($child) |
|
57
|
|
|
{ |
|
58
|
132 |
|
if (method_exists($child, "renderPrefix")) { |
|
59
|
131 |
|
if (!empty($child->renderPrefix()) && !in_array($child->renderPrefix(), $this->childrenPrefixes)) { |
|
60
|
36 |
|
$this->childrenPrefixes[] = $child->renderPrefix(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
132 |
|
if (method_exists($child, "renderSuffix")) { |
|
64
|
131 |
|
if (!empty($child->renderSuffix()) && !in_array($child->renderSuffix(), $this->childrenSuffixes)) { |
|
65
|
31 |
|
$this->childrenSuffixes[] = $child->renderSuffix(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
132 |
|
if (method_exists($child, "getDelimiter")) { |
|
69
|
79 |
|
if (!empty($child->getDelimiter()) && !in_array($child->getDelimiter(), $this->childrenDelimiter)) { |
|
70
|
79 |
|
$this->childrenDelimiter[] = $child->getDelimiter(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
132 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
|
|
|
|
|
76
|
|
|
* @param string $string |
|
|
|
|
|
|
77
|
|
|
* @return string |
|
|
|
|
|
|
78
|
|
|
*/ |
|
79
|
136 |
|
protected function removeConsecutiveChars($string) |
|
80
|
|
|
{ |
|
81
|
136 |
|
foreach ($this->childrenPrefixes as $prefix) { |
|
82
|
36 |
|
$string = $this->removeConsecutivePunctuation($prefix, $string); |
|
83
|
|
|
} |
|
84
|
136 |
|
foreach ($this->childrenSuffixes as $suffix) { |
|
85
|
31 |
|
$string = $this->removeConsecutivePunctuation($suffix, $string); |
|
86
|
|
|
} |
|
87
|
136 |
|
foreach ($this->childrenDelimiter as $delimiter) { |
|
88
|
79 |
|
$string = $this->removeConsecutivePunctuation($delimiter, $string); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
136 |
|
$string = preg_replace("/\s{2,}/", " ", $string); |
|
92
|
|
|
|
|
93
|
136 |
|
return $string; |
|
94
|
|
|
} |
|
95
|
|
|
} |