Completed
Push — master ( 0bb17e...5a7e4d )
by Sebastian
04:41
created

CiteProc::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
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\Options\GlobalOptions;
17
use Seboettg\CiteProc\Style\Root;
18
use Seboettg\CiteProc\Styles\Css\CssStyle;
19
20
21
/**
22
 * Class CiteProc
23
 * @package Seboettg\CiteProc
24
 *
25
 * @author Sebastian Böttger <[email protected]>
26
 */
27
class CiteProc
28
{
29
30
    /**
31
     * @var Context
32
     */
33
    private static $context;
34
35
36
    /**
37
     * @return Context
38
     */
39
    public static function getContext()
40
    {
41
        return self::$context;
42
    }
43
44
    /**
45
     * @param Context $context
46
     */
47
    public static function setContext($context)
48
    {
49
        self::$context = $context;
50
    }
51
52
    private $lang;
53
54
    /**
55
     * @var string
56
     */
57
    private $styleSheet;
58
59
    /**
60
     * @var \SimpleXMLElement
61
     */
62
    private $styleSheetXml;
63
64
    /**
65
     * CiteProc constructor.
66
     * @param string $styleSheet xml formatted csl stylesheet
67
     */
68
    public function __construct($styleSheet, $lang = "en-US")
69
    {
70
        $this->styleSheet = $styleSheet;
71
        $this->lang = $lang;
72
    }
73
74
    public function __destruct()
75
    {
76
        self::$context = null;
77
    }
78
79
    /**
80
     * @param \SimpleXMLElement $style
81
     */
82
    private function parse(\SimpleXMLElement $style)
83
    {
84
        $root = new Root();
85
        $root->initInheritableNameAttributes($style);
86
        self::$context->setRoot($root);
87
        $globalOptions = new GlobalOptions($style);
88
        self::$context->setGlobalOptions($globalOptions);
89
90
        /** @var \SimpleXMLElement $node */
91
        foreach ($style as $node) {
92
            $name = $node->getName();
93
            switch ($name) {
94
                case 'info':
95
                    break;
96
                case 'locale':
97
                    self::$context->getLocale()->addXml($node);
98
                    break;
99
                case 'macro':
100
                    $macro = new Macro($node, $root);
101
                    self::$context->addMacro($macro->getName(), $macro);
102
                    break;
103
                case 'bibliography':
104
                    $bibliography = new Bibliography($node, $root);
105
                    self::$context->setBibliography($bibliography);
106
                    break;
107
                case 'citation':
108
                    $citation = new Citation($node, $root);
109
                    self::$context->setCitation($citation);
110
                    break;
111
            }
112
        }
113
    }
114
115
    /**
116
     * @param DataList $data
117
     * @return string
118
     */
119
    protected function bibliography($data)
120
    {
121
122
        return self::$context->getBibliography()->render($data);
123
    }
124
125
    /**
126
     * @param DataList $data
127
     * @return string
128
     */
129
    protected function citation($data)
130
    {
131
        return self::$context->getCitation()->render($data);
132
    }
133
134
    /**
135
     * @param array|DataList $data
136
     * @param string $mode (citation|bibliography)
137
     * @return string
138
     * @throws CiteProcException
139
     */
140
    public function render($data, $mode = "bibliography")
141
    {
142
143
        if (!in_array($mode, ['citation', 'bibliography'])) {
144
            throw new \InvalidArgumentException("\"$mode\" is not a valid mode.");
145
        }
146
147
        $this->init(); //initialize
148
149
        $res = "";
150
151
        if (is_array($data)) {
152
            $data = new DataList($data);
153
        } else if (!($data instanceof DataList)) {
154
            throw new CiteProcException('No valid format for variable data. Either DataList or array expected');
155
        }
156
157
        // set CitationItems to Context
158
        self::getContext()->setCitationItems($data);
159
160
        switch ($mode) {
161
            case 'bibliography':
162
                self::$context->setMode($mode);
163
                $res = $this->bibliography($data);
164
                break;
165
            case 'citation':
166
                self::$context->setMode($mode);
167
                $res = $this->citation($data);
168
        }
169
        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...
170
171
        return $res;
172
    }
173
174
    /**
175
     * initializes CiteProc and start parsing XML stylesheet
176
     */
177
    public function init()
178
    {
179
        self::$context = new Context($this);
180
        self::$context->setLocale(new Locale\Locale($this->lang)); //init locale
181
        $this->styleSheetXml = new \SimpleXMLElement($this->styleSheet);
182
        $this->parse($this->styleSheetXml);
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function renderCssStyles()
189
    {
190
        if (self::getContext() === null) {
191
            $this->init();
192
        }
193
194
        if (self::getContext()->getCssStyle() == null) {
195
            $cssStyle = new CssStyle(self::getContext()->getBibliographySpecificOptions());
196
            self::getContext()->setCssStyle($cssStyle);
197
        }
198
199
        return self::getContext()->getCssStyle()->render();
200
    }
201
}