|
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; |
|
11
|
|
|
use Seboettg\CiteProc\Data\DataList; |
|
12
|
|
|
use Seboettg\CiteProc\Exception\CiteProcException; |
|
13
|
|
|
use Seboettg\CiteProc\Style\Bibliography; |
|
14
|
|
|
use Seboettg\CiteProc\Style\Citation; |
|
15
|
|
|
use Seboettg\CiteProc\Style\Macro; |
|
16
|
|
|
use Seboettg\CiteProc\Style\Root; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class CiteProc |
|
21
|
|
|
* @package Seboettg\CiteProc |
|
22
|
|
|
* |
|
23
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class CiteProc |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Context |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $context; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return Context |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function getContext() |
|
38
|
|
|
{ |
|
39
|
|
|
return self::$context; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Context $context |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function setContext($context) |
|
46
|
|
|
{ |
|
47
|
|
|
self::$context = $context; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $styleName |
|
52
|
|
|
* @deprecated |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function loadStyleSheet($styleName) |
|
55
|
|
|
{ |
|
56
|
|
|
return StyleSheet::loadStyleSheet($styleName); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $styleSheet; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var \SimpleXMLElement |
|
67
|
|
|
*/ |
|
68
|
|
|
private $styleSheetXml; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* CiteProc constructor. |
|
72
|
|
|
* @param string $styleSheet xml formatted csl stylesheet |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct($styleSheet, $lang = "en-US") |
|
75
|
|
|
{ |
|
76
|
|
|
$this->styleSheet = $styleSheet; |
|
77
|
|
|
$this->lang = $lang; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param \SimpleXMLElement $style |
|
82
|
|
|
*/ |
|
83
|
|
|
private function parse(\SimpleXMLElement $style) |
|
84
|
|
|
{ |
|
85
|
|
|
$root = new Root(); |
|
86
|
|
|
$root->initInheritableNameAttributes($style); |
|
87
|
|
|
self::$context->setRoot($root); |
|
88
|
|
|
|
|
89
|
|
|
/** @var \SimpleXMLElement $node */ |
|
90
|
|
|
foreach ($style as $node) { |
|
91
|
|
|
$name = $node->getName(); |
|
92
|
|
|
switch ($name) { |
|
93
|
|
|
case 'info': |
|
94
|
|
|
break; |
|
95
|
|
|
case 'locale': |
|
96
|
|
|
self::$context->getLocale()->addXml($node); |
|
97
|
|
|
break; |
|
98
|
|
|
case 'macro': |
|
99
|
|
|
$macro = new Macro($node, $root); |
|
100
|
|
|
self::$context->addMacro($macro->getName(), $macro); |
|
101
|
|
|
break; |
|
102
|
|
|
case 'bibliography': |
|
103
|
|
|
$bibliography = new Bibliography($node, $root); |
|
104
|
|
|
self::$context->setBibliography($bibliography); |
|
105
|
|
|
break; |
|
106
|
|
|
case 'citation': |
|
107
|
|
|
$citation = new Citation($node, $root); |
|
108
|
|
|
self::$context->setCitation($citation); |
|
109
|
|
|
break; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array|DataList $data |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function bibliography($data) |
|
119
|
|
|
{ |
|
120
|
|
|
|
|
121
|
|
|
return self::$context->getBibliography()->render($data); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param array|DataList $data |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function citation($data) |
|
129
|
|
|
{ |
|
130
|
|
|
return self::$context->getCitation()->render($data); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array|DataList $data |
|
135
|
|
|
* @param string $mode (citation|bibliography) |
|
136
|
|
|
* @return string |
|
137
|
|
|
* @throws CiteProcException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function render($data, $mode = "bibliography") |
|
140
|
|
|
{ |
|
141
|
|
|
|
|
142
|
|
|
if (!in_array($mode, ['citation', 'bibliography'])) { |
|
143
|
|
|
throw new \InvalidArgumentException("\"$mode\" is not a valid mode."); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$this->init(); //initialize |
|
147
|
|
|
|
|
148
|
|
|
$res = ""; |
|
149
|
|
|
|
|
150
|
|
|
if (is_array($data)) { |
|
151
|
|
|
$data = new DataList($data); |
|
152
|
|
|
} else if (!($data instanceof DataList)) { |
|
153
|
|
|
throw new CiteProcException('No valid format for variable data. Either DataList or array expected'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// set CitationItems to Context |
|
157
|
|
|
self::getContext()->setCitationItems($data); |
|
158
|
|
|
|
|
159
|
|
|
switch ($mode) { |
|
160
|
|
|
case 'bibliography': |
|
161
|
|
|
self::$context->setMode($mode); |
|
162
|
|
|
$res = $this->bibliography($data); |
|
163
|
|
|
break; |
|
164
|
|
|
case 'citation': |
|
165
|
|
|
self::$context->setMode($mode); |
|
166
|
|
|
$res = $this->citation($data); |
|
167
|
|
|
} |
|
168
|
|
|
self::setContext(null); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
return $res; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function init() |
|
174
|
|
|
{ |
|
175
|
|
|
self::$context = new Context($this); |
|
176
|
|
|
self::$context->setLocale(new Locale\Locale($this->lang)); //init locale |
|
177
|
|
|
$this->styleSheetXml = new \SimpleXMLElement($this->styleSheet); |
|
178
|
|
|
$this->parse($this->styleSheetXml); |
|
179
|
|
|
} |
|
180
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: