Completed
Push — master ( bf66ca...210cc2 )
by Sebastian
05:53
created

CiteProc::render()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 24
nc 12
nop 4
dl 0
loc 39
rs 5.3846
c 0
b 0
f 0
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
12
use Seboettg\CiteProc\Data\DataList;
13
use Seboettg\CiteProc\Exception\CiteProcException;
14
use Seboettg\CiteProc\Root\Info;
15
use Seboettg\CiteProc\Style\Bibliography;
16
use Seboettg\CiteProc\Style\Citation;
17
use Seboettg\CiteProc\Style\Macro;
18
use Seboettg\CiteProc\Style\Options\GlobalOptions;
19
use Seboettg\CiteProc\Root\Root;
20
use Seboettg\CiteProc\Styles\Css\CssStyle;
21
use Seboettg\Collection\ArrayList;
22
23
24
/**
25
 * Class CiteProc
26
 * @package Seboettg\CiteProc
27
 *
28
 * @author Sebastian Böttger <[email protected]>
29
 */
30
class CiteProc
31
{
32
33
    /**
34
     * @var Context
35
     */
36
    private static $context;
37
38
39
    /**
40
     * @return Context
41
     */
42
    public static function getContext()
43
    {
44
        return self::$context;
45
    }
46
47
    /**
48
     * @param Context $context
49
     */
50
    public static function setContext($context)
51
    {
52
        self::$context = $context;
53
    }
54
55
    private $lang;
56
57
    /**
58
     * @var string
59
     */
60
    private $styleSheet;
61
62
    /**
63
     * @var \SimpleXMLElement
64
     */
65
    private $styleSheetXml;
66
67
    /**
68
     * @var array
69
     */
70
    private $markupExtension;
71
72
    /**
73
     * CiteProc constructor.
74
     * @param string $styleSheet xml formatted csl stylesheet
75
     * @param string $lang
76
     * @param array $markupExtension
77
     */
78
    public function __construct($styleSheet, $lang = "en-US", $markupExtension = [])
79
    {
80
        $this->styleSheet = $styleSheet;
81
        $this->lang = $lang;
82
        $this->markupExtension = $markupExtension;
83
    }
84
85
    public function __destruct()
86
    {
87
        self::$context = null;
88
    }
89
90
    /**
91
     * @param \SimpleXMLElement $style
92
     */
93
    private function parse(\SimpleXMLElement $style)
94
    {
95
        $root = new Root();
96
        $root->initInheritableNameAttributes($style);
97
        self::$context->setRoot($root);
98
        $globalOptions = new GlobalOptions($style);
99
        self::$context->setGlobalOptions($globalOptions);
100
101
        /** @var \SimpleXMLElement $node */
102
        foreach ($style as $node) {
103
            $name = $node->getName();
104
            switch ($name) {
105
                case 'info':
106
                    self::$context->setInfo(new Info($node));
107
                    break;
108
                case 'locale':
109
                    self::$context->getLocale()->addXml($node);
110
                    break;
111
                case 'macro':
112
                    $macro = new Macro($node, $root);
0 ignored issues
show
Documentation introduced by
$root is of type object<Seboettg\CiteProc\Root\Root>, but the function expects a object<Seboettg\CiteProc\Style\Root>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
                    self::$context->addMacro($macro->getName(), $macro);
114
                    break;
115
                case 'bibliography':
116
                    $bibliography = new Bibliography($node, $root);
0 ignored issues
show
Documentation introduced by
$root is of type object<Seboettg\CiteProc\Root\Root>, but the function expects a object<Seboettg\CiteProc\Style\Root>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
                    self::$context->setBibliography($bibliography);
118
                    break;
119
                case 'citation':
120
                    $citation = new Citation($node, $root);
121
                    self::$context->setCitation($citation);
122
                    break;
123
            }
124
        }
125
    }
126
127
    /**
128
     * @param DataList $data
129
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
130
     */
131
    protected function bibliography($data)
132
    {
133
134
        return self::$context->getBibliography()->render($data);
135
    }
136
137
    /**
138
     * @param DataList $data
139
     * @param ArrayList $citationItems
140
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
141
     */
142
    protected function citation($data, $citationItems)
143
    {
144
        return self::$context->getCitation()->render($data, $citationItems);
145
    }
146
147
    /**
148
     * @param array|DataList $data
149
     * @param string $mode (citation|bibliography)
150
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
151
     * @throws CiteProcException
152
     */
153
    public function render($data, $mode = "bibliography", $citationItems = [], $citationAsArray = false)
154
    {
155
156
        if (!in_array($mode, ['citation', 'bibliography'])) {
157
            throw new \InvalidArgumentException("\"$mode\" is not a valid mode.");
158
        }
159
160
        $this->init($citationAsArray); //initialize
161
162
        $res = "";
163
164
        if (is_array($data)) {
165
            $data = new DataList($data);
166
        } else if (!($data instanceof DataList)) {
167
            throw new CiteProcException('No valid format for variable data. Either DataList or array expected');
168
        }
169
170
        switch ($mode) {
171
            case 'bibliography':
172
                self::$context->setMode($mode);
173
                // set CitationItems to Context
174
                self::getContext()->setCitationItems($data);
175
                $res = $this->bibliography($data);
176
                break;
177
            case 'citation':
178
                if (is_array($citationItems)) {
179
                    $citationItems = new ArrayList($citationItems);
180
                } else if (!($citationItems instanceof ArrayList)) {
181
                    throw new CiteProcException('No valid format for variable `citationItems`, ArrayList expected.');
182
                }
183
                self::$context->setMode($mode);
184
                // set CitationItems to Context
185
                //self::getContext()->setCitationItems($data); will now set in Layout
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186
                $res = $this->citation($data, $citationItems);
187
        }
188
        self::setContext(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Seboettg\CiteProc\Context>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
190
        return $res;
191
    }
192
193
    /**
194
     * initializes CiteProc and start parsing XML stylesheet
195
     * @param bool $citationAsArray
196
     */
197
    public function init($citationAsArray = false)
198
    {
199
        self::$context = new Context($this);
200
        self::$context->setLocale(new Locale\Locale($this->lang)); //init locale
201
        self::$context->setCitationsAsArray($citationAsArray);
202
        // set markup extensions
203
        self::$context->setMarkupExtension($this->markupExtension);
204
        $this->styleSheetXml = new \SimpleXMLElement($this->styleSheet);
205
        $this->parse($this->styleSheetXml);
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function renderCssStyles()
212
    {
213
        if (self::getContext() === null) {
214
            $this->init();
215
        }
216
217
        if (self::getContext()->getCssStyle() == null) {
218
            $cssStyle = new CssStyle(self::getContext()->getBibliographySpecificOptions());
219
            self::getContext()->setCssStyle($cssStyle);
220
        }
221
222
        return self::getContext()->getCssStyle()->render();
223
    }
224
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
225