|
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; |
|
11
|
|
|
|
|
12
|
|
|
use Seboettg\CiteProc\Exception\InvalidStylesheetException; |
|
13
|
|
|
use Seboettg\CiteProc\Styles\AffixesTrait; |
|
14
|
|
|
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait; |
|
15
|
|
|
use Seboettg\CiteProc\Styles\DelimiterTrait; |
|
16
|
|
|
use Seboettg\CiteProc\Styles\DisplayTrait; |
|
17
|
|
|
use Seboettg\CiteProc\Styles\FormattingTrait; |
|
18
|
|
|
use Seboettg\CiteProc\Util\Factory; |
|
19
|
|
|
use Seboettg\Collection\ArrayList; |
|
20
|
|
|
use SimpleXMLElement; |
|
21
|
|
|
use stdClass; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class Group |
|
26
|
|
|
* The cs:group rendering element must contain one or more rendering elements (with the exception of cs:layout). |
|
27
|
|
|
* cs:group may carry the delimiter attribute to separate its child elements, as well as affixes and display attributes |
|
28
|
|
|
* (applied to the output of the group as a whole) and formatting attributes (transmitted to the enclosed elements). |
|
29
|
|
|
* cs:group implicitly acts as a conditional: cs:group and its child elements are suppressed if a) at least one |
|
30
|
|
|
* rendering element in cs:group calls a variable (either directly or via a macro), and b) all variables that are |
|
31
|
|
|
* called are empty. This accommodates descriptive cs:text elements. |
|
32
|
|
|
* |
|
33
|
|
|
* @package Seboettg\CiteProc\Rendering |
|
34
|
|
|
* |
|
35
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
36
|
|
|
*/ |
|
|
|
|
|
|
37
|
|
|
class Group implements Rendering, HasParent |
|
38
|
|
|
{ |
|
39
|
|
|
use DelimiterTrait, |
|
|
|
|
|
|
40
|
|
|
AffixesTrait, |
|
41
|
|
|
DisplayTrait, |
|
42
|
|
|
FormattingTrait, |
|
43
|
|
|
ConsecutivePunctuationCharacterTrait; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
|
|
|
|
|
46
|
|
|
* @var ArrayList |
|
47
|
|
|
*/ |
|
48
|
|
|
private $children; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* cs:group may carry the delimiter attribute to separate its child elements |
|
|
|
|
|
|
52
|
|
|
* @var |
|
|
|
|
|
|
53
|
|
|
*/ |
|
54
|
|
|
private $delimiter = ""; |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
private $parent; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
|
|
|
|
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
private $renderedChildsWithVariable = []; |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Group constructor. |
|
66
|
|
|
* @param SimpleXMLElement $node |
|
|
|
|
|
|
67
|
|
|
* @param $parent |
|
|
|
|
|
|
68
|
|
|
* @throws InvalidStylesheetException |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
73 |
|
public function __construct(SimpleXMLElement $node, $parent) |
|
71
|
|
|
{ |
|
72
|
73 |
|
$this->parent = $parent; |
|
73
|
73 |
|
$this->children = new ArrayList(); |
|
74
|
73 |
|
foreach ($node->children() as $child) { |
|
75
|
73 |
|
$this->children->append(Factory::create($child, $this)); |
|
76
|
|
|
} |
|
77
|
73 |
|
$this->initDisplayAttributes($node); |
|
78
|
73 |
|
$this->initAffixesAttributes($node); |
|
79
|
73 |
|
$this->initDelimiterAttributes($node); |
|
80
|
73 |
|
$this->initFormattingAttributes($node); |
|
81
|
73 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
|
|
|
|
|
84
|
|
|
* @param $data |
|
|
|
|
|
|
85
|
|
|
* @param int|null $citationNumber |
|
|
|
|
|
|
86
|
|
|
* @return string |
|
|
|
|
|
|
87
|
|
|
*/ |
|
88
|
66 |
|
public function render($data, $citationNumber = null) |
|
89
|
|
|
{ |
|
90
|
66 |
|
$textParts = array(); |
|
91
|
66 |
|
$terms = $variables = $haveVariables = $elementCount = 0; |
|
92
|
66 |
|
foreach ($this->children as $child) { |
|
93
|
66 |
|
$elementCount++; |
|
94
|
|
|
|
|
95
|
66 |
|
if (($child instanceof Text) && |
|
96
|
46 |
|
($child->getSource() == 'term' || |
|
|
|
|
|
|
97
|
46 |
|
$child->getSource() == 'value')) { |
|
|
|
|
|
|
98
|
26 |
|
++$terms; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
66 |
|
if (($child instanceof Label)) { |
|
102
|
12 |
|
++$terms; |
|
103
|
|
|
} |
|
104
|
66 |
|
if (method_exists($child, "getSource") && $child->getSource() == 'variable' && |
|
105
|
39 |
|
!empty($child->getVariable()) && $child->getVariable() != "date" && |
|
|
|
|
|
|
106
|
39 |
|
!empty($data->{$child->getVariable()}) |
|
|
|
|
|
|
107
|
|
|
) { |
|
108
|
29 |
|
++$variables; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** @var stdClass $data */ |
|
|
|
|
|
|
112
|
66 |
|
$text = $child->render($data, $citationNumber); |
|
113
|
66 |
|
$delimiter = $this->delimiter; |
|
114
|
66 |
|
if (!empty($text)) { |
|
115
|
65 |
|
if ($delimiter && ($elementCount < count($this->children))) { |
|
116
|
|
|
//check to see if the delimiter is already the last character of the text string |
|
117
|
|
|
//if so, remove it so we don't have two of them when the group will be merged |
|
118
|
58 |
|
$stext = strip_tags(trim($text)); |
|
119
|
58 |
|
if ((strrpos($stext, $delimiter[0]) + 1) == strlen($stext) && strlen($stext) > 1) { |
|
120
|
12 |
|
$text = str_replace($stext, '----REPLACE----', $text); |
|
121
|
12 |
|
$stext = substr($stext, 0, -1); |
|
122
|
12 |
|
$text = str_replace('----REPLACE----', $stext, $text); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
65 |
|
$textParts[] = $text; |
|
126
|
|
|
|
|
127
|
65 |
|
if (method_exists($child, "getSource") && $child->getSource() == 'variable' || |
|
|
|
|
|
|
128
|
64 |
|
(method_exists($child, |
|
|
|
|
|
|
129
|
64 |
|
"getVariable") && $child->getVariable() != "date" && !empty($child->getVariable()))) { |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
54 |
|
$haveVariables++; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
65 |
|
if (method_exists($child, "getSource") && $child->getSource() == 'macro') { |
|
135
|
29 |
|
$haveVariables++; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
66 |
|
return $this->formatting($textParts, $variables, $haveVariables, $terms); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
|
|
|
|
|
143
|
|
|
* @return mixed |
|
144
|
|
|
*/ |
|
145
|
10 |
|
public function getParent() |
|
146
|
|
|
{ |
|
147
|
10 |
|
return $this->parent; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
|
|
|
|
|
151
|
|
|
* @param $textParts |
|
|
|
|
|
|
152
|
|
|
* @param $variables |
|
|
|
|
|
|
153
|
|
|
* @param $haveVariables |
|
|
|
|
|
|
154
|
|
|
* @param $terms |
|
|
|
|
|
|
155
|
|
|
* @return string |
|
|
|
|
|
|
156
|
|
|
*/ |
|
157
|
66 |
|
protected function formatting($textParts, $variables, $haveVariables, $terms) |
|
158
|
|
|
{ |
|
159
|
66 |
|
if (empty($textParts)) { |
|
160
|
20 |
|
return ""; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
65 |
|
if ($variables && !$haveVariables) { |
|
164
|
|
|
return ""; // there has to be at least one other none empty value before the term is output |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
65 |
|
if (count($textParts) == $terms) { |
|
168
|
16 |
|
return ""; // there has to be at least one other none empty value before the term is output |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
63 |
|
$text = implode($this->delimiter, $textParts); // insert the delimiter if supplied. |
|
172
|
63 |
|
if (!empty($text)) { |
|
173
|
63 |
|
return $this->wrapDisplayBlock($this->addAffixes($this->format(($text)))); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return ""; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
17 |
|
public function hasDelimiter() |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
17 |
|
return !empty($this->delimiter); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
59 |
|
public function getDelimiter() |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
59 |
|
return $this->delimiter; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|