Passed
Push — master ( e13826...b82956 )
by Sebastian
16:47 queued 09:52
created

CiteProc::loadStyleSheet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Style\Bibliography;
12
use Seboettg\CiteProc\Style\Citation;
13
use Seboettg\CiteProc\Style\Macro;
14
15
/**
16
 * Class CiteProc
17
 * @package Seboettg\CiteProc
18
 *
19
 * @author Sebastian Böttger <[email protected]>
20
 */
21
class CiteProc
22
{
23
24
    /**
25
     * @var Context
26
     */
27
    private static $context;
28
29
30
    /**
31
     * @return Context
32
     */
33
    public static function getContext()
34
    {
35
        return self::$context;
36
    }
37
38
    /**
39
     * @param Context $context
40
     */
41
    public static function setContext($context)
42
    {
43
        self::$context = $context;
44
    }
45
46
    /**
47
     * @param $styleName
48
     * @deprecated
49
     */
50
    public static function loadStyleSheet($styleName)
51
    {
52
        return StyleSheet::loadStyleSheet($styleName);
53
    }
54
55
56
    /**
57
     * @var string
58
     */
59
    private $styleSheet;
60
61
    /**
62
     * @var \SimpleXMLElement
63
     */
64
    private $styleSheetXml;
65
66
    /**
67
     * CiteProc constructor.
68
     * @param string $styleSheet xml formatted csl stylesheet
69
     */
70
    public function __construct($styleSheet, $lang = "en-US")
71
    {
72
        $this->styleSheet = $styleSheet;
73
        self::$context = new Context();
74
        self::$context->setLocale(new Locale\Locale($lang)); //init locale
75
        $this->styleSheetXml = new \SimpleXMLElement($this->styleSheet);
76
        $this->parse($this->styleSheetXml);
77
    }
78
79
    /**
80
     * @param \SimpleXMLElement $style
81
     */
82
    private function parse(\SimpleXMLElement $style)
83
    {
84
        /** @var \SimpleXMLElement $node */
85
        foreach ($style as $node) {
86
            $name = $node->getName();
87
            switch ($name) {
88
                case 'info':
89
                    break;
90
                case 'locale':
91
                    self::$context->getLocale()->addXml($node);
92
                    break;
93
                case 'macro':
94
                    $macro = new Macro($node);
95
                    self::$context->addMacro($macro->getName(), $macro);
96
                    break;
97
                case 'bibliography':
98
                    $bibliography = new Bibliography($node);
99
                    self::$context->setBibliography($bibliography);
100
                    break;
101
                case 'citation':
102
                    $citation = new Citation($node);
103
                    self::$context->setCitation($citation);
104
                    break;
105
            }
106
        }
107
    }
108
109
    /**
110
     * @param string $data
111
     * @return string
112
     */
113
    public function bibliography($data = '')
114
    {
115
        return self::$context->getBibliography()->render($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a object<stdClass>.

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...
116
    }
117
118
    /**
119
     * @param string $data
120
     * @return string
121
     */
122
    public function citation($data = '')
123
    {
124
        return self::$context->getCitation()->render($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a object<stdClass>.

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...
125
    }
126
127
    public function render($data, $mode = "bibliography") {
128
129
        switch ($mode) {
130
            case 'bibliography':
131
                self::$context->setMode($mode);
132
                return $this->bibliography($data);
133
            case 'citation':
134
                self::$context->setMode($mode);
135
                return $this->citation($data);
136
            default:
137
                throw new \InvalidArgumentException("\"$mode\" is not a valid mode.");
138
        }
139
    }
140
}