Open Journal Systems  3.3.0
Names.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 
21 use Seboettg\CiteProc\Styles\DelimiterTrait;
26 use SimpleXMLElement;
27 use stdClass;
28 
36 class Names implements Rendering, HasParent
37 {
38  use DelimiterTrait,
39  AffixesTrait,
40  FormattingTrait,
41  InheritableNameAttributesTrait;
42 
50  private $variables;
51 
58  private $name;
59 
67  private $label;
68 
91  private $substitute;
92 
101  private $etAl;
102 
109  private $delimiter = ", ";
110 
111  private $parent;
112 
120  public function __construct(SimpleXMLElement $node, $parent)
121  {
122  $this->initInheritableNameAttributes($node);
123  $this->parent = $parent;
127  foreach ($node->children() as $child) {
128  switch ($child->getName()) {
129  case "name":
130  $this->name = Factory::create($child, $this);
131  break;
132  case "label":
133  $this->label = Factory::create($child);
134  break;
135  case "substitute":
136  $this->substitute = new Substitute($child, $this);
137  break;
138  case "et-al":
139  $this->etAl = Factory::create($child);
140  }
141  }
142 
146  foreach ($node->attributes() as $attribute) {
147  if ("variable" === $attribute->getName()) {
148  $this->variables = new ArrayList(explode(" ", (string) $attribute));
149  break;
150  }
151  }
152 
153  $this->initDelimiterAttributes($node);
154  $this->initAffixesAttributes($node);
155  $this->initFormattingAttributes($node);
156  }
157 
172  public function render($data, $citationNumber = null)
173  {
174  $str = "";
175 
176  /* when the selection consists of “editor” and “translator”, and when the contents of these two name variables
177  is identical, then the contents of only one name variable is rendered. In addition, the “editortranslator”
178  term is used if the cs:names element contains a cs:label element, replacing the default “editor” and
179  “translator” terms (e.g. resulting in “Doe (editor & translator)”) */
180  if ($this->variables->hasValue("editor") && $this->variables->hasValue("translator")) {
181  if (isset($data->editor)
182  && isset($data->translator) && NameHelper::sameNames($data->editor, $data->translator)
183  ) {
184  if (isset($this->name)) {
185  $str .= $this->name->render($data, 'editor');
186  } else {
187  $arr = [];
188  foreach ($data->editor as $editor) {
189  $edt = $this->format($editor->family.", ".$editor->given);
190  $results[] = NameHelper::addExtendedMarkup('editor', $editor, $edt);
191  }
192  $str .= implode($this->delimiter, $arr);
193  }
194  if (isset($this->label)) {
195  $this->label->setVariable("editortranslator");
196  $str .= $this->label->render($data);
197  }
198  $vars = $this->variables->toArray();
199  $vars = array_filter($vars, function ($value) {
200  return !($value === "editor" || $value === "translator");
201  });
202  $this->variables->setArray($vars);
203  }
204  }
205 
206  $results = [];
207  foreach ($this->variables as $var) {
208  if (!empty($data->{$var})) {
209  if (!empty($this->name)) {
210  $res = $this->name->render($data, $var, $citationNumber);
211  $name = $res;
212  if (!empty($this->label)) {
213  $name = $this->appendLabel($data, $var, $name);
214  }
215  //add multiple counting values
216  if (is_numeric($name) && $this->name->getForm() === "count") {
217  $results = $this->addCountValues($res, $results);
218  } else {
219  $results[] = $this->format($name);
220  }
221  } else {
222  foreach ($data->{$var} as $name) {
223  $formatted = $this->format($name->given." ".$name->family);
224  $results[] = NameHelper::addExtendedMarkup($var, $name, $formatted);
225  }
226  }
227  // suppress substituted variables
228  if (CiteProc::getContext()->getRenderingState()->getValue() === RenderingState::SUBSTITUTION) {
229  unset($data->{$var});
230  }
231  } else {
232  if (!empty($this->substitute)) {
233  $results[] = $this->substitute->render($data);
234  }
235  }
236  }
237  $results = $this->filterEmpty($results);
238  $str .= implode($this->delimiter, $results);
239  return !empty($str) ? $this->addAffixes($str) : "";
240  }
241 
242 
249  private function appendLabel($data, $var, $name)
250  {
251  $this->label->setVariable($var);
252  if (in_array($this->label->getForm(), ["verb", "verb-short"])) {
253  $name = $this->label->render($data).$name;
254  } else {
255  $name .= $this->label->render($data);
256  }
257  return $name;
258  }
259 
265  private function addCountValues($res, $results)
266  {
267  $lastElement = current($results);
268  $key = key($results);
269  if (!empty($lastElement)) {
270  $lastElement += $res;
271  $results[$key] = $lastElement;
272  } else {
273  $results[] = $res;
274  }
275  return $results;
276  }
277 
281  public function hasEtAl()
282  {
283  return !empty($this->etAl);
284  }
285 
289  public function getEtAl()
290  {
291  return $this->etAl;
292  }
293 
298  public function setEtAl(EtAl $etAl)
299  {
300  $this->etAl = $etAl;
301  return $this;
302  }
303 
307  public function hasName()
308  {
309  return !empty($this->name);
310  }
311 
315  public function getName()
316  {
317  return $this->name;
318  }
319 
324  public function setName(Name $name)
325  {
326  $this->name = $name;
327  return $this;
328  }
329 
333  public function getDelimiter()
334  {
335  return $this->delimiter;
336  }
337 
341  public function getVariables()
342  {
343  return $this->variables;
344  }
345 
349  public function hasLabel()
350  {
351  return !empty($this->label);
352  }
353 
357  public function getLabel()
358  {
359  return $this->label;
360  }
361 
365  public function setLabel($label)
366  {
367  $this->label = $label;
368  }
369 
373  public function getParent()
374  {
375  return $this->parent;
376  }
377 
378  private function filterEmpty(array $results)
379  {
380  return array_filter($results, function ($item) {
381  return !empty($item);
382  });
383  }
384 }
Seboettg\CiteProc\Rendering\Name\Names\__construct
__construct(SimpleXMLElement $node, $parent)
Definition: Names.php:138
Seboettg\CiteProc\Rendering\Name\Names\setLabel
setLabel($label)
Definition: Names.php:383
Seboettg\CiteProc\Rendering\Name\EtAl
Definition: EtAl.php:30
Seboettg\CiteProc\Rendering\Name\Names\hasEtAl
hasEtAl()
Definition: Names.php:299
Seboettg\CiteProc\Styles\AffixesTrait
trait AffixesTrait
Definition: AffixesTrait.php:21
Seboettg\CiteProc\Styles\FormattingTrait
trait FormattingTrait
Definition: FormattingTrait.php:21
Seboettg\CiteProc\Util\NameHelper
Definition: NameHelper.php:21
Seboettg\Collection\current
current()
Definition: ArrayListTrait.php:53
Seboettg\CiteProc\Style\InheritableNameAttributesTrait
trait InheritableNameAttributesTrait
Definition: InheritableNameAttributesTrait.php:37
Seboettg\CiteProc\Rendering\Name\Names\hasLabel
hasLabel()
Definition: Names.php:367
Seboettg\CiteProc\Rendering\Name\Names\setEtAl
setEtAl(EtAl $etAl)
Definition: Names.php:316
Seboettg\CiteProc\Rendering\Name\Names\getParent
getParent()
Definition: Names.php:391
Seboettg\CiteProc\Rendering\Name\Names\getLabel
getLabel()
Definition: Names.php:375
Seboettg\CiteProc\Rendering\Name\Names\hasName
hasName()
Definition: Names.php:325
Seboettg\CiteProc\Exception\InvalidStylesheetException
Definition: InvalidStylesheetException.php:10
Seboettg\CiteProc\Rendering\Name\Substitute
Definition: Substitute.php:47
Seboettg\CiteProc\Rendering\HasParent
Definition: HasParent.php:17
Seboettg\CiteProc\Rendering\Name
Definition: EtAl.php:10
Seboettg\CiteProc\Rendering\Name\Names\getName
getName()
Definition: Names.php:333
Seboettg\CiteProc\CiteProc
Definition: CiteProc.php:32
Seboettg\CiteProc\Rendering\Name\Name
Definition: Name.php:39
Seboettg\CiteProc\Rendering\Name\Names
Definition: Names.php:36
Seboettg\CiteProc\Rendering\Name\Names\setName
setName(Name $name)
Definition: Names.php:342
Seboettg\CiteProc\Exception\CiteProcException
Definition: CiteProcException.php:20
Seboettg\CiteProc\Rendering\Name\Names\getVariables
getVariables()
Definition: Names.php:359
Seboettg\CiteProc\RenderingState
Definition: RenderingState.php:27
Seboettg\CiteProc\Util\NameHelper\sameNames
static sameNames($persons1, $persons2)
Definition: NameHelper.php:97
Seboettg\CiteProc\Styles\addAffixes
addAffixes($text)
Definition: AffixesTrait.php:75
Seboettg\CiteProc\Rendering\Label
Definition: Label.php:25
Seboettg\CiteProc\CiteProc\getContext
static getContext()
Definition: CiteProc.php:45
Seboettg\CiteProc\Rendering\Name\Names\getEtAl
getEtAl()
Definition: Names.php:307
Seboettg\CiteProc\RenderingState\SUBSTITUTION
const SUBSTITUTION
Definition: RenderingState.php:33
Seboettg\CiteProc\Rendering\Name\Names\render
render($data, $citationNumber=null)
Definition: Names.php:190
Seboettg\CiteProc\Rendering\Name\Names\getDelimiter
getDelimiter()
Definition: Names.php:351
Seboettg\CiteProc\Rendering\Rendering
Definition: Rendering.php:22
Seboettg\Collection\ArrayList
Definition: ArrayList.php:20
Seboettg\CiteProc\Util\NameHelper\addExtendedMarkup
static addExtendedMarkup($nameVar, $nameItem, $formattedName)
Definition: NameHelper.php:127
Seboettg\CiteProc\Util\Factory
Definition: Util/Factory.php:21
Seboettg\CiteProc\Styles\format
format($text)
Definition: FormattingTrait.php:81
Seboettg\CiteProc\Util\Factory\create
static create($node, $param=null)
Definition: Util/Factory.php:55