Open Journal Systems  3.3.0
Label.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 
16 use SimpleXMLElement;
17 use stdClass;
18 
25 class Label implements Rendering
26 {
27  use AffixesTrait,
28  FormattingTrait,
29  TextCaseTrait;
30 
31  private $variable;
32 
42  private $form = "";
43 
56  private $plural = "contextual";
57 
62  public function __construct(SimpleXMLElement $node)
63  {
65  foreach ($node->attributes() as $attribute) {
66  switch ($attribute->getName()) {
67  case "variable":
68  $this->variable = (string) $attribute;
69  break;
70  case "form":
71  $this->form = (string) $attribute;
72  break;
73  case "plural":
74  $this->plural = (string) $attribute;
75  break;
76  }
77  }
78 
79  $this->initFormattingAttributes($node);
80  $this->initAffixesAttributes($node);
81  $this->initTextCaseAttributes($node);
82  }
83 
89  public function render($data, $citationNumber = null)
90  {
91  $lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en';
92 
93  $text = '';
94  $variables = explode(' ', $this->variable);
95  $form = !empty($this->form) ? $this->form : 'long';
96  $plural = $this->defaultPlural();
97 
98  if ($this->variable === "editortranslator") {
99  if (isset($data->editor) && isset($data->translator)) {
100  $plural = $this->getPlural($data, $plural, "editortranslator");
101  $term = CiteProc::getContext()->getLocale()->filter('terms', "editortranslator", $form);
102  $pluralForm = $term->{$plural};
103  if (!empty($pluralForm)) {
104  $text = $pluralForm;
105  }
106  }
107  } else {
108  foreach ($variables as $variable) {
109  if (isset($data->{$variable})) {
110  $plural = $this->getPlural($data, $plural, $variable);
111  $term = CiteProc::getContext()->getLocale()->filter('terms', $variable, $form);
112  $pluralForm = $term->{$plural};
113  if (!empty($data->{$variable}) && !empty($pluralForm)) {
114  $text = $pluralForm;
115  break;
116  }
117  }
118  }
119  }
120 
121  return $this->formatting($text, $lang);
122  }
123 
129  private function evaluateStringPluralism($data, $variable)
130  {
131  $str = isset($data->{$variable}) ? $data->{$variable} : '';
132  $plural = 'single';
133  if (!empty($str)) {
134  switch ($variable) {
135  case 'page':
136  $pageRegex = "/([a-zA-Z]*)([0-9]+)\s*(?:–|-)\s*([a-zA-Z]*)([0-9]+)/";
137  $err = preg_match($pageRegex, $str, $m);
138  if ($err !== false && count($m) == 0) {
139  $plural = 'single';
140  } elseif ($err !== false && count($m)) {
141  $plural = 'multiple';
142  }
143  break;
144  default:
145  if (is_numeric($str)) {
146  return $str > 1 ? 'multiple' : 'single';
147  }
148  }
149  }
150  return $plural;
151  }
152 
156  public function setVariable($variable)
157  {
158  $this->variable = $variable;
159  }
160 
167  protected function getPlural($data, $plural, $variable)
168  {
169 
170  if ($variable === "editortranslator" && isset($data->editor)) {
171  $var = $data->editor;
172  } else {
173  $var = $data->{$variable};
174  }
175  if (((!isset($this->plural) || empty($plural))) && !empty($var)) {
176  if (is_array($var)) {
177  $count = count($var);
178  if ($count == 1) {
179  $plural = 'single';
180  return $plural;
181  } elseif ($count > 1) {
182  $plural = 'multiple';
183  return $plural;
184  }
185  return $plural;
186  } else {
187  return $this->evaluateStringPluralism($data, $variable);
188  }
189  } else {
190  if ($this->plural != "always") {
191  $plural = $this->evaluateStringPluralism($data, $variable);
192  return $plural;
193  }
194  return $plural;
195  }
196  }
197 
201  public function getForm()
202  {
203  return $this->form;
204  }
205 
209  public function setForm($form)
210  {
211  $this->form = $form;
212  }
213 
219  protected function formatting($text, $lang)
220  {
221  if (empty($text)) {
222  return "";
223  }
224  if ($this->stripPeriods) {
225  $text = str_replace('.', '', $text);
226  }
227 
228  $text = preg_replace("/\s&\s/", " &#38; ", $text); //replace ampersands by html entity
229  $text = $this->format($this->applyTextCase($text, $lang));
230  return $this->addAffixes($text);
231  }
232 
236  protected function defaultPlural()
237  {
238  $plural = "";
239  switch ($this->plural) {
240  case 'never':
241  $plural = 'single';
242  break;
243  case 'always':
244  $plural = 'multiple';
245  break;
246  case 'contextual':
247  default:
248  }
249  return $plural;
250  }
251 }
Seboettg\CiteProc\Rendering\Label\formatting
formatting($text, $lang)
Definition: Label.php:225
Seboettg\CiteProc\Styles\AffixesTrait
trait AffixesTrait
Definition: AffixesTrait.php:21
Seboettg\CiteProc\Styles\FormattingTrait
trait FormattingTrait
Definition: FormattingTrait.php:21
Seboettg\CiteProc\Rendering\Label\setForm
setForm($form)
Definition: Label.php:215
Seboettg\CiteProc\Style\getForm
getForm()
Definition: InheritableNameAttributesTrait.php:621
Seboettg\CiteProc\Rendering\Label\__construct
__construct(SimpleXMLElement $node)
Definition: Label.php:68
Seboettg\CiteProc\Rendering\Label\render
render($data, $citationNumber=null)
Definition: Label.php:95
Seboettg\CiteProc\Rendering\Label\getForm
getForm()
Definition: Label.php:207
Seboettg\CiteProc\Rendering\Label\getPlural
getPlural($data, $plural, $variable)
Definition: Label.php:173
Seboettg\CiteProc\CiteProc
Definition: CiteProc.php:32
Seboettg\CiteProc\Styles\TextCaseTrait
trait TextCaseTrait
Definition: TextCaseTrait.php:23
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
Seboettg\CiteProc\Rendering
Seboettg\CiteProc\Rendering\Label\setVariable
setVariable($variable)
Definition: Label.php:162
Seboettg\CiteProc\Rendering\Label
Definition: Label.php:25
Seboettg\CiteProc\CiteProc\getContext
static getContext()
Definition: CiteProc.php:45
Seboettg\CiteProc\Style\setForm
setForm($form)
Definition: InheritableNameAttributesTrait.php:629
Seboettg\CiteProc\Rendering\Rendering
Definition: Rendering.php:22
Seboettg\CiteProc\Rendering\Label\defaultPlural
defaultPlural()
Definition: Label.php:242