Passed
Push — master ( dcb940...49cd7f )
by Sebastian
14:22 queued 12:58
created

CiteProc::bibliography()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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
12
use InvalidArgumentException;
13
use Seboettg\CiteProc\Data\DataList;
14
use Seboettg\CiteProc\Exception\CiteProcException;
15
use Seboettg\CiteProc\Root\Info;
16
use Seboettg\CiteProc\Style\Bibliography;
17
use Seboettg\CiteProc\Style\Citation;
18
use Seboettg\CiteProc\Style\Macro;
19
use Seboettg\CiteProc\Style\Options\GlobalOptions;
20
use Seboettg\CiteProc\Root\Root;
21
use Seboettg\CiteProc\Styles\Css\CssStyle;
22
use Seboettg\CiteProc\Util\CiteProcHelper;
23
use Seboettg\Collection\ArrayList;
24
use SimpleXMLElement;
25
26
/**
27
 * Class CiteProc
28
 * @package Seboettg\CiteProc
29
 *
30
 * @author Sebastian Böttger <[email protected]>
31
 */
32
class CiteProc
33
{
34
35
    /**
36
     * @var Context
37
     */
38
    private static $context;
39
40
41
    /**
42
     * @return Context
43
     */
44 179
    public static function getContext()
45
    {
46 179
        return self::$context;
47
    }
48
49
    /**
50
     * @param Context $context
51
     */
52 176
    public static function setContext($context)
53
    {
54 176
        self::$context = $context;
55 176
    }
56
57
    private $lang;
58
59
    /**
60
     * @var string
61
     */
62
    private $styleSheet;
63
64
    /**
65
     * @var SimpleXMLElement
66
     */
67
    private $styleSheetXml;
68
69
    /**
70
     * @var array
71
     */
72
    private $markupExtension;
73
74
    /**
75
     * CiteProc constructor.
76
     * @param string $styleSheet xml formatted csl stylesheet
77
     * @param string $lang
78
     * @param array $markupExtension
79
     */
80 170
    public function __construct($styleSheet, $lang = "en-US", $markupExtension = [])
81
    {
82 170
        $this->styleSheet = $styleSheet;
83 170
        $this->lang = $lang;
84 170
        $this->markupExtension = $markupExtension;
85 170
    }
86
87 170
    public function __destruct()
88
    {
89 170
        self::$context = null;
90 170
    }
91
92
    /**
93
     * @param SimpleXMLElement $style
94
     * @throws CiteProcException
95
     */
96 170
    private function parse(SimpleXMLElement $style)
97
    {
98 170
        $root = new Root();
99 170
        $root->initInheritableNameAttributes($style);
100 170
        self::$context->setRoot($root);
101 170
        $globalOptions = new GlobalOptions($style);
102 170
        self::$context->setGlobalOptions($globalOptions);
103
104
        /** @var SimpleXMLElement $node */
105 170
        foreach ($style as $node) {
106 170
            $name = $node->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            /** @scrutinizer ignore-call */ 
107
            $name = $node->getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107 170
            switch ($name) {
108 170
                case 'info':
109 162
                    self::$context->setInfo(new Info($node));
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $node of Seboettg\CiteProc\Root\Info::__construct() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
                    self::$context->setInfo(new Info(/** @scrutinizer ignore-type */ $node));
Loading history...
110 162
                    break;
111 170
                case 'locale':
112 54
                    self::$context->getLocale()->addXml($node);
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $xml of Seboettg\CiteProc\Locale\Locale::addXml() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
                    self::$context->getLocale()->addXml(/** @scrutinizer ignore-type */ $node);
Loading history...
113 54
                    break;
114 170
                case 'macro':
115 70
                    $macro = new Macro($node, $root);
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $node of Seboettg\CiteProc\Style\Macro::__construct() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
                    $macro = new Macro(/** @scrutinizer ignore-type */ $node, $root);
Loading history...
116 70
                    self::$context->addMacro($macro->getName(), $macro);
117 70
                    break;
118 170
                case 'bibliography':
119 85
                    $bibliography = new Bibliography($node, $root);
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $node of Seboettg\CiteProc\Style\...iography::__construct() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
                    $bibliography = new Bibliography(/** @scrutinizer ignore-type */ $node, $root);
Loading history...
120 85
                    self::$context->setBibliography($bibliography);
121 85
                    break;
122 159
                case 'citation':
123 159
                    $citation = new Citation($node, $root);
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $node of Seboettg\CiteProc\Style\Citation::__construct() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
                    $citation = new Citation(/** @scrutinizer ignore-type */ $node, $root);
Loading history...
124 159
                    self::$context->setCitation($citation);
125 159
                    break;
126
            }
127
        }
128 170
    }
129
130
    /**
131
     * @param DataList $data
132
     * @return string
133
     */
134 78
    protected function bibliography($data)
135
    {
136
137 78
        return self::$context->getBibliography()->render($data);
138
    }
139
140
    /**
141
     * @param DataList $data
142
     * @param ArrayList $citationItems
143
     * @return string
144
     */
145 104
    protected function citation($data, $citationItems)
146
    {
147 104
        return self::$context->getCitation()->render($data, $citationItems);
148
    }
149
150
    /**
151
     * @param array|DataList $data
152
     * @param string $mode (citation|bibliography)
153
     * @param array $citationItems
154
     * @param bool $citationAsArray
155
     * @return string
156
     * @throws CiteProcException
157
     */
158 166
    public function render($data, $mode = "bibliography", $citationItems = [], $citationAsArray = false)
159
    {
160 166
        if (is_array($data)) {
161 166
            $data = CiteProcHelper::cloneArray($data);
162
        }
163
164 166
        if (!in_array($mode, ['citation', 'bibliography'])) {
165
            throw new InvalidArgumentException("\"$mode\" is not a valid mode.");
166
        }
167
168 166
        $this->init($citationAsArray); //initialize
169
170 166
        $res = "";
171
172 166
        if (is_array($data)) {
173 166
            $data = new DataList(...$data);
174
        } elseif (!($data instanceof DataList)) {
0 ignored issues
show
introduced by
$data is always a sub-type of Seboettg\CiteProc\Data\DataList.
Loading history...
175
            throw new CiteProcException('No valid format for variable data. Either DataList or array expected');
176
        }
177
178 166
        switch ($mode) {
179 166
            case 'bibliography':
180 78
                self::$context->setMode($mode);
181
                // set CitationItems to Context
182 78
                self::getContext()->setCitationData($data);
183 78
                $res = $this->bibliography($data);
184 78
                break;
185 104
            case 'citation':
186 104
                if (is_array($citationItems)) {
0 ignored issues
show
introduced by
The condition is_array($citationItems) is always true.
Loading history...
187 104
                    $citationItems = new ArrayList(...$citationItems);
188
                } elseif (!($citationItems instanceof ArrayList)) {
189
                    throw new CiteProcException('No valid format for variable `citationItems`, ArrayList expected.');
190
                }
191 104
                self::$context->setMode($mode);
192
                // set CitationItems to Context
193 104
                self::getContext()->setCitationItems($citationItems);
194 104
                $res = $this->citation($data, $citationItems);
195
        }
196 166
        self::setContext(null);
197
198 166
        return $res;
199
    }
200
201
    /**
202
     * initializes CiteProc and start parsing XML stylesheet
203
     * @param bool $citationAsArray
204
     * @throws CiteProcException
205
     */
206 170
    public function init($citationAsArray = false)
207
    {
208 170
        self::$context = new Context();
209 170
        self::$context->setLocale(new Locale\Locale($this->lang)); //init locale
210 170
        self::$context->setCitationsAsArray($citationAsArray);
211
        // set markup extensions
212 170
        self::$context->setMarkupExtension($this->markupExtension);
213 170
        $this->styleSheetXml = new SimpleXMLElement($this->styleSheet);
214 170
        $this->parse($this->styleSheetXml);
215 170
    }
216
217
    /**
218
     * @return string
219
     * @throws CiteProcException
220
     */
221 3
    public function renderCssStyles()
222
    {
223 3
        if (self::getContext() === null) {
224 3
            $this->init();
225
        }
226
227 3
        if (self::getContext()->getCssStyle() == null) {
228 3
            $cssStyle = new CssStyle(self::getContext()->getBibliographySpecificOptions());
229 3
            self::getContext()->setCssStyle($cssStyle);
230
        }
231
232 3
        return self::getContext()->getCssStyle()->render();
233
    }
234
}
235