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

Parser::parseStylesheet()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 26
nc 7
nop 1
dl 0
loc 29
ccs 26
cts 26
cp 1
crap 7
rs 8.5706
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\CiteProc;
14
use Seboettg\CiteProc\Exception\CiteProcException;
15
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
16
use Seboettg\CiteProc\Config;
17
use Seboettg\CiteProc\Locale\Locale;
18
use Seboettg\CiteProc\Root\Info;
19
use Seboettg\CiteProc\Root\Root;
20
use Seboettg\CiteProc\Style\Bibliography;
21
use Seboettg\CiteProc\Style\Citation;
22
use Seboettg\CiteProc\Style\Macro;
23
use Seboettg\CiteProc\Style\Options\GlobalOptions;
24
use Seboettg\CiteProc\StyleSheet;
25
use SimpleXMLElement;
26
27
class Parser
28
{
29
    /**
30
     * @param SimpleXMLElement $styleSheet
31
     * @throws CiteProcException
32
     * @throws InvalidStylesheetException
33
     */
34 167
    public function parseStylesheet(SimpleXMLElement $styleSheet)
35
    {
36 167
        $root = new Root();
37 167
        $root->initInheritableNameAttributes($styleSheet);
38 167
        CiteProc::getContext()->setRoot($root);
39 167
        $globalOptions = new GlobalOptions($styleSheet);
40 167
        CiteProc::getContext()->setGlobalOptions($globalOptions);
41
42 167
        foreach ($styleSheet as $node) {
43 167
            $name = $node->getName();
44
            switch ($name) {
45 167
                case 'info':
46 159
                    CiteProc::getContext()->setInfo(new Info($node));
47 159
                    break;
48 167
                case 'locale':
49 51
                    CiteProc::getContext()->getLocale()->addXml($node);
50 51
                    break;
51 167
                case 'macro':
52 67
                    $macro = new Macro($node, $root);
53 67
                    CiteProc::getContext()->addMacro($macro->getName(), $macro);
54 67
                    break;
55 167
                case 'bibliography':
56 82
                    $bibliography = new Bibliography($node, $root);
57 82
                    CiteProc::getContext()->setBibliography($bibliography);
58 82
                    break;
59 156
                case 'citation':
60 156
                    $citation = new Citation($node, $root);
61 156
                    CiteProc::getContext()->setCitation($citation);
62 167
                    break;
63
            }
64
        }
65 167
    }
66
67
    /**
68
     * @param Config\Locale $locale
69
     * @throws CiteProcException
70
     */
71 167
    public function parseLocale(Config\Locale $locale)
72
    {
73 167
        CiteProc::getContext()->setLocale(new Locale($locale)); //parse locale
74 167
    }
75
}
76