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

Renderer::render()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.5034

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 23
nc 15
nop 3
dl 0
loc 30
ccs 18
cts 23
cp 0.7826
crap 7.5034
rs 8.6186
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2020 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Util;
12
13
use Seboettg\CiteProc\Config;
14
use Seboettg\CiteProc\Data\DataList;
15
use Seboettg\CiteProc\Exception\CiteProcException;
16
use Seboettg\CiteProc\Rendering\Observer\RenderingObserver;
17
use Seboettg\CiteProc\Rendering\Observer\RenderingObserverTrait;
18
use Seboettg\CiteProc\Style\Bibliography;
19
use Seboettg\CiteProc\Style\Citation;
20
use Seboettg\CiteProc\Style\Options\BibliographyOptions;
21
use Seboettg\CiteProc\Styles\Css\CssStyle;
22
use Seboettg\Collection\ArrayList;
23
24
class Renderer implements RenderingObserver
25
{
26
    use RenderingObserverTrait;
27
28
    /** @var CssStyle */
29
    private $cssStyle;
30
31
    /** @var BibliographyOptions */
32
    private $bibliographySpecificOptions;
33
34
    /** @var Bibliography */
35
    private $bibliography;
36
37
    /** @var Citation */
38
    private $citation;
39
40 173
    public function __construct(
41
        ?Bibliography $bibliography,
42
        ?Citation $citation,
43
        ?BibliographyOptions $bibliographySpecificOptions
44
    ) {
45 173
        $this->bibliography = $bibliography;
46 173
        $this->citation = $citation;
47 173
        $this->bibliographySpecificOptions = $bibliographySpecificOptions;
48 173
        $this->initObserver();
49 173
    }
50
51
    /**
52
     * @param array|DataList $data
53
     * @param Config\RenderingMode $mode
54
     * @param array|ArrayList $citationItems
55
     * @return string|array
56
     * @throws CiteProcException
57
     */
58 169
    public function render($data, Config\RenderingMode $mode, $citationItems = [])
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...
59
    {
60 169
        $res = "";
61 169
        if (is_array($data)) {
62 169
            $data = new DataList(...$data);
63
        } elseif (!($data instanceof DataList)) {
0 ignored issues
show
introduced by
$data is always a sub-type of Seboettg\CiteProc\Data\DataList.
Loading history...
64
            throw new CiteProcException('No valid format for variable data. Either DataList or array expected');
65
        }
66 169
        if (is_array($citationItems)) {
67 169
            $citationItems = new ArrayList(...$citationItems);
68
        } elseif (!($citationItems instanceof ArrayList)) {
0 ignored issues
show
introduced by
$citationItems is always a sub-type of Seboettg\Collection\ArrayList.
Loading history...
69
            throw new CiteProcException('No valid format for variable ' .
70
                '`citationItems`, ' .
71
                'array or ArrayList expected.');
72
        }
73 169
        $this->setCitationData($data);
74 169
        switch ((string)$mode) {
75 169
            case Config\RenderingMode::BIBLIOGRAPHY:
76 80
                $this->setMode($mode);
77
                // set CitationItems to Context
78 80
                $this->setCitationData($data);
79 80
                $res = $this->bibliography($data);
80 80
                break;
81 103
            case Config\RenderingMode::CITATION:
82 103
                $this->setMode($mode);
83
                // set CitationItems to Context
84 103
                $this->setCitationItems($citationItems);
85 103
                $res = $this->citation($data);
86
        }
87 169
        return $res;
88
    }
89
90
    /**
91
     * @return string
92
     */
93 3
    public function renderCssStyles(): string
94
    {
95 3
        $res = "";
96 3
        if (null === $this->cssStyle && !empty($this->bibliographySpecificOptions)) {
97 3
            $this->cssStyle = new CssStyle($this->bibliographySpecificOptions);
98 3
            $res = $this->cssStyle->render();
99
        }
100 3
        return $res;
101
    }
102
103
    /**
104
     * @param DataList $data
105
     * @return string|array
106
     */
107 80
    protected function bibliography(DataList $data)
108
    {
109 80
        return $this->bibliography->render($data);
110
    }
111
112
    /**
113
     * @param DataList $data
114
     * @return string|array
115
     */
116 103
    protected function citation(DataList $data)
117
    {
118 103
        return $this->citation->render($data);
119
    }
120
}
121