Passed
Push — new-api ( f151f9...5a646f )
by Sebastian
04:44
created

CiteProc::setContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
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 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
use Seboettg\Collection\ArrayList;
19
20
/**
21
 * Class CiteProc
22
 * @package Seboettg\CiteProc
23
 *
24
 * @author Sebastian Böttger <[email protected]>
25
 */
26
class CiteProc implements CiteProcInterface
27
{
28
    /**
29
     * @var Context
30
     */
31
    private static $context;
32
33
    /**
34
     * @return ?Context
35
     */
36 183
    public static function getContext(): ?Context
37
    {
38 183
        return self::$context;
39
    }
40
41
    /**
42
     * @param ?Context $context
43
     */
44 27
    public static function setContext(?Context $context)
45
    {
46 27
        self::$context = $context;
47 27
    }
48
49
    /** @var Config\Locale|null */
50
    protected $locale;
51
52
    /** @var StyleSheet */
53
    protected $styleSheet;
54
55
    /** @var array */
56
    protected $markupExtension;
57
58
    /** @var bool */
59
    protected $styleSheetParsed = false;
60
61
    /** @var bool */
62
    protected $localeParsed = false;
63
64
    /** @var Parser */
65
    protected $parser;
66
67
    /** @var Renderer */
68
    protected $renderer;
69
70
    /**
71
     * CiteProc constructor.
72
     * @param StyleSheet $styleSheet
73
     * @param ?Config\Locale $locale
74
     * @param array $markupExtension
75
     */
76 173
    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...
77
    {
78 173
        $this->styleSheet = $styleSheet;
79 173
        $this->locale = ($locale ?? Config\Locale::EN_US());
80 173
        $this->markupExtension = $markupExtension;
81 173
        self::$context = new Context();
82 173
        $this->parser = new Parser();
83 173
    }
84
85 173
    public function __destruct()
86
    {
87 173
        self::$context = null;
88 173
    }
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 169
    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 169
        if (is_array($data)) {
115 169
            $data = CiteProcHelper::cloneArray($data);
116
        }
117 169
        self::$context->setMode($mode);
118 169
        $this->init($citationAsArray); //initialize
119 169
        return $this->renderer->render($data, $mode, $citationItems);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->renderer->... $mode, $citationItems) also could return the type array|string[] which is incompatible with the documented return type string.
Loading history...
120
    }
121
122
    /**
123
     * initializes CiteProc and start parsing XML stylesheet
124
     * @param bool $citationAsArray
125
     * @return CiteProcInterface
126
     * @throws CiteProcException
127
     */
128 173
    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 173
        if (null === self::$context) {
131 16
            self::$context = new Context();
132
        }
133 173
        if (!$this->localeParsed) {
134 173
            $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 173
            $this->localeParsed = true;
136
        }
137 173
        self::$context->setCitationsAsArray($citationAsArray);
138
        // set markup extensions
139 173
        self::$context->setMarkupExtension($this->markupExtension);
140 173
        if (!$this->styleSheetParsed) {
141 173
            $this->parser->parseStylesheet(($this->styleSheet)());
142 173
            $this->styleSheetParsed = true;
143
        }
144 173
        $this->renderer = new Renderer(
145 173
            self::$context->getBibliography(),
146 173
            self::$context->getCitation(),
147 173
            self::$context->getBibliographySpecificOptions()
148
        );
149 173
        self::$context->addObserver($this->renderer);
150 173
        return $this;
151
    }
152
153
    /**
154
     * @return string
155
     * @throws CiteProcException
156
     */
157 3
    public function renderCssStyles(): string
158
    {
159 3
        $this->init(); //initialize
160 3
        return $this->renderer->renderCssStyles();
161
    }
162
}
163