Passed
Push — master ( 279db1...661b73 )
by Sebastian
18:02 queued 11:37
created

CiteProc::citation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
/**
28
 * Class CiteProc
29
 * @package Seboettg\CiteProc
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
30
 *
31
 * @author Sebastian Böttger <[email protected]>
32
 */
3 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
33
class CiteProc
34
{
35
36
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @var Context
38
     */
39
    private static $context;
0 ignored issues
show
Coding Style introduced by
Private member variable "context" must be prefixed with an underscore
Loading history...
40
41
42
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @return Context
44
     */
45
    public static function getContext()
46
    {
47
        return self::$context;
48
    }
49
50
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
51
     * @param Context $context
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
53
    public static function setContext($context)
54
    {
55
        self::$context = $context;
56
    }
57
58
    private $lang;
0 ignored issues
show
Coding Style introduced by
Private member variable "lang" must be prefixed with an underscore
Loading history...
59
60
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
61
     * @var string
62
     */
63
    private $styleSheet;
0 ignored issues
show
Coding Style introduced by
Private member variable "styleSheet" must be prefixed with an underscore
Loading history...
64
65
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
66
     * @var SimpleXMLElement
67
     */
68
    private $styleSheetXml;
0 ignored issues
show
Coding Style introduced by
Private member variable "styleSheetXml" must be prefixed with an underscore
Loading history...
69
70
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @var array
72
     */
73
    private $markupExtension;
0 ignored issues
show
Coding Style introduced by
Private member variable "markupExtension" must be prefixed with an underscore
Loading history...
74
75
    /**
76
     * CiteProc constructor.
77
     * @param string $styleSheet xml formatted csl stylesheet
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
78
     * @param string $lang
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     * @param array $markupExtension
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __destruct()
Loading history...
89
    {
90
        self::$context = null;
91
    }
92
93
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
94
     * @param SimpleXMLElement $style
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
95
     * @throws CiteProcException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97
    private function parse(SimpleXMLElement $style)
1 ignored issue
show
Coding Style introduced by
Private method name "CiteProc::parse" must be prefixed with an underscore
Loading history...
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
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
106
        foreach ($style as $node) {
107
            $name = $node->getName();
108
            switch ($name) {
109
                case 'info':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
110
                    self::$context->setInfo(new Info($node));
111
                    break;
112
                case 'locale':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
113
                    self::$context->getLocale()->addXml($node);
114
                    break;
115
                case 'macro':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
116
                    $macro = new Macro($node, $root);
117
                    self::$context->addMacro($macro->getName(), $macro);
118
                    break;
119
                case 'bibliography':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
120
                    $bibliography = new Bibliography($node, $root);
121
                    self::$context->setBibliography($bibliography);
122
                    break;
123
                case 'citation':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
124
                    $citation = new Citation($node, $root);
125
                    self::$context->setCitation($citation);
126
                    break;
127
            }
128
        }
129
    }
130
131
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
132
     * @param DataList $data
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
133
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
134
     */
135
    protected function bibliography($data)
136
    {
137
138
        return self::$context->getBibliography()->render($data);
139
    }
140
141
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
142
     * @param DataList $data
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
143
     * @param ArrayList $citationItems
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
144
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
145
     */
146
    protected function citation($data, $citationItems)
147
    {
148
        return self::$context->getCitation()->render($data, $citationItems);
149
    }
150
151
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
152
     * @param array|DataList $data
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
153
     * @param string $mode (citation|bibliography)
2 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
154
     * @param array $citationItems
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
155
     * @param bool $citationAsArray
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
156
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
157
     * @throws CiteProcException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
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)) {
0 ignored issues
show
introduced by
$data is always a sub-type of Seboettg\CiteProc\Data\DataList.
Loading history...
176
            throw new CiteProcException('No valid format for variable data. Either DataList or array expected');
177
        }
178
179
        switch ($mode) {
180
            case 'bibliography':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
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':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
187
                if (is_array($citationItems)) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
introduced by
The condition is_array($citationItems) is always true.
Loading history...
188
                    $citationItems = new ArrayList($citationItems);
189
                } else if (!($citationItems instanceof ArrayList)) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
190
                    throw new CiteProcException('No valid format for variable `citationItems`, ArrayList expected.');
191
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
204
     * @param bool $citationAsArray
3 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
205
     * @throws CiteProcException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
206
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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
Coding Style introduced by
Missing short description in doc comment
Loading history...
219
     * @return string
220
     * @throws CiteProcException
221
     */
222
    public function renderCssStyles()
223
    {
224
        if (self::getContext() === null) {
225
            $this->init();
226
        }
227
228
        if (self::getContext()->getCssStyle() == null) {
229
            $cssStyle = new CssStyle(self::getContext()->getBibliographySpecificOptions());
230
            self::getContext()->setCssStyle($cssStyle);
231
        }
232
233
        return self::getContext()->getCssStyle()->render();
234
    }
235
}
236