Open Journal Systems  3.3.0
AffixesTrait.php
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 
11 
13 use SimpleXMLElement;
14 
20 trait AffixesTrait
21 {
22 
26  private $prefix = "";
27 
31  private $suffix = "";
32 
36  private $quote = false;
37 
41  protected function initAffixesAttributes(SimpleXMLElement $node)
42  {
44  foreach ($node->attributes() as $attribute) {
46  $name = (string) $attribute->getName();
47  $value = (string) $attribute;
48 
49  switch ($name) {
50  case 'prefix':
51  $this->prefix = $value;
52  break;
53  case 'suffix':
54  $this->suffix = $value;
55  break;
56  case 'quote':
57  $this->quote = (bool) $attribute;
58  }
59  }
60  }
61 
66  protected function addAffixes($text)
67  {
68  $prefix = $this->prefix;
69  $suffix = $this->suffix;
70 
71  if (!empty($suffix)) { // guard against repeated suffixes...
72  $no_tags = strip_tags($text);
73  if (strlen($no_tags) && ($no_tags[(strlen($no_tags) - 1)] == $suffix[0])) {
74  $suffix = substr($suffix, 1);
75  }
76 
77  // punctuation in quote?
78  $piq = CiteProc::getContext()
79  ->getLocale()
80  ->filter('options', 'punctuation-in-quote');
81  $punctuationInQuote = is_array($piq) ? current($piq) : $piq;
82 
83  if ($punctuationInQuote && in_array($suffix, [',', ';', '.'])) {
84  $closeQuote = CiteProc::getContext()->getLocale()->filter("terms", "close-quote")->single;
85  $lastChar = mb_substr($text, -1, 1);
86  if ($closeQuote === $lastChar) { // last char is closing quote?
87  $text = mb_substr($text, 0, mb_strlen($text) - 1); //set suffix before
88  return $text . $suffix . $lastChar;
89  }
90  }
91  }
92 
93  return $prefix . $text . $suffix;
94  }
95 
99  public function renderPrefix()
100  {
101  return $this->prefix;
102  }
103 
107  public function renderSuffix()
108  {
109  return $this->suffix;
110  }
111 }
Seboettg\CiteProc\Styles\renderPrefix
renderPrefix()
Definition: AffixesTrait.php:108
Seboettg\CiteProc\Styles\renderSuffix
renderSuffix()
Definition: AffixesTrait.php:116
Seboettg\CiteProc\Styles\AffixesTrait
trait AffixesTrait
Definition: AffixesTrait.php:21
Seboettg\CiteProc\Styles\initAffixesAttributes
initAffixesAttributes(SimpleXMLElement $node)
Definition: AffixesTrait.php:50
Seboettg\CiteProc\Styles
Definition: AffixesTrait.php:10
Seboettg\CiteProc\CiteProc
Definition: CiteProc.php:32
Seboettg\CiteProc\Styles\addAffixes
addAffixes($text)
Definition: AffixesTrait.php:75
Seboettg\CiteProc\CiteProc\getContext
static getContext()
Definition: CiteProc.php:45