1: <?php
2:
3: 4: 5: 6: 7:
8:
9: namespace academicpuma\citeproc;
10:
11: 12: 13: 14: 15:
16:
17: class Names extends Format {
18:
19: private $substitutes;
20:
21: function init_formatting() {
22:
23: parent::init_formatting();
24: }
25:
26: function init($dom_node, $citeproc) {
27: $etal = '';
28: $tag = $dom_node->getElementsByTagName('substitute')->item(0);
29: if ($tag) {
30: $this->substitutes = Factory::create($tag, $citeproc);
31: $dom_node->removeChild($tag);
32: }
33:
34: $tag = $dom_node->getElementsByTagName('et-al')->item(0);
35: if ($tag) {
36: $etal = Factory::create($tag, $citeproc);
37: $dom_node->removeChild($tag);
38: }
39:
40: $var = $dom_node->getAttribute('variable');
41: foreach ($dom_node->childNodes as $node) {
42: if ($node->nodeType == 1) {
43: $element = Factory::create($node, $citeproc);
44: if (($element instanceof Label))
45: $element->variable = $var;
46: if (($element instanceof Name) && $etal) {
47: $element->etal = $etal;
48: }
49: $this->addElement($element);
50: }
51: }
52: }
53:
54: function render($data, $mode = NULL) {
55: $matches = array();
56: $variable_parts = array();
57:
58: if (!isset($this->delimiter)) {
59: $style_delimiter = $this->citeproc->style->{'names-delimiter'};
60: $mode_delimiter = $this->citeproc->{$mode}->{'names-delimiter'};
61: $this->delimiter = (isset($mode_delimiter)) ? $mode_delimiter : (isset($style_delimiter) ? $style_delimiter : '');
62: }
63:
64: $variables = explode(' ', $this->variable);
65:
66: foreach ($variables as $var) {
67: if (in_array($var, $this->citeproc->quash))
68: continue;
69: if (isset($data->{$var}) && (!empty($data->{$var}))) {
70: $matches[] = $var;
71: }
72: }
73:
74: if (empty($matches)) {
75: if (isset($this->substitutes)) {
76: foreach ($this->substitutes->elements as $element) {
77: if (($element instanceof Names)) {
78: $sub_variables = explode(' ', $element->variable);
79: foreach ($sub_variables as $var) {
80: if (isset($data->{$var})) {
81: $matches[] = $var;
82: $this->citeproc->quash[] = $var;
83: }
84: }
85: } else {
86: $text = $element->render($data, $mode);
87: $this->citeproc->quash[] = isset($element->variable) ? $element->variable : $element->var;
88: if (!empty($text))
89: $variable_parts[] = $text;
90: }
91: if (!empty($matches))
92: break;
93: }
94: }
95: }
96:
97: foreach ($matches as $var) {
98: if (in_array($var, $this->citeproc->quash) && in_array($var, $variables))
99: continue;
100: $text = '';
101: if (!empty($data->{$var})) {
102: foreach ($this->elements as $element) {
103: if ($element instanceof Label) {
104: $element->variable = $var;
105: $text .= $element->render($data, $mode);
106: } elseif ($element instanceof Name) {
107: $text .= $element->render($data->{$var}, $mode);
108: }
109: }
110: }
111: if (!empty($text))
112: $variable_parts[] = $text;
113: }
114:
115: if (!empty($variable_parts)) {
116: $text = implode($this->delimiter, $variable_parts);
117: return $this->format($text);
118: }
119:
120: return;
121: }
122:
123: }
124: