Passed
Push — new-api ( 42b595 )
by Sebastian
06:18
created

CiteProc::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 4
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
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 Seboettg\CiteProc\Config;
13
use Seboettg\CiteProc\Data\DataList;
14
use Seboettg\CiteProc\Exception\CiteProcException;
15
use Seboettg\CiteProc\Util\CiteProcHelper;
16
use Seboettg\CiteProc\Util\Parser;
17
use Seboettg\CiteProc\Util\Renderer;
18
19
/**
20
 * Class CiteProc
21
 * @package Seboettg\CiteProc
22
 *
23
 * @author Sebastian Böttger <[email protected]>
24
 */
25
class CiteProc implements CiteProcInterface
26
{
27
    /**
28
     * @var Context
29
     */
30
    private static $context;
31
32
    /**
33
     * @return ?Context
34
     */
35 176
    public static function getContext(): ?Context
36
    {
37 176
        return self::$context;
38
    }
39
40
    /**
41
     * @param ?Context $context
42
     */
43 25
    public static function setContext(?Context $context)
44
    {
45 25
        self::$context = $context;
46 25
    }
47
48
    /** @var Config\Locale|null */
49
    protected $locale;
50
51
    /** @var StyleSheet */
52
    protected $styleSheet;
53
54
    /** @var array */
55
    protected $markupExtension;
56
57
    /** @var bool */
58
    protected $styleSheetParsed = false;
59
60
    /** @var bool */
61
    protected $localeParsed = false;
62
63
    /** @var Parser */
64
    protected $parser;
65
66
    /** @var Renderer */
67
    protected $renderer;
68
69
    /**
70
     * CiteProc constructor.
71
     * @param StyleSheet $styleSheet
72
     * @param ?Config\Locale $locale
73
     * @param array $markupExtension
74
     */
75 167
    public function __construct(StyleSheet $styleSheet, ?Config\Locale $locale = null, array $markupExtension = [])
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$locale" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$locale"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$markupExtension" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$markupExtension"; expected 0 but found 1
Loading history...
76
    {
77 167
        $this->styleSheet = $styleSheet;
78 167
        $this->locale = ($locale ?? Config\Locale::EN_US());
79 167
        $this->markupExtension = $markupExtension;
80 167
        $this->parser = new Parser();
81 167
        $this->renderer = new Renderer();
82 167
        self::$context = new Context();
83 167
    }
84
85 167
    public function __destruct()
86
    {
87 167
        self::$context = null;
88 167
    }
89
90
    public function setLocale(?Config\Locale $locale): CiteProcInterface
91
    {
92
        $this->locale = $locale;
93
        $this->localeParsed = false;
94
        return $this;
95
    }
96
97 1
    public function setStyleSheet(StyleSheet $stylesheet): CiteProcInterface
98
    {
99 1
        $this->styleSheet = $stylesheet;
100 1
        $this->styleSheetParsed = false;
101 1
        return $this;
102
    }
103
104
    /**
105
     * @param array|DataList $data
106
     * @param Config\RenderingMode $mode (citation|bibliography)
107
     * @param array $citationItems
108
     * @param bool $citationAsArray
109
     * @return string
110
     * @throws CiteProcException
111
     */
112 163
    public function render($data, Config\RenderingMode $mode, $citationItems = [], $citationAsArray = false)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationItems" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationItems"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$citationAsArray" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationAsArray"; expected 0 but found 1
Loading history...
113
    {
114 163
        if (is_array($data)) {
115 163
            $data = CiteProcHelper::cloneArray($data);
116
        }
117 163
        self::$context->setMode($mode);
118 163
        $this->init($citationAsArray); //initialize
119 163
        return $this->renderer->render($data, $mode, $citationItems);
120
    }
121
122
    /**
123
     * initializes CiteProc and start parsing XML stylesheet
124
     * @param bool $citationAsArray
125
     * @return CiteProcInterface
126
     * @throws CiteProcException
127
     */
128 167
    public function init($citationAsArray = false): CiteProcInterface
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationAsArray" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationAsArray"; expected 0 but found 1
Loading history...
129
    {
130 167
        if (null === self::$context) {
131 17
            self::$context = new Context();
132
        }
133 167
        if (!$this->localeParsed) {
134 167
            $this->parser->parseLocale($this->locale);
0 ignored issues
show
Bug introduced by
It seems like $this->locale can also be of type null; however, parameter $locale of Seboettg\CiteProc\Util\Parser::parseLocale() does only seem to accept Seboettg\CiteProc\Config\Locale, 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

134
            $this->parser->parseLocale(/** @scrutinizer ignore-type */ $this->locale);
Loading history...
135 167
            $this->localeParsed = true;
136
        }
137 167
        self::$context->setCitationsAsArray($citationAsArray);
138
        // set markup extensions
139 167
        self::$context->setMarkupExtension($this->markupExtension);
140 167
        if (!$this->styleSheetParsed) {
141 167
            $this->parser->parseStylesheet(($this->styleSheet)());
142 167
            $this->styleSheetParsed = true;
143
        }
144 167
        return $this;
145
    }
146
147
    /**
148
     * @return string
149
     */
150 3
    public function renderCssStyles(): string
151
    {
152 3
        $this->init(); //initialize
153 3
        return $this->renderer->renderCssStyles();
154
    }
155
}
156