12 use InvalidArgumentException;
38 private static $context;
46 return self::$context;
54 self::$context = $context;
67 private $styleSheetXml;
72 private $markupExtension;
80 public function __construct($styleSheet, $lang =
"en-US", $markupExtension = [])
82 $this->styleSheet = $styleSheet;
84 $this->markupExtension = $markupExtension;
89 self::$context =
null;
96 private function parse(SimpleXMLElement $style)
99 $root->initInheritableNameAttributes($style);
100 self::$context->setRoot($root);
102 self::$context->setGlobalOptions($globalOptions);
105 foreach ($style as $node) {
106 $name = $node->getName();
109 self::$context->setInfo(
new Info($node));
112 self::$context->getLocale()->addXml($node);
115 $macro =
new Macro($node, $root);
116 self::$context->addMacro($macro->getName(), $macro);
120 self::$context->setBibliography($bibliography);
123 $citation =
new Citation($node, $root);
124 self::$context->setCitation($citation);
137 return self::$context->getBibliography()->render($data);
145 protected function citation($data, $citationItems)
147 return self::$context->getCitation()->render($data, $citationItems);
158 public function render($data, $mode =
"bibliography", $citationItems = [], $citationAsArray =
false)
160 if (is_array($data)) {
164 if (!in_array($mode, [
'citation',
'bibliography'])) {
165 throw new InvalidArgumentException(
"\"$mode\" is not a valid mode.");
168 $this->
init($citationAsArray);
172 if (is_array($data)) {
174 } elseif (!($data instanceof
DataList)) {
175 throw new CiteProcException(
'No valid format for variable data. Either DataList or array expected');
180 self::$context->setMode($mode);
186 if (is_array($citationItems)) {
187 $citationItems =
new ArrayList($citationItems);
188 } elseif (!($citationItems instanceof ArrayList)) {
189 throw new CiteProcException(
'No valid format for variable `citationItems`, ArrayList expected.');
191 self::$context->setMode($mode);
194 $res = $this->
citation($data, $citationItems);
206 public function init($citationAsArray =
false)
208 self::$context =
new Context($this);
209 self::$context->setLocale(
new Locale\Locale($this->lang));
210 self::$context->setCitationsAsArray($citationAsArray);
212 self::$context->setMarkupExtension($this->markupExtension);
213 $this->styleSheetXml =
new SimpleXMLElement($this->styleSheet);
214 $this->parse($this->styleSheetXml);
223 if (self::getContext() ===
null) {
227 if (self::getContext()->getCssStyle() ==
null) {
228 $cssStyle =
new CssStyle(self::getContext()->getBibliographySpecificOptions());