|
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
|
|
|
|
|
10
|
|
|
namespace Seboettg\CiteProc\Rendering\Name; |
|
11
|
|
|
use Seboettg\CiteProc\CiteProc; |
|
12
|
|
|
use Seboettg\CiteProc\Exception\InvalidStylesheetException; |
|
13
|
|
|
use Seboettg\CiteProc\Rendering\Rendering; |
|
14
|
|
|
use Seboettg\CiteProc\RenderingState; |
|
15
|
|
|
use Seboettg\CiteProc\Util\Factory; |
|
16
|
|
|
use Seboettg\Collection\ArrayList; |
|
17
|
|
|
use SimpleXMLElement; |
|
18
|
|
|
use stdClass; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Substitute |
|
23
|
|
|
* The optional cs:substitute element, which must be included as the last child element of cs:names, adds substitution |
|
24
|
|
|
* in case the name variables specified in the parent cs:names element are empty. The substitutions are specified as |
|
25
|
|
|
* child elements of cs:substitute, and must consist of one or more rendering elements (with the exception of cs:layout). |
|
26
|
|
|
* |
|
27
|
|
|
* A shorthand version of cs:names without child elements, which inherits the attributes values set on the cs:name and |
|
28
|
|
|
* cs:et-al child elements of the original cs:names element, may also be used. |
|
29
|
|
|
* |
|
30
|
|
|
* If cs:substitute contains multiple child elements, the first element to return a non-empty result is used for |
|
31
|
|
|
* substitution. Substituted variables are suppressed in the rest of the output to prevent duplication. An example, |
|
32
|
|
|
* where an empty “author” name variable is substituted by the “editor” name variable, or, when no editors exist, by |
|
33
|
|
|
* the “title” macro: |
|
34
|
|
|
* <pre> |
|
35
|
|
|
* <macro name="author"> |
|
36
|
|
|
* <names variable="author"> |
|
37
|
|
|
* <substitute> |
|
38
|
|
|
* <names variable="editor"/> |
|
39
|
|
|
* <text macro="title"/> |
|
40
|
|
|
* </substitute> |
|
41
|
|
|
* </names> |
|
42
|
|
|
* </macro> |
|
43
|
|
|
* </pre> |
|
44
|
|
|
* @package Seboettg\CiteProc\Rendering\Name |
|
|
|
|
|
|
45
|
|
|
* |
|
46
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
47
|
|
|
*/ |
|
|
|
|
|
|
48
|
|
|
class Substitute implements Rendering |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
|
|
|
|
|
52
|
|
|
* @var ArrayList |
|
53
|
|
|
*/ |
|
54
|
|
|
private $children; |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
|
|
|
|
|
57
|
|
|
* @var Names |
|
58
|
|
|
*/ |
|
59
|
|
|
private $parent; |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Substitute constructor. |
|
63
|
|
|
* @param SimpleXMLElement $node |
|
|
|
|
|
|
64
|
|
|
* @param Names $parent |
|
|
|
|
|
|
65
|
|
|
* @throws InvalidStylesheetException |
|
|
|
|
|
|
66
|
|
|
* @throws InvalidStylesheetException |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct(SimpleXMLElement $node, Names $parent) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->parent = $parent; |
|
71
|
|
|
$this->children = new ArrayList(); |
|
72
|
|
|
foreach ($node->children() as $child) { |
|
73
|
|
|
|
|
74
|
|
|
/** @var SimpleXMLElement $child */ |
|
|
|
|
|
|
75
|
|
|
if ($child->getName() === "names") { |
|
76
|
|
|
|
|
77
|
|
|
/** @var Names $names */ |
|
|
|
|
|
|
78
|
|
|
$names = Factory::create($child, $this); |
|
79
|
|
|
|
|
80
|
|
|
/* A shorthand version of cs:names without child elements, which inherits the attributes values set on |
|
81
|
|
|
the cs:name and cs:et-al child elements of the original cs:names element, may also be used. */ |
|
82
|
|
|
if (!$names->hasEtAl()) { |
|
83
|
|
|
// inherit et-al |
|
84
|
|
|
if ($this->parent->hasEtAl()) { |
|
85
|
|
|
$names->setEtAl($this->parent->getEtAl()); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
if (!$names->hasName()) { |
|
89
|
|
|
// inherit name |
|
90
|
|
|
if ($this->parent->hasName()) { |
|
91
|
|
|
$names->setName($this->parent->getName()); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
// inherit label |
|
95
|
|
|
if (!$names->hasLabel() && $this->parent->hasLabel()) { |
|
96
|
|
|
$names->setLabel($this->parent->getLabel()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->children->append($names); |
|
100
|
|
|
} else { |
|
101
|
|
|
$object = Factory::create($child, $this); |
|
102
|
|
|
$this->children->append($object); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
|
|
|
|
|
108
|
|
|
* @param stdClass $data |
|
|
|
|
|
|
109
|
|
|
* @param int|null $citationNumber |
|
|
|
|
|
|
110
|
|
|
* @return string |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
|
|
public function render($data, $citationNumber = null) |
|
113
|
|
|
{ |
|
114
|
|
|
$ret = []; |
|
115
|
|
|
if (CiteProc::getContext()->getRenderingState()->getValue() !== RenderingState::SORTING) { |
|
116
|
|
|
CiteProc::getContext()->setRenderingState(new RenderingState(RenderingState::SUBSTITUTION)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** @var Rendering $child */ |
|
|
|
|
|
|
120
|
|
|
foreach ($this->children as $child) { |
|
121
|
|
|
/* If cs:substitute contains multiple child elements, the first element to return a |
|
122
|
|
|
non-empty result is used for substitution. */ |
|
123
|
|
|
$res = $child->render($data, $citationNumber); |
|
124
|
|
|
if (!empty($res)) { |
|
125
|
|
|
$ret[] = $res; |
|
126
|
|
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
if (CiteProc::getContext()->getRenderingState()->getValue() === RenderingState::SUBSTITUTION) { |
|
130
|
|
|
CiteProc::getContext()->setRenderingState(new RenderingState(RenderingState::RENDERING)); |
|
131
|
|
|
} |
|
132
|
|
|
return implode("", $ret); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|