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