| Total Complexity | 26 |
| Total Lines | 201 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 33 | class CiteProc |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
|
1 ignored issue
–
show
|
|||
| 37 | * @var Context |
||
| 38 | */ |
||
| 39 | private static $context; |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
|
1 ignored issue
–
show
|
|||
| 43 | * @return Context |
||
| 44 | */ |
||
| 45 | public static function getContext() |
||
| 46 | { |
||
| 47 | return self::$context; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
|
1 ignored issue
–
show
|
|||
| 51 | * @param Context $context |
||
|
1 ignored issue
–
show
|
|||
| 52 | */ |
||
| 53 | public static function setContext($context) |
||
| 54 | { |
||
| 55 | self::$context = $context; |
||
| 56 | } |
||
| 57 | |||
| 58 | private $lang; |
||
| 59 | |||
| 60 | /** |
||
|
1 ignored issue
–
show
|
|||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $styleSheet; |
||
| 64 | |||
| 65 | /** |
||
|
1 ignored issue
–
show
|
|||
| 66 | * @var SimpleXMLElement |
||
| 67 | */ |
||
| 68 | private $styleSheetXml; |
||
| 69 | |||
| 70 | /** |
||
|
1 ignored issue
–
show
|
|||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $markupExtension; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * CiteProc constructor. |
||
| 77 | * @param string $styleSheet xml formatted csl stylesheet |
||
|
1 ignored issue
–
show
|
|||
| 78 | * @param string $lang |
||
|
1 ignored issue
–
show
|
|||
| 79 | * @param array $markupExtension |
||
|
2 ignored issues
–
show
|
|||
| 80 | */ |
||
| 81 | public function __construct($styleSheet, $lang = "en-US", $markupExtension = []) |
||
| 82 | { |
||
| 83 | $this->styleSheet = $styleSheet; |
||
| 84 | $this->lang = $lang; |
||
| 85 | $this->markupExtension = $markupExtension; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function __destruct() |
||
| 89 | { |
||
| 90 | self::$context = null; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
|
1 ignored issue
–
show
|
|||
| 94 | * @param SimpleXMLElement $style |
||
|
2 ignored issues
–
show
|
|||
| 95 | * @throws CiteProcException |
||
|
1 ignored issue
–
show
|
|||
| 96 | */ |
||
| 97 | private function parse(SimpleXMLElement $style) |
||
|
1 ignored issue
–
show
|
|||
| 98 | { |
||
| 99 | $root = new Root(); |
||
| 100 | $root->initInheritableNameAttributes($style); |
||
| 101 | self::$context->setRoot($root); |
||
| 102 | $globalOptions = new GlobalOptions($style); |
||
| 103 | self::$context->setGlobalOptions($globalOptions); |
||
| 104 | |||
| 105 | /** @var SimpleXMLElement $node */ |
||
|
3 ignored issues
–
show
|
|||
| 106 | foreach ($style as $node) { |
||
| 107 | $name = $node->getName(); |
||
| 108 | switch ($name) { |
||
| 109 | case 'info': |
||
| 110 | self::$context->setInfo(new Info($node)); |
||
| 111 | break; |
||
| 112 | case 'locale': |
||
| 113 | self::$context->getLocale()->addXml($node); |
||
| 114 | break; |
||
| 115 | case 'macro': |
||
| 116 | $macro = new Macro($node, $root); |
||
| 117 | self::$context->addMacro($macro->getName(), $macro); |
||
| 118 | break; |
||
| 119 | case 'bibliography': |
||
| 120 | $bibliography = new Bibliography($node, $root); |
||
| 121 | self::$context->setBibliography($bibliography); |
||
| 122 | break; |
||
| 123 | case 'citation': |
||
| 124 | $citation = new Citation($node, $root); |
||
| 125 | self::$context->setCitation($citation); |
||
| 126 | break; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
|
1 ignored issue
–
show
|
|||
| 132 | * @param DataList $data |
||
|
2 ignored issues
–
show
|
|||
| 133 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 134 | */ |
||
| 135 | protected function bibliography($data) |
||
| 136 | { |
||
| 137 | |||
| 138 | return self::$context->getBibliography()->render($data); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
|
1 ignored issue
–
show
|
|||
| 142 | * @param DataList $data |
||
|
3 ignored issues
–
show
|
|||
| 143 | * @param ArrayList $citationItems |
||
|
2 ignored issues
–
show
|
|||
| 144 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 145 | */ |
||
| 146 | protected function citation($data, $citationItems) |
||
| 147 | { |
||
| 148 | return self::$context->getCitation()->render($data, $citationItems); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
|
1 ignored issue
–
show
|
|||
| 152 | * @param array|DataList $data |
||
|
2 ignored issues
–
show
|
|||
| 153 | * @param string $mode (citation|bibliography) |
||
|
2 ignored issues
–
show
|
|||
| 154 | * @param array $citationItems |
||
|
3 ignored issues
–
show
|
|||
| 155 | * @param bool $citationAsArray |
||
|
3 ignored issues
–
show
|
|||
| 156 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 157 | * @throws CiteProcException |
||
|
1 ignored issue
–
show
|
|||
| 158 | */ |
||
| 159 | public function render($data, $mode = "bibliography", $citationItems = [], $citationAsArray = false) |
||
| 160 | { |
||
| 161 | if (is_array($data)) { |
||
| 162 | $data = CiteProcHelper::cloneArray($data); |
||
| 163 | } |
||
| 164 | |||
| 165 | if (!in_array($mode, ['citation', 'bibliography'])) { |
||
| 166 | throw new InvalidArgumentException("\"$mode\" is not a valid mode."); |
||
| 167 | } |
||
| 168 | |||
| 169 | $this->init($citationAsArray); //initialize |
||
| 170 | |||
| 171 | $res = ""; |
||
| 172 | |||
| 173 | if (is_array($data)) { |
||
| 174 | $data = new DataList($data); |
||
| 175 | } else if (!($data instanceof DataList)) { |
||
| 176 | throw new CiteProcException('No valid format for variable data. Either DataList or array expected'); |
||
| 177 | } |
||
| 178 | |||
| 179 | switch ($mode) { |
||
| 180 | case 'bibliography': |
||
| 181 | self::$context->setMode($mode); |
||
| 182 | // set CitationItems to Context |
||
| 183 | self::getContext()->setCitationItems($data); |
||
| 184 | $res = $this->bibliography($data); |
||
| 185 | break; |
||
| 186 | case 'citation': |
||
| 187 | if (is_array($citationItems)) { |
||
| 188 | $citationItems = new ArrayList($citationItems); |
||
| 189 | } else if (!($citationItems instanceof ArrayList)) { |
||
| 190 | throw new CiteProcException('No valid format for variable `citationItems`, ArrayList expected.'); |
||
| 191 | } |
||
| 192 | self::$context->setMode($mode); |
||
| 193 | // set CitationItems to Context |
||
| 194 | //self::getContext()->setCitationItems($data); will now set in Layout |
||
| 195 | $res = $this->citation($data, $citationItems); |
||
| 196 | } |
||
| 197 | self::setContext(null); |
||
| 198 | |||
| 199 | return $res; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * initializes CiteProc and start parsing XML stylesheet |
||
| 204 | * @param bool $citationAsArray |
||
|
3 ignored issues
–
show
|
|||
| 205 | * @throws CiteProcException |
||
|
1 ignored issue
–
show
|
|||
| 206 | */ |
||
| 207 | public function init($citationAsArray = false) |
||
| 208 | { |
||
| 209 | self::$context = new Context($this); |
||
| 210 | self::$context->setLocale(new Locale\Locale($this->lang)); //init locale |
||
| 211 | self::$context->setCitationsAsArray($citationAsArray); |
||
| 212 | // set markup extensions |
||
| 213 | self::$context->setMarkupExtension($this->markupExtension); |
||
| 214 | $this->styleSheetXml = new SimpleXMLElement($this->styleSheet); |
||
| 215 | $this->parse($this->styleSheetXml); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
|
1 ignored issue
–
show
|
|||
| 219 | * @return string |
||
| 220 | * @throws CiteProcException |
||
| 221 | */ |
||
| 222 | public function renderCssStyles() |
||
| 234 | } |
||
| 235 | } |
||
| 236 |