Open Journal Systems  3.3.0
ConsecutivePunctuationCharacterTrait.php
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 
18 {
19 
23  private $childrenPrefixes = [];
24 
28  private $childrenSuffixes = [];
29 
33  private $childrenDelimiter = [];
34 
40  public function removeConsecutivePunctuation($punctuationSign, $subject)
41  {
42  if (empty($punctuationSign) || preg_match("/^\s+$/", $punctuationSign)) {
43  return $subject;
44  }
45  $pattern = '/'.preg_quote(trim($punctuationSign)).'{2,}/';
46  if (preg_match($pattern, $subject)) {
47  return preg_replace($pattern, $punctuationSign, $subject);
48  }
49  return $subject;
50  }
51 
55  protected function getChildsAffixesAndDelimiter($child)
56  {
57  if (method_exists($child, "renderPrefix")) {
58  if (!empty($child->renderPrefix()) && !in_array($child->renderPrefix(), $this->childrenPrefixes)) {
59  $this->childrenPrefixes[] = $child->renderPrefix();
60  }
61  }
62  if (method_exists($child, "renderSuffix")) {
63  if (!empty($child->renderSuffix()) && !in_array($child->renderSuffix(), $this->childrenSuffixes)) {
64  $this->childrenSuffixes[] = $child->renderSuffix();
65  }
66  }
67  if (method_exists($child, "getDelimiter")) {
68  if (!empty($child->getDelimiter()) && !in_array($child->getDelimiter(), $this->childrenDelimiter)) {
69  $this->childrenDelimiter[] = $child->getDelimiter();
70  }
71  }
72  }
73 
78  protected function removeConsecutiveChars($string)
79  {
80  foreach ($this->childrenPrefixes as $prefix) {
81  $string = $this->removeConsecutivePunctuation($prefix, $string);
82  }
83  foreach ($this->childrenSuffixes as $suffix) {
84  $string = $this->removeConsecutivePunctuation($suffix, $string);
85  }
86  foreach ($this->childrenDelimiter as $delimiter) {
87  $string = $this->removeConsecutivePunctuation($delimiter, $string);
88  }
89 
90  $string = preg_replace("/\s{2,}/", " ", $string);
91 
92  return $string;
93  }
94 }
Seboettg\CiteProc\Styles\getChildsAffixesAndDelimiter
getChildsAffixesAndDelimiter($child)
Definition: ConsecutivePunctuationCharacterTrait.php:64
Seboettg\CiteProc\Styles
Definition: AffixesTrait.php:10
Seboettg\CiteProc\Styles\removeConsecutiveChars
removeConsecutiveChars($string)
Definition: ConsecutivePunctuationCharacterTrait.php:87
Seboettg\CiteProc\Styles\removeConsecutivePunctuation
removeConsecutivePunctuation($punctuationSign, $subject)
Definition: ConsecutivePunctuationCharacterTrait.php:49
Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait
trait ConsecutivePunctuationCharacterTrait
Definition: ConsecutivePunctuationCharacterTrait.php:18