Passed
Push — master ( f36a08...6258cd )
by Sebastian
12:50
created

Factory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 12
ccs 6
cts 9
cp 0.6667
crap 3.3332
rs 10
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\Util;
11
12
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
13
use SimpleXMLElement;
14
15
/**
16
 * Class Factory
17
 * @package Seboettg\CiteProc\Util
18
 *
19
 * @author Sebastian Böttger <[email protected]>
20
 */
21
class Factory
22
{
23
    const CITE_PROC_NODE_NAMESPACE = "Seboettg\\CiteProc\\Rendering";
24
25
    /**
26
     * @var array
27
     */
28
    private static $nodes = [
29
30
        'layout'        => "\\Layout",
31
        'text'          => "\\Text",
32
        "macro"         => "\\Macro",
33
        "number"        => "\\Number",
34
        "label"         => "\\Label",
35
        "group"         => "\\Group",
36
        "choose"        => "\\Choose\\Choose",
37
        "if"            => "\\Choose\\ChooseIf",
38
        "else-if"       => "\\Choose\\ChooseElseIf",
39
        "else"          => "\\Choose\\ChooseElse",
40
        'date'          => "\\Date\\Date",
41
        "date-part"     => "\\Date\\DatePart",
42
        "names"         => "\\Name\\Names",
43
        "name"          => "\\Name\\Name",
44
        "name-part"     => "\\Name\\NamePart",
45
        "substitute"    => "\\Name\\Substitute",
46
        "et-al"         => "\\Name\\EtAl"
47
    ];
48
49
    /**
50
     * @param SimpleXMLElement $node
51
     * @param mixed $param
52
     * @return mixed
53
     * @throws InvalidStylesheetException
54
     */
55 158
    public static function create($node, $param = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$param" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$param"; expected 0 but found 1
Loading history...
56
    {
57 158
        $nodeClass = self::CITE_PROC_NODE_NAMESPACE . self::$nodes[$node->getName()];
58 158
        if (!class_exists($nodeClass)) {
59
            throw new InvalidStylesheetException("For node {$node->getName()} " .
60
                "does not exist any counterpart class \"" . $nodeClass .
61
                "\". The given stylesheet seems to be invalid.");
62
        }
63 158
        if ($param != null) {
64 158
            return new $nodeClass($node, $param);
65
        }
66 78
        return new $nodeClass($node);
67
    }
68
}
69